【发布时间】:2012-10-31 23:19:33
【问题描述】:
谁能指出我在使用这个 java swing gui 代码时哪里出错了。我正在尝试向 JPanel 添加两个按钮,然后在设置大小后将其添加到框架中,但它似乎没有响应传递给它的 setSize 值
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,但正在将 PaintSurface 添加到 BorderLayout.CENTER。你不应该设置尺寸。让布局根据其布局的组件的大小来决定合适的大小。
-
那个编辑也不好。
标签: java swing layout layout-manager