1

我正在使用网格包布局来创建如下所示的布局: 在此处输入图像描述

但我所拥有的是: 在此处输入图像描述

为什么会这样?我已经指定了左对齐并占据了所有水平空间,但我仍然得到了这个。这是我的代码:

public DepotView()
{
    setSize(FRAME_WIDTH,FRAME_HEIGHT);
    setLocationRelativeTo ( null );

    getContentPane ().setLayout ( new GridBagLayout());

    JPanel workerPanel = createTextAreaPanel("Processing: ",workerArea);
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.weightx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    getContentPane().add ( workerPanel );


    JPanel customerPanel = createTextAreaPanel("Customers: ",textArea);
    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 0.5;
    c.insets = new Insets(5,5,5,5);
    getContentPane().add ( customerPanel );

    JPanel depotPanel = createTextAreaPanel("Depot: ",depotArea);
    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 0.5;
    c.insets = new Insets(5,5,5,5);
    getContentPane().add ( depotPanel );


    //pack();
    setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
4

3 回答 3

4

问题是框​​架的大小小于内容窗格的总首选大小。从那里,你得到一个旋转的布局。

还有几件事:

  • 使用pack()而不是setSize()在您的 JFrame 上获得适当的帧大小。
  • 避免使用gridx/gridy,它们往往会使约束复杂且难以维护
  • anchor/fill应该几乎总是与 0 结合weightx和/或weighty大于 0
  • 而不是使用默认值FlowLayout,而是JPanel使用 LayoutManager,它将利用额外的可用空间(例如BorderLayout
  • 不要使用static变量,它只是邪恶的
  • 您的 textarea 变量始终为空。

这是一段似乎运行良好的代码:

import java.awt.BorderLayout;
import java.awt.Font;
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.SwingUtilities;

public class DepotView extends JFrame {

    private JTextArea textArea;
    private JTextArea depotArea;
    private JTextArea workerArea;

    public DepotView() {

        getContentPane().setLayout(new GridBagLayout());

        JPanel workerPanel = createTextAreaPanel("Processing: ", workerArea = new JTextArea());
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        getContentPane().add(workerPanel, c);

        JPanel customerPanel = createTextAreaPanel("Customers: ", textArea = new JTextArea());
        c = new GridBagConstraints();
        c.insets = new Insets(5, 5, 5, 5);
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        getContentPane().add(customerPanel, c);

        JPanel depotPanel = createTextAreaPanel("Depot: ", depotArea = new JTextArea());
        c = new GridBagConstraints();
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(5, 5, 5, 5);
        c.fill = GridBagConstraints.BOTH;
        getContentPane().add(depotPanel, c);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
    }

    private JPanel createTextAreaPanel(String label, JTextArea textArea) {
        JPanel customerQueuePanel = new JPanel(new BorderLayout());

        customerQueuePanel.add(new JLabel(label), BorderLayout.NORTH);
        textArea.setRows(15);
        textArea.setColumns(20);
        textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
        textArea.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(textArea);
        customerQueuePanel.add(scrollPane);
        return customerQueuePanel;
    }

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

}
于 2013-03-20T13:41:37.053 回答
2

看起来您只是GridBadConstriants在将面板添加到框架时忘记包含 。只需更改getContentPane().add(panel);getContentPane().add(panel, c);,它应该可以工作。

于 2013-03-20T13:19:11.747 回答
0

的布局JPanel没有正确定义。您当前正在使用JPanel's default layout FlowLayout,在您的情况下这是一个糟糕的选择。

  • 它不会调整组件的大小以匹配容器的大小(JPanel)。
  • 根据 的大小JPanel,它会将文本放置在标签旁边。

最好的选择可能是使用BorderLayout.

还为 设置最小尺寸JPanel

JPanel customerQueuePanel = new JPanel(new BorderLayout());
customerQueuePanel.setMinimumSize(new Dimension(250, 150));
...
customerQueuePanel.add ( new JLabel(label), BorderLayout.NORTH);
customerQueuePanel.add( scrollPane, BorderLayout.CENTER); 

编辑:上面的代码是对您从问题中删除的 createTextArea 方法的编辑。

也像以前一样添加约束作为参数。

getContentPane().add(panel, c);
于 2013-03-20T13:24:39.727 回答