2017-03-16 1 views
-1

OSX에서 NetBeans 8.2를 사용하고 있고 코드를 실행할 때 적절한 제목이있는 창이 나타나지만 구성 요소가 나타나지 않습니다. 모든 프레임이 표시되도록 설정했으나 여전히 문제가 지속됩니다.JFrame 누락 된 내용 패널이있는 GridBagLayout

public Project(){ 
    setTitle("Panel"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(400, 200); 
    setLocationRelativeTo(null); 
    setLayout(new GridBagLayout()); 
    setVisible(true); 

    GridBagConstraints layout = new GridBagConstraints(); 
    layout.gridy = 3; 

    JPanel topPanel = new JPanel(); 
    topPanel.setVisible(true); 
    topPanel.setLayout(new GridLayout(2,1)); 
    topPanel.add(label1); 
    topPanel.add(textField1); 

    JPanel midPanel = new JPanel(); 
    midPanel.setVisible(true); 
    midPanel.setLayout(new GridLayout(1,1)); 
    midPanel.add(button1); 

    JPanel bottomPanel = new JPanel(); 
    bottomPanel.setVisible(true); 
    bottomPanel.setLayout(new GridLayout(2,1)); 
    bottomPanel.add(label2); 
    bottomPanel.add(textField2); 

    add(topPanel); 
    add(midPanel); 
    add(bottomPanel); 
    } 

    public static void main(String[] args) { 
    Project1 frame = new Project1(); 
    } 

답변

1

public Project(){ 
    setTitle("Panel"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(400, 200); 
    setLocationRelativeTo(null); 
    setLayout(new GridBagLayout()); 
    //setVisible(true); 
    //... 
    setVisible(true); 
} 

당신은 또한 UI는 이벤트의 컨텍스트 파견 스레드

내에 구성되어 있는지 확인해야 setVisible(true);을 가지고 그것은 당신이 생성자에서 호출 할 수있는 마지막 일을
public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      Project1 frame = new Project1(); 
     } 
    }); 
} 

이렇게하면 발생할 수있는 기이함을 방지하는 데 도움이됩니다.