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