Java:gridbaglayout混乱

时间:2012-11-06 14:12:27

标签: java swing gridbaglayout

这是我在这里的第一个问题,所以当我违反任何规则或不使用正确的格式时请原谅我

我在java swing中创建一个简单的表单,包含1个JLabel,1个JTextField和1个按钮

|---------------------------|
|                           |
|           JLabel          |
|                           |
|---------------------------|
|    JTextField    | Button |
|---------------------------|

Button应位于右下角,JTextField位于其左侧,JLabel位于顶部,跨越两列

我希望Button是一个固定的大小,JTextField是一个固定的高度,但是使用全宽度(除了Button使用的内容),以及使用所有其他空间(带和高度)的JLabel

我甚至不确定我是否应该使用GridBagLayout或其他布局?

这可能是一个非常简单的问题,但是让我困惑了很长一段时间(我猜想GridBarLayout的选项太多了)

4 个答案:

答案 0 :(得分:2)

首先,将面板的布局设置为GridBagLayout

然后,创建一个GridBagConstraints对象并将填充设置为GridBagConstraints.BOTH

对于JLabel,在约束对象上设置以下属性:gridx = 0, gridy = 0, gridwidth = 2, gridheight = 2, weightx = 1, weighty = 1

对于JTextField,在约束对象上设置以下属性:gridx = 0, gridy = 1, gridwidth = 1, gridheight = 1, weightx = 1, weighty = 0

对于JButton,在约束对象上设置以下属性:gridx = 1, gridy = 1, gridwidth = 1, gridheight = 1, weightx = 0, weighty = 0

答案 1 :(得分:2)

类BorderLayout易于使用,功能不如GridBagLayout。

但是当事情很简单时,解决方案必须是相同的。

panel.add( label, BorderLayout.CENTER );
JPanel south = new JPanel();
south.add( textfield );
south.add( button );
button.setPreferredSize( x, y );
panel.add( south, BorderLayout.SOUTH );

答案 2 :(得分:1)

好的,这是一个演示代码,可以帮助您:

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestGridBagLayout {

    protected void initUI() {
        JFrame frame = new JFrame(TestGridBagLayout.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button = new JButton("A button");
        JTextField textField = new JTextField();
        JLabel label = new JLabel("A cool long nice label that will stretch.");
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setBorder(BorderFactory.createLineBorder(Color.GREEN));
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;// Fill the "cell" in both direction
        gbc.weightx = 1.0;// Allocate extra-width to the label
        gbc.weighty = 1.0;// Allocate extra-height to the label
        gbc.gridwidth = GridBagConstraints.REMAINDER;// The label takes all the available width of the "row"
        panel.add(label, gbc);

        gbc.weighty = 0; // Don't stretch TF vertically
        gbc.fill = GridBagConstraints.HORIZONTAL; // Fill horizontally
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        panel.add(textField, gbc);
        gbc.weightx = 0; // No extra horizontal space is given to the button
        gbc.fill = GridBagConstraints.NONE; // No fill for the button
        panel.add(button, gbc);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestGridBagLayout().initUI();
            }
        });
    }

}

答案 3 :(得分:1)

我不知道这是常见的事情,但下面是有效的代码(主要来自Dan和Guillaume的代码)

    //show stuff
    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    //show label
    c.fill = GridBagConstraints.BOTH;           // Fill the "cell" in both direction
    c.weightx = 1.0;                            // Allocate extra-width to the label
    c.weighty = 1.0;                            // Allocate extra-height to the label
    c.gridwidth = GridBagConstraints.REMAINDER; // The label takes all the available width of the "row"
    add(mlblShow,c);
    //show cmd txt
    c.weighty = 0;                              // Don't stretch TF vertically
c.fill = GridBagConstraints.BOTH;           // Fill horizontally and vertically
c.gridwidth = GridBagConstraints.RELATIVE;
    add(mtxtCmd,c);
    //show send button
     c.weightx = 0;                             // No extra horizontal space is given to the button
 c.fill = GridBagConstraints.NONE;          // No fill for the button
    add(cmdSend,c);