更新された回答によると、レイアウトを何にも設定していません。
とにかく、LayoutManager を使用する場合 (これを使用する必要があります) setSize()/setBounds()/setLocation()
、LayoutManager によってオーバーライドされるため、呼び出しても意味がありません (実際にはその仕事です)。
Test
クラスが extendsJFrame
を呼び出すと推測するthis.add(buttonPanel); this.add(new PaintSurface());
と、同じ制約 (のコンテンツ ペインのデフォルトの LayoutManager であるためBorderLayout.CENTER
)を持つ 2 つのコンポーネントをコンテンツ ペインに追加することになります。BorderLayout
JFrame
LayoutManager チュートリアルを読むことを検討してください。
参考までに、完全にはほど遠いですが、これは「機能している」ことを示しています。
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test extends JFrame {
private JPanel buttonPanel;
public class PaintSurface extends JButton {
public PaintSurface() {
super("Paint surface dummy");
}
}
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);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Test();
}
});
}
}