here is the question. I have a JApplet, and inside the applet I have two JPanels, panel1 and panel2. Each panel has a label shown “panel1” or “panel2”, and each panel has a button called “switch”. When I run the applet, I only want panel1 to be visible. And when I click the switch button, I wanna panel1 to be invisible (or disappear) and panel2 to be visible. I will also want to click the switch button in panel2 to switch back to panel1. Can anyone help me with this?
public class MyApplet extends JApplet
{
private Panel1 panel1;
private Panel2 panel2;
public void init()
{
setLayout(new FlowLayout());
panel1 = new Panel1();
panel2 = new Panel2();
add(panel1);
//add(panel2);
}
}
public class Panel1 extends JPanel
{
private JLabel label;
private JButton button;
public Panel1()
{
setLayout(new FlowLayout());
label = new JLabel("Panel1");
button = new JButton("Switch1");
add(label);
add(button);
}
}
public class Panel2 extends JPanel
{
private JLabel label;
private JButton button;
public Panel2()
{
setLayout(new FlowLayout());
label = new JLabel("Panel2");
button = new JButton("Switch2");
add(label);
add(button);
}
}
Add a “content” to the applet, where you want to switch panels in and out off.
Set this panels layout manager to
CardLayoutAdd your other panels to the “content” pane
Use the CardLayout API to switch the panels…
Have a read through How to Use CardLayout for more details