2016-07-13 2 views
2

요구 사항은 2 개의 패널과 ie가 있어야한다는 것입니다. Panel1, Panel2. Panel1에는 2 개의 버튼이 있습니다. 버튼을 클릭하면 Panel1의 해당 버튼과 관련된 구성 요소가 동적으로 표시되어야합니다. 클래스의JPanel의 모든 구성 요소를 동적으로 삭제하는 방법

public class ListenerForRadioButton implements ActionListener{ 

JButton browseGlobal; 
JFrame ParentFrame = new JFrame("Bla-Bla"); 
JPanel ChildPanel2 = new JPanel(); 
JButton upload ; 

public ListenerForRadioButton(JFrame JFrameConstructor, JPanel JPanelConstructor, JButton uploadConstructor){ 
    this.ParentFrame = JFrameConstructor; 
    this.ChildPanel2 = JPanelConstructor; 
    this.upload = uploadConstructor; 
} 

public void actionPerformed(ActionEvent event){ 

    //ChildPanel2.remove(upload); 
    ChildPanel2.remove(upload); 
    System.out.println("My listener is called"); 

} 방법 의 // 끝} // 끝

Public 클래스 Create_JFrame는 {

public Create_JFrame(){ 

    //Create a Frame 
    JFrame ParentFrame = new JFrame("Bla-Bla"); 
    JPanel ChildPanel1 = new JPanel(); 
    JPanel ChildPanel2 = new JPanel(); 
    JButton Option1 = new JButton("Option1"); 
    JButton browse = new JButton("Browse"); 
    JButton upload = new JButton("Upload"); 

    //Layout management 
    ParentFrame.getContentPane().add(BorderLayout.WEST, ChildPanel1); 
    ParentFrame.getContentPane().add(BorderLayout.EAST, ChildPanel2); 


    //Create a button 
    browse.addActionListener(new ListenerForRadioButton(ParentFrame,ChildPanel2,upload)); //Registering my listener 

    ChildPanel2.add(browse); 
    ChildPanel2.add(upload); 
    ChildPanel1.add(Option1); 


    //Make the frame visible 
    ParentFrame.setSize(500, 300); 
    ParentFrame.setVisible(true); 
    }//end of Main 
}//end of Class 
+0

'모든 구성 요소 제거'효과가 나오는 단어로 시작하는 모든 문제 문장은 ['CardLayout']을 사용하여 더 잘 해결할 수 있습니다 (http://download.oracle.com/javase/8/docs). /api/java/awt/CardLayout.html) [이 답변] (http://stackoverflow.com/a/5786005/418556)에 나와 있습니다. –

답변

5

removeAll()을 사용하면 Container에서 모든 구성 요소를 제거 할 수 있습니다.

ChildPanel2.removeAll(); 
ChildPanel2.revalidate(); 
ChildPanel2.repaint(); 
+0

감사합니다! 그 완벽하게 일했다 :) – Krishan

2

구성 요소를 확인하고 삭제

Component[] components = ChildPanel2.getComponents(); 

for (Component component : components) { 
    ChildPanel2.remove(b); 
} 

ChildPanel2.revalidate(); 
ChildPanel2.repaint(); 

JFrame의 확장 참고 : d에 싶지 않으면 모든 구성 요소를 elete, 구성 요소가 죽을 후보가 있는지 remove 확인 전에 조건을 삽입하십시오.

출처 :1, 2, 3.

관련 문제