java - GridBagLayout:如何设置固定列宽?

标签 java swing awt gridbaglayout

多年来,我一直在尝试在以下代码中设置固定的列宽...问题是,当我向左侧面板添加标签时,宽度会自动增加...我会比如要固定列宽...

谁能帮帮我??

这是代码:

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import java.io.IOException;

public class CurrentItem extends JPanel{

    private static JFrame currentScreen; 

    public CurrentItem(JFrame p) { 

        GridBagConstraints c = new GridBagConstraints();

        //Borramos todo lo que haya en la pantalla 
        this.removeAll();
        this.revalidate();

        currentScreen = p;     

        this.setBackground(Color.LIGHT_GRAY);      

        this.setLayout(new GridBagLayout());  

        currentScreen.add(this);       

        // --->
        Panel leftPanel = new Panel(); 
        leftPanel.setBackground(Color.BLACK);

        c.weightx = 1.5;
        c.weighty = 1;
        //c.fill = GridBagConstraints.BOTH;      
        this.add(leftPanel, c);
        // <---

        // --->
        Panel secondPanel = new Panel(); 
        secondPanel.setBackground(Color.green);

        c.weightx = 0.25;
        c.weighty = 1;
        c.fill = GridBagConstraints.BOTH;

        this.add(secondPanel, c);
        // <---     

        // --->
        Panel rightPanel = new Panel(); 
        rightPanel.setBackground(Color.CYAN);

        c.weightx = 1.5;
        c.weighty = 1;
        c.fill = GridBagConstraints.BOTH;

        this.add(rightPanel, c);
        // <---

        currentScreen.setVisible(true);

        this.requestFocusInWindow();         
    }
}

最佳答案

GridBagLayout 将组件放置在网格中的矩形(单元格)中,然后使用组件的首选大小来确定单元格的大小。

权重用于确定如何在列之间分配空间(weightx)和在行之间分配空间(weighty);这对于指定调整大小行为很重要。

通常权重指定为 0.0 和 1.0 作为极端值:必要时使用中间的数字。

阅读更多 How to Use GridBagLayout


示例代码:(下面的代码设置固定宽度的列,垂直尺寸为 100%。)

Panel leftPanel = new Panel() {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(..., ...);
    }
};

删除第一列的 weightx 并覆盖 getPreferredSize() 方法。

leftPanel.setBackground(Color.BLACK);

c.weighty = 1;
c.fill = GridBagConstraints.VERTICAL;
this.add(leftPanel, c);

关于java - GridBagLayout:如何设置固定列宽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24780480/

相关文章:

java - 如何在 java 中 "disable a method"并使其在 actionPerformed 方法中处于 Activity 状态(如果有任何意义)

java - 与 Grave Accent 相同的 KeyEvent 编号是多少

java |合成是什么意思?

java - 如何在GridBaglayout中从左上角然后垂直放置组件?

java - 无法从第三列获取值

java - 用定时器绘图不工作

java - AWT-EventQueue-0 异常

java - 使用 Jackson 进行序列化 (JSON) - 获取 "No serializer found"?

Java Mission Control 代码分析器为空

java - 以编程方式调用 Google App 脚本和用户身份?