GridBagLayout:如何设置固定列宽?

时间:2014-07-16 12:11:15

标签: 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();         
    }
}

2 个答案:

答案 0 :(得分:6)

GridBagLayout将组件放置在网格中的矩形(单元格)中,然后使用组件&#39; 首选尺寸以确定细胞应该有多大。

权重用于确定如何在列(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);

答案 1 :(得分:2)

如果您提前知道要添加的标签,以及每个标签的宽度:

  • 您可以在每个标签上调用label.getPreferredSize().width以确定最大标签的宽度。

  • 然后,在列中添加一个不可见的组件(Box.createHorizontalStrut(width)),以强制它始终为那么宽。

  • 然后,当您添加所有标签时,面板该列的宽度不会改变。