4

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

我正在 java swing 中创建一个简单的表单,它由 1 个 JLabel、1 个 JTextField 和 1 个按钮组成

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

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

我希望 Button 是固定大小,JTextField 是固定高度,但使用全宽(Button 使用的除外),而 JLabel 使用所有其他空间(带有和高度)

我什至不确定我应该使用 GridBagLayout 还是其他 Layout ?

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

4

4 回答 4

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

于 2012-11-06T14:32:39.263 回答
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 );
于 2012-11-06T14:33:42.003 回答
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();
            }
        });
    }

}
于 2012-11-06T15:29:35.117 回答
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);
于 2012-11-06T16:00:08.633 回答