边框布局不占用所有可用空间

时间:2013-04-15 13:12:19

标签: java swing user-interface

我是新手,我怀疑这个问题与BoxLayout有关。我试图在一个帧的顶部依次制作一系列文本字段和标签。这是我的代码:

public static void main(String[] args) {
    JFrame frame = new JFrame("New Message");
    frame.setSize(100, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextArea textArea = new JTextArea();
    frame.add(textArea, BorderLayout.CENTER);

    JPanel list = new JPanel();
    list.setLayout(new BoxLayout(list, BoxLayout.Y_AXIS));
    frame.add(list, BorderLayout.NORTH);

    String[] labels = {"To: ", "Cc: ", "Bcc: ", "Subject: "};
    for (int i = 0; i < labels.length; i++) {
        JLabel l = new JLabel(labels[i]);
        JTextField f = new JTextField();
        JPanel p = new JPanel();
        p.add(l, BorderLayout.WEST);
        p.add(f, BorderLayout.CENTER);
        list.add(p);
    }

    frame.pack();

    frame.setVisible(true);

}

这是结果: enter image description here

我想要的是To,Cc,Bcc和Subject一直在左边,TextField占据剩下的空间。

3 个答案:

答案 0 :(得分:3)

很少注意事项:

  • 通过将用户界面初始化包装在SwingUtilities.invokeLater
  • 中,从EDT启动您的用户界面
  • 指定JTextField的列数以及JTextArea
  • 的行和列总是一个好主意
  • 如果您之后致电setSize(),则拨打pack()无效。一般情况下,忘记在任何Swing组件上使用setSize()/setLocation/setBounds()(将所有这些留给LayoutManager)

GridBagLayout在这里做得很好。 GroupLayout也可以使用。

见这个例子:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Mail {

    protected void initUI() {
        JFrame frame = new JFrame("New Message");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel list = new JPanel(new GridBagLayout());
        frame.add(list, BorderLayout.CENTER);
        GridBagConstraints labelGBC = new GridBagConstraints();
        labelGBC.insets = new Insets(3, 3, 3, 3); // Put some space between elements for nicer look
        labelGBC.anchor = GridBagConstraints.WEST; // Align left within its cell
        GridBagConstraints fieldGBC = new GridBagConstraints();
        fieldGBC.gridwidth = GridBagConstraints.REMAINDER; // Last element of the row
        fieldGBC.weightx = 1.0; // Cell takes up all extra horizontal space
        fieldGBC.fill = GridBagConstraints.HORIZONTAL; // Fill the cell horizontally
        fieldGBC.insets = new Insets(3, 3, 3, 3); // Put some space between elements for nicer look
        String[] labels = { "To: ", "Cc: ", "Bcc: ", "Subject: " };
        for (int i = 0; i < labels.length; i++) {
            JLabel l = new JLabel(labels[i]);
            JTextField f = new JTextField(50);
            list.add(l, labelGBC);
            list.add(f, fieldGBC);
        }
        GridBagConstraints taGBC = new GridBagConstraints();
        taGBC.gridwidth = 2;
        taGBC.weightx = 1.0; // Cell takes up all extra horizontal space
        taGBC.weighty = 1.0; // Cell takes up all extra vertical space
        taGBC.fill = GridBagConstraints.BOTH; // Fill cell in both direction
        taGBC.insets = new Insets(3, 3, 3, 3); // Put some space between elements for nicer look
        JTextArea textArea = new JTextArea(10, 80);
        list.add(new JScrollPane(textArea), taGBC);

        frame.pack();

        frame.setVisible(true);
    }

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

    }
}

结果

enter image description here

答案 1 :(得分:1)

尝试java.awt.FlowLayout查看另一个组件旁边的组件。

答案 2 :(得分:1)

您可以尝试在每个BorderLayout上设置JPanel p

public static void main(String[] args) {
    JFrame frame = new JFrame("New Message");
    frame.setSize(100, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextArea textArea = new JTextArea();
    frame.add(textArea, BorderLayout.CENTER);

    JPanel list = new JPanel();
    list.setLayout(new BoxLayout(list, BoxLayout.Y_AXIS));
    frame.add(list, BorderLayout.NORTH);

    String[] labels = {"To: ", "Cc: ", "Bcc: ", "Subject: "};
    for (int i = 0; i < labels.length; i++) {
        JLabel l = new JLabel(labels[i]);
        JTextField f = new JTextField();
        JPanel p = new JPanel(new BorderLayout());
        p.add(l, BorderLayout.WEST);
        p.add(f, BorderLayout.CENTER);
        list.add(p);
    }

    frame.pack();

    frame.setVisible(true);

}