java - 使用 GridBagLayout 在 JFrame 内具有不同宽度的多个 JPanel

标签 java swing jpanel layout-manager gridbaglayout

我正在尝试在 JFrame 中创建两个宽度不同的面板。我希望右面板的宽度是左面板的两倍。我正在尝试通过为我的左面板使用 gridwidth = 1 和为我的右面板使用 gridwidth = 2 来使用 GridBagLayout。但是我没有得到不同的宽度,而是得到了两个宽度相同的面板。我应该改变什么?任何帮助将不胜感激。

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

public class AppFrame2 extends JFrame {

    GridBagLayout gbl;
    GridBagConstraints gbc;
    JPanel leftpanel;
    JPanel rightpanel;

    public AppFrame2() {
        this.setSize(500, 500);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.getContentPane().setBackground(Color.BLACK);

        // Settning Frame Layout
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        this.setLayout(gbl);

        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;

        // Add Panels
        leftpanel = new JPanel();
        leftpanel.setBackground(Color.green);
        this.addPanels(0, 0, 1, 1, leftpanel); // row, col, height, width component

        rightpanel = new JPanel();
        rightpanel.setBackground(Color.red);
        this.addPanels(0, 1, 1, 2, rightpanel);
    }

    private void addPanels(int row, int col, int height, int width,
        Component com) {
        gbc.gridx = col;
        gbc.gridy = row;
        gbc.gridheight = height;
        gbc.gridwidth = width;
        gbl.setConstraints(com, gbc);
        this.getContentPane().add(com);
    }

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

}

最佳答案

您的问题是您没有指定列的宽度。添加行 gbl.columnWidths = new int[] {500/3, 500/3*2}; 会将列的宽度设置为您希望的宽度。我在此处将这些行添加到您的代码中以使其工作:)

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

public class AppFrame2 extends JFrame {

    GridBagLayout gbl;
    GridBagConstraints gbc;
    JPanel leftpanel;
    JPanel rightpanel;

    public AppFrame2() {
        this.setSize(500, 500);
        this.setResizable(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.getContentPane().setBackground(Color.BLACK);

        // Settning Frame Layout
        gbl = new GridBagLayout();
        //************* New code **************//
        gbl.columnWidths = new int[] {500/3, 500/3*2};
        gbl.rowHeights = new int[] {500};
        gbl.columnWeights = new double[] {1, 1};
        gbl.rowWeights = new double[] {1};
        //************* End code **************//

        gbc = new GridBagConstraints();
        this.setLayout(gbl);

        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;

        // Add Panels
        leftpanel = new JPanel();
        leftpanel.setBackground(Color.green);
        this.addPanels(0, 0, 1, 1, leftpanel); // row, col, height, width component

        rightpanel = new JPanel();
        rightpanel.setBackground(Color.red);
        this.addPanels(0, 1, 1, 2, rightpanel);
    }

    private void addPanels(int row, int col, int height, int width,
                           Component com) {
        gbc.gridx = col;
        gbc.gridy = row;
        gbc.gridheight = height;
        gbc.gridwidth = width;
        gbl.setConstraints(com, gbc);
        this.getContentPane().add(com);
    }

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

}

关于java - 使用 GridBagLayout 在 JFrame 内具有不同宽度的多个 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27256400/

相关文章:

java - 如何使用线程异步在java中运行命令行

Java BufferedWriter 关闭()

Java - 关于 JTextField 和 JPasswordField 的问题

java - 使用卡片布局更改面板

java - 将 JPanel 添加到 Canvas

java - 子组件setSize的地方在哪里

java - 无法在 MAVEN EXPORT 中找到 Javac 编译器

java - Java 中的泛型类型使用

java - Nimbus L&F 尝试使用 UIManager 更改 JFormattedTextField 的背景颜色

java - 如何仅检查第二位数字后的月份的正确性?