【问题标题】:2 panel in one frame with different width2个面板在一帧不同的宽度
【发布时间】:2015-04-17 11:00:39
【问题描述】:

我需要 Java 方面的帮助,我无法在一帧中插入 2 个 JPanel 并具有不同的首选大小,这是代码:

public class canvas extends JFrame {

    public final String TITLE = "test"; /* Game's title */

    public static int wWIDTH = 800; /* WIDTH of the window's games */

    public static int wHEIGHT = 600; /* HEIGHT of the window's game */

    /* => GAME ENGINEERING ATTRIBUTES <= */

    private Dimension WINDOW_SIZE;
    private Container c;
    private rightpane rightpane;
    private leftpane leftpane;

    public canvas() {
        Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
        WINDOW_SIZE = new Dimension(wWIDTH, wHEIGHT);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle(TITLE);
        this.setUndecorated(false);
        this.setSize(WINDOW_SIZE);
        this.setResizable(false);
        this.setLocation(((SCREEN_SIZE.width / 2) - (wWIDTH / 2)),
                         ((SCREEN_SIZE.height / 2) - (wHEIGHT / 2)));

        leftpane = new leftpane();
        rightpane = new rightpane();
        //leftpane.setPreferredSize(leftpane_dim);

        this.add(rightpane,BorderLayout.EAST);
        this.add(leftpane,BorderLayout.WEST);
        this.setVisible(true);
    }

rightpaneleftpane 类:

public class rightpane extends JPanel {

    public JLabel lb;

    public rightpane() {
        this.setBackground(Color.YELLOW);
        lb = new JLabel();
        lb.setText("right paneeeeeeeee");
        this.add(lb);
    }

    public void paintComponents(Graphics g) {
        super.paintComponents(g);

    }
}

public class leftpane extends JPanel {
    public JLabel lb;

    public leftpane() {

        this.setBackground(Color.BLACK);

            lb = new JLabel();
            lb.setText("left pane");
            this.add(lb);
    }

    public void paintComponents(Graphics g) {
        super.paintComponents(g);
    }

}

这是结果:

我试过GridBagLayout

GridBagLayout gl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        this.setLayout(gl);
        c.ipadx = 200;


        c.gridwidth = 2;
        c.gridx = c.gridy = 0;
        c.anchor = GridBagConstraints.WEST;
        c.gridheight = 3;
        gl.setConstraints(leftpane, c);
        this.add(new leftpane());

        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 3;
        c.anchor = GridBagConstraints.EAST;
        gl.setConstraints(rightpane, c);
        this.add(new rightpane());

在网络中找到其他解决方案但仍无法正常工作。谁能帮帮我?

【问题讨论】:

  • 一切都取决于是否以及如何使用 JFrame 调整 JPanel 的大小
  • 有什么问题?

标签: java swing jframe jpanel awt


【解决方案1】:

您的 GridBagLayout 中存在一些问题。这是固定版本

GridBagLayout gl = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
this.setLayout(gl);
c.fill = GridBagConstraints.BOTH; // <== need to set fill 
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
c.weightx = 1; // <== need to set weight 
c.weighty = 1; // <== need to set weight 
// gl.setConstraints(leftpane, c);   // <== constraints get overridden by your add() call...
this.add(new leftpane(), c);  // pass constraints in here

c.gridx = 1;
c.weightx = 3;  <== need to set weight, note different value, space shared in ratio 1:3
c.anchor = GridBagConstraints.EAST;  // pass constraints in here

图片

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2013-12-25
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 2016-01-31
    相关资源
    最近更新 更多