hi im new to java and im learning about how to add/remove panels from another panel...
This is my main frame
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DynamicFrame extends JFrame{
private JButton jb = new JButton("Remove");
private JPanel jp = new JPanel();
public DynamicFrame(){
jp.add(jb);
add(jp);
setVisible(true);
pack();
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
remove(jp);
add( new DynamicPanel1().getD1());
validate();
pack();
}
});
}
public static void main(String a[]){
new DynamicFrame();
}
}
this is my panel 1
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DynamicPanel1 extends JPanel{
private JPanel jp = new JPanel();
private JButton jb = new JButton("Dynamic Panel 2");
private JLabel jl= new JLabel("Dynamic Panel 1");
private JPanel center =new JPanel();
public DynamicPanel1(){
add(jl);
add(jb);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//what to put?
}
});
}
public JPanel getD1(){
return this;
}
}
this is my panel2
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DynamicPanel2 extends JPanel{
private JPanel jp = new JPanel();
private JLabel jl = new JLabel("Dyanamic 2");
private JButton jb = new JButton("Dynamic Panel 1");
public DynamicPanel2(){
add(jl);
add(jb);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//what to put?
}
});
}
public JPanel getD2(){
return this;
}
}