1

我是新手,我怀疑这个问题与 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);

}

这是结果: 在此处输入图像描述

我想要的是 To、Cc、Bcc 和 Subject 一直在左边,而 TextField 占据了其余的空间。

4

3 回答 3

3

一些旁注:

  • 通过将 UI 初始化包装在一个SwingUtilities.invokeLater
  • JTextField为 a 指定列数,为 a 指定行数和列数总是一个好主意JTextArea
  • setSize()如果你在之后打电话,打电话是没用的pack()。一般来说,忘记setSize()/setLocation/setBounds()在任何 Swing 组件上使用(将所有这些留给 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();
            }
        });

    }
}

结果

在此处输入图像描述

于 2013-04-15T13:59:07.793 回答
1

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

于 2013-04-15T13:16:09.737 回答
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);

}
于 2013-04-15T13:24:28.657 回答