2012-12-09 3 views
2

GridBagConstraints를 사용하여 레이아웃을 업데이트합니다. 버튼을 클릭하면 일부 입력 필드가 나타납니다.GridBagConstraints를 사용하여 레이아웃 업데이트

private class EventListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      input_panel.removeAll(); 
       String str = e.getActionCommand(); 
       System.out.print(str); 

       JLabel jl = new JLabel("Label "); 
      // Create constraints 
       GridBagConstraints textFieldConstraints = new GridBagConstraints(); 
       GridBagConstraints labelConstraints = new GridBagConstraints(); 

       labelConstraints.gridx = 0; 
       labelConstraints.gridy = 0; 

       input_panel.add(jl,textFieldConstraints); 
     } 
    } 

이 기능은 성공적으로 이상한 문제가 나는 버튼을 클릭하면, 그것은 업데이 트를 수행하지만, 즉시 표시되지, 내가 그것을 볼 수있는 창 크기를 조정해야한다,이, 그러나 실행됩니다. 무슨 일이야?

답변

4
input_panel.add(jl,textFieldConstraints); 
input_panel.revalidate(); //try to add this 
input_panel.repaint(); // and this 
+0

또는 더 나은,'(이)'재 페인트 작동()' –

+0

'다음에 재 검증. @@ ... 당신에게 편집했다 – panda

+0

너무 감사합니다. 특히 부품을 제거한 경우에는 페인트 칠이 필요할 수 있습니다. @PandaYang :'revalidate()'는 컨테이너 레이아웃 관리자에게 구성 요소의 재 레이아웃을 요청하고'repaint()'는 페인트 메소드로 더러운 화면 부분을 정리할 수 있음을 이해합니다. –

관련 문제