任何人都可以指出我的错在哪里这个java swing gui代码。我想两个按钮添加到JPanel,然后将其设置规模后加入到一个框架,但它似乎没有响应传递给它Java Swing设置JPanel大小
public Test() {
GridLayout layout = new GridLayout(1, 2);
//this.setLayout(layout);
this.setSize(700, 700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonPanel = new JPanel();
buttonPanel.setSize(new Dimension(30, 100));
JButton rectButton = new JButton("Rectangle");
JButton ovalButton = new JButton("Oval");
buttonPanel.add(rectButton);
buttonPanel.add(ovalButton);
this.add(buttonPanel);
this.add(new PaintSurface());
this.setVisible(true);
}
您的代码不作很有道理。您正在使用GridLayout,但正在向BorderLayout.CENTER添加PaintSurface。你不应该设置大小。根据布局组件的大小让布局决定合适的尺寸。 –
编辑也不好。 –