java - 面板占据相同的空间

标签 java layout panel frame gridbaglayout

我有两个面板。第一个看起来像这样。

public class BoardPanel extends JPanel
{
    public BoardPanel()
    {
         setLayout(null);
         this.setOpaque(false);

        Button button = new JButton("..");
        button.setLocation(...);
        button.setSize(...);
        add(button);
    }

    public void paintComponent( Graphics g )
    {
        /*
        * Painting some stuff here. 
        */
    }   
}

另一个面板是这样的:

public class OtherPanel extends JPanel
{
    public OtherPanel()
    {
        super();
        this.setLayout(null);
        this.setOpaque(false);

        JPanel panel1 = new JPanel();
        panel1.setLocation(...);
        panel1.setSize(...);
        panel1.setOpaque( .. );

        JPanel panel2 = new JPanel();
        panel2.setLocation(...);
        panel2.setSize(...);
        panel2.setOpaque( .. );

        add(panel1):
        add(panel2);
    }   

}

之后,我将两个面板放入框架中。但我希望我的 BoardPanel 比 OtherPanel 占据更多的屏幕。所以我使用 GridBagLayout 作为框架

public class MainFrame extends JFrame
{ 
    private GridBagLayout aGridLayout = new GridBagLayout();
    private GridBagConstraints constraints = new GridBagConstraints();

    public MainFrame()
    {
        super("Quoridor");
        setLayout(gridLayout);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1366, 768);
        setVisible(true);
        setResizable(false);
        this.getContentPane().setBackground(Color.decode("#b2a6a6"));

        BoardPanel boardPanel = new BoardPanel();
        OtherPanel otherPanel = new OtherPanel();

        this.addComponent(boardPanel, 1, 1, 2, 1);
        this.addComponent(otherPanel, 1, 3, 1, 1);
    }

    public void addComponent(Component component , int row , int column , int width
            , int height)
    {
        constraints.gridx = column;
        constraints.gridy = row;
        constraints.gridwidth = width;
        constraints.gridheight = height;
        aGridLayout.setConstraints(component, constraints);
        add(component);
    }
}

问题是,框架为两个面板提供了相等的空间,并且没有为 boardPanel 提供更多的空间。 为什么会出现这种情况?它与面板的边界有关吗?

最佳答案

这里有一个关于 GridBagLayout 的很好的教程:https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html 。另请参阅下面的代码和屏幕截图。 anchor 字段将组件定位在第一行。 Weightx 字段为 boardPanel 的列提供了更多空间。 ipady 字段指定要添加到组件高度的多少。在这里,boardPanel 获得了大部分宽度和全部高度。 otherPanel 面板的高度为一半。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GridExample {
    private JFrame mainFrame;
    private JPanel boardPanel, otherPanel;

    public GridExample(){
        mainFrame = new JFrame();
        mainFrame.setSize(600,400);
        mainFrame.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent){
                System.exit(0);
            }        
        });    

        boardPanel = new JPanel();
        boardPanel.add(new JLabel("board panel"));
        boardPanel.setBackground(Color.yellow);

        otherPanel = new JPanel();
        otherPanel.add(new JLabel("other panel"));
        otherPanel.setBackground(Color.green);

        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.75;
        c.ipady = 400;
        c.gridx = 0;
        c.gridy = 0;
        mainFrame.add(boardPanel, c);

        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.25;
        c.ipady = 200;
        c.gridx = 1;
        c.gridy = 0;    
        mainFrame.add(otherPanel, c);
        mainFrame.setVisible(true);  
    }

    public static void main(String[] args){
        GridExample  swingContainerDemo = new GridExample();  
    }
}

enter image description here

关于java - 面板占据相同的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45187695/

相关文章:

java - Android导入项目失败

java - 指定 ZonedDateTime 的时区而不更改实际日期

java - 选择了 Android ListView 数组适配器

python - 如何从数据帧列字符串中提取周期和变量名称以进行多索引面板数据准备

java - 在java中创建框架内的可移动面板

java - Json 响应值未设置为 Bean 类

java - 无法从 linux 中的 eclipse juno 端口 80 启动 Apache tomcat

android - 支持不同的屏幕尺寸 - xml 未正确加载。可能的错误?

html - 网页的最佳绝对宽度是多少?

wpf - 如何将容器的高度限制为特定内容元素的高度?