這裏是問題。我有一個JApplet,在applet中有兩個JPanel,panel1和panel2。每個面板都有一個標籤,顯示爲「panel1」或「panel2」,每個面板都有一個稱爲「開關」的按鈕。當我運行小程序時,我只想讓panel1可見。當我點擊開關按鈕時,我希望panel1不可見(或消失),並且panel2可見。我還想要點擊面板2中的開關按鈕切換回面板1。誰能幫我這個?在JApplet中切換JPanel
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);
}
}
謝謝,但有沒有辦法做到這一點沒有cardlayout? – tianz
['Container#remove'](http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#remove%28java.awt.Component%29)和['Container#添加'](http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#add%28java.awt.Component%29) – MadProgrammer
*「..有什麼辦法這樣做沒有cardlayout?「*是否有任何理由**不**使用'CardLayout'?在這些情況下將「爲什麼」添加到「什麼」總是方便的。 –