업데이트 된 답변에 따르면 레이아웃에 아무 것도 설정하지 않은 것입니다.
어쨌든 LayoutManager를 사용해야한다면 (실제로해야 할) setSize()/setBounds()/setLocation()
을 호출하는 것은 무의미합니다. LayoutManager (실제로는 해당 작업)에 의해 재정의 될 것이기 때문입니다.
과 같은 제약 조건이 개 구성 요소를 추가 this.add(buttonPanel); this.add(new PaintSurface());
를 호출하여, 당신의 Test
클래스 JFrame
를 확장 추측 컨텐츠 창에 (BorderLayout
때문에, BorderLayout.CENTER
하면 JFrame
의 컨텐츠 구획의 기본 LayoutManager에있다).
LayoutManager tutorial을 읽어보십시오. 그냥 정보
, 지금까지 완벽한에서,이 뭔가 "작업"을 보여줍니다 있지만 :
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();
}
});
}
}
귀하의 코드를 확인하지 않습니다 많은 감각. GridLayout을 사용하고 있지만 BorderLayout.CENTER에 PaintSurface를 추가하고 있습니다. 크기를 설정하면 안됩니다. 레이아웃을 통해 레이아웃하는 구성 요소의 크기에 따라 적절한 크기가 결정됩니다. –
그 편집도 좋지 않았습니다. –