简体   繁体   English

为什么我所有的组件都聚集在JFrame的中心

[英]Why are all my components clustered at the center of the JFrame

I'm using a grid bag layout to create a layout that looks like this: 我正在使用网格袋布局来创建如下所示的布局: 在此处输入图片说明

but what I have is this: 但是我有这个: 在此处输入图片说明

Why is this happening? 为什么会这样呢? I've specified left alignment and to take up all horizontal space but i still end up with this. 我已经指定了左对齐并占据了所有水平空间,但最终还是这样。 Here's my code: 这是我的代码:

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 );

The problem is that the size of your frame is smaller than the total preferred size of your content pane. 问题在于框架的大小小于内容窗格的总首选大小。 From there, you get a swcrewed layout. 从那里,您可以看到一个拧紧的布局。

A few more things: 还有几件事:

  • use pack() instead of setSize() on your JFrame to get an appropriate frame size. 在JFrame上使用pack()而不是setSize()来获取适当的帧大小。
  • avoid using gridx/gridy , they tend to make constraint complex and hard to maintain 避免使用gridx/gridy ,它们往往会使约束变得复杂且难以维护
  • anchor/fill should almost always be combined with weightx and/or weighty bigger than 0 anchor/fill几乎应始终与weightx和/或weighty大于0组合
  • instead of using the default FlowLayout of JPanel , use a LayoutManager which will take advantage of extra avilable space (for example BorderLayout ) 而不是使用JPanel的默认FlowLayout ,而是使用LayoutManager,它将利用额外的可用空间(例如BorderLayout
  • don't use static variables, it is just evil 不要使用static变量,这只是邪恶的
  • your textarea variables are always empty. 您的textarea变量始终为空。

Here is a piece of code which seems to work quite well: 这是一段似乎运行良好的代码:

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);
            }
        });
    }

}

It looks like you just forgot to include the GridBadConstriants when adding the panels to the frame. 好像您只是在将面板添加到框架中时忘记包括GridBadConstriants Just change getContentPane().add(panel); 只需更改getContentPane().add(panel); to getContentPane().add(panel, c); getContentPane().add(panel, c); and it should work. 它应该工作。

The layout of the JPanel isn't defined properly. JPanel的布局未正确定义。 You're currently using JPanel 's default layout FlowLayout , which is a poor choice in your case. 您当前正在使用JPanel的默认布局FlowLayout ,在您的情况下,这是一个糟糕的选择。

  • It doesn't resize the components to match the size of the container (the JPanel ). 它不会调整组件的大小以匹配容器( JPanel )的大小。
  • Depending on the size of the JPanel , it would place the text are next to the label. 根据JPanel的大小,它将文本放置在标签旁边。

The best choice would probably be to use BorderLayout . 最好的选择可能是使用BorderLayout

Also set a minimum size for the JPanel : 还要为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); 

EDIT: The code above is an edit to your createTextArea method which you removed from your question. 编辑:上面的代码是对您从问题中删除的createTextArea方法的编辑。

Also add the constraints as a parameter, like you did before. 像以前一样,也将约束作为参数添加。

getContentPane().add(panel, c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM