简体   繁体   English

边框布局与网格布局冲突

[英]Border Layout Conflicting with Grid Layout

I have 3 JPanel components. 我有3个JPanel组件。 Panel 1 is set to BorderLayout.NORTH , panel 2 is set to BorderLayout.CENTER and panel 3 is set to BorderLayout.SOUTH . 面板1设置为BorderLayout.NORTH ,面板2设置为BorderLayout.CENTER ,面板3设置为BorderLayout.SOUTH

Now panel 1 has a GridLayout(1,2) and panel 2 also has GridLayout(1,2) . 现在,面板1具有GridLayout(1,2) ,面板2也具有GridLayout(1,2) The thing that bothers me is in panels 1 and 2 the components are aligned as though they are in GridLayout combined. 困扰我的是在面板1和面板2中,组件对齐,就好像它们在GridLayout组合中一样。

To be specific I have a set of JTextField components in panel 1 in position at row=0 and column=1. 具体来说,我在面板1中有一组JTextField组件,位于row = 0和column = 1。

I have a JTable inside JScrollPane at position row=0 and column=1 in panel 2. 我在JScrollPane里面有一个JTable ,在位置为row = 0,在面板2中为column = 1。

Both these components are being rendered with the same dimension no matter what I try. 无论我尝试什么,这两个组件都以相同的尺寸呈现。

The code is too long to post here and I would appreciate if someone can clarify how to conceptually solve this problem. 代码太长了,无法在此发布,如果有人能够澄清如何从概念上解决这个问题,我将不胜感激。

public class PanelTest extends JFrame{
    public static void main (String args[]) {
        new PanelTest();
    }
    public PanelTest() {
        this.setPreferredSize(new Dimension(800,200));
        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(1,2));
        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1,2));

        JTextField textField1 = new JTextField();
        textField1.setPreferredSize(new Dimension(500, 100));
        JTextField textField2 = new JTextField();
        textField2.setPreferredSize(new Dimension(300, 100));
        panel1.setMinimumSize(new Dimension(800, 100));

        panel1.add(textField1);
        panel1.add(textField2);

        JTable table = new JTable();
        //... 
        JScrollPane jps1 = new JScrollPane(table);
        jps1.setPreferredSize(new Dimension(200, 300));
        JTextField textField3 = new JTextField(10); 
        JScrollPane jps2 = new JScrollPane(textField3);

        panel2.add(jps1);
        panel2.add(jps2);
        setLayout(new BorderLayout());
        add(panel1, BorderLayout.NORTH);
        add(panel2, BorderLayout.SOUTH);
        this.setVisible(true);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Using GridBagLayout: 使用GridBagLayout:

  public PanelTest() {
    this.setPreferredSize(new Dimension(800,200));
    this.setMinimumSize(new Dimension(800,200));
   JPanel panel1 = new JPanel();
    panel1.setLayout(new GridBagLayout());
    JPanel panel2 = new JPanel();
    panel2.setLayout(new GridBagLayout());

    JTextField textField1 = new JTextField();
    textField1.setPreferredSize(new Dimension(500, 100));
    JTextField textField2 = new JTextField();
    textField2.setPreferredSize(new Dimension(300, 100));
    panel1.setMinimumSize(new Dimension(800, 100));

    GridBagConstraints gbc = new GridBagConstraints();
    //      gbc.insets = new Insets(5,100,5,1);
    gbc.gridx=0;
    gbc.gridy=0;
    panel1.add(textField1, gbc);
    gbc.gridx=1;
    panel1.add(textField2, gbc);

    JTable table = new JTable();
    //... 
    JScrollPane jps1 = new JScrollPane(table);
    jps1.setPreferredSize(new Dimension(600, 300));
    jps1.setMinimumSize(new Dimension(600, 300));
    JTextField textField3 = new JTextField(); 

    gbc = new GridBagConstraints();
    //      gbc.insets = new Insets(5,100,5,1);
    gbc.gridx=0;
    gbc.gridy=0;
    panel2.add(jps1, gbc);
    gbc.gridx=1;
    panel2.add(textField3, gbc);
    setLayout(new BorderLayout());
    add(panel1, BorderLayout.NORTH);
    add(panel2, BorderLayout.SOUTH);
    this.setVisible(true);
    this.setVisible(true);
    this.pack();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

The thing that bothers me is in panels 1 and 2 the components are aligned as though they are in GridLayout combined. 困扰我的是在面板1和面板2中,组件对齐,就好像它们在GridLayout组合中一样。

From the Java docs : 来自Java文档

GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.

"It will display them in equal size", so, if you have 2 columns and your JFrame 's size is 900px, you will have 2 components of 450px each, if you had 3 then it will be 3 components of 300px each. “它将以相同的大小显示它们”,因此,如果你有2列且你的JFrame的大小是900px,你将拥有2个450px的组件,如果你有3个,那么它将是3个组件,每个300px。

That's why you should avoid the use of setPreferred|maximum|minimum size methods and let the Layout Manager choose the dimensions for you or if you really really need a fixed size, you should instead override the getPreferred|maximum|minimum size as said by @trashgod in his answer. 这就是为什么你应该避免使用setPreferred | maximum | minimum size方法并让布局管理器为你选择尺寸,或者如果你真的需要一个固定的尺寸,你应该改为覆盖getPreferred|maximum|minimum size,如@他的回答中有一个垃圾桶。

You probably want to use a GridBagLayout instead or a different Layout Manager such as FlowLayout or BoxLayout and combine them. 您可能希望使用GridBagLayout或不同的布局管理器 (如FlowLayoutBoxLayout)并将它们组合在一起。

You also should avoid extending JFrame unless it's extremely necessary. 你也应该避免扩展JFrame除非它非常必要。

Since you didn't posted how the UI should look like, I can imagine you wanted something like this: 既然你没有发布UI应该是什么样子,我可以想象你想要这样的东西:

在此输入图像描述

Which is done with the following code: 使用以下代码完成:

import java.awt.*;
import javax.swing.*;
public class LayoutManagerExample {
    JFrame frame;
    JPanel pane1, pane2, pane3;
    JTextField field1, field2, field3;
    JTable table;
    JScrollPane jsp;
    LayoutManagerExample() {
        frame = new JFrame("LayoutManagerExample");
        pane1 = new JPanel();
        pane2 = new JPanel();
        pane3 = new JPanel();
        field1 = new JTextField(10);
        field2 = new JTextField(10);
        field3 = new JTextField(20);
        table = new JTable();
        jsp = new JScrollPane(table);

        pane1.setLayout(new FlowLayout());
        pane2.setLayout(new GridLayout(1, 2));
        pane3.setLayout(new FlowLayout());

        pane1.add(field1);
        pane1.add(field2);
        pane2.add(jsp);
        pane2.add(field3);
        pane3.add(new JLabel("Something else"));

        frame.add(pane1, BorderLayout.NORTH);
        frame.add(pane2, BorderLayout.CENTER);
        frame.add(pane3, BorderLayout.SOUTH);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main (String args[]) {
        new LayoutManagerExample();
    }
}

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

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