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



我正在尝试在JFrame中创建两个不同宽度的面板。我希望我的右面板宽度是左面板的两倍。我正在尝试使用 GridBagLayout,对我的左面板使用 gridwidth = 1,对我的右面板使用 gridwidth = 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(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();
    }
}

最新更新