【发布时间】: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);
}
和rightpane 和leftpane 类:
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