小永
2006-11-14 19:22:45 UTC
請問一下,我想用JMenuBar有個選單來改變ContertPane的內容
程式可以正常執行。
但是為什麼我的寫法不能成功?
是我的寫法哪邊有問題嗎,還是說有可以有其它的方法?
以下是完整的程式碼
==========================================================================
public class test extends JFrame
implements ActionListener
{ JRadioButtonMenuItem change2Apanel, change2Bpanel;
Container c;
JPanel aPanel = new JPanel();
JPanel bPanel = new JPanel();
JLabel aLabel = new JLabel("Apanel");
JLabel bLabel = new JLabel("Bpanel");
public test()
{
c = getContentPane();
JMenuBar jmb = new JMenuBar();
setJMenuBar(jmb);
JMenu change = new JMenu("切換");
ButtonGroup buttongroup = new ButtonGroup();
change2Apanel = new JRadioButtonMenuItem("A_panel");
change.add(change2Apanel);
buttongroup.add(change2Apanel);
change2Apanel.addActionListener(this);
change2Bpanel = new JRadioButtonMenuItem("B_panel");
change2Bpanel.setSelected(true);
change.add(change2Bpanel);
buttongroup.add(change2Bpanel);
change2Bpanel.addActionListener(this);
jmb.add(change);
aPanel.add(aLabel);
bPanel.add(bLabel);
//c.add(aPanel);
//c.add(bPanel);
}
public void actionPerformed(ActionEvent evt)
{ if ( evt.getSource() == change2Apanel ) {
c.add(aPanel); [34m /*注解的地方想在這邊執行*/[m
System.out.println("切換到Apanel!!"); }
if ( evt.getSource() == change2Bpanel ) {
c.add(bPanel);
System.out.println("切換到Bpanel!!"); }
}
public static void main(String[] args)
{
test app = new test();
app.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent evt)
{ System.exit(0); }
});
app.setVisible(true);
}
}
--
[1;36m [34m▊▊█ ▊▊█ [36m [;30m██ [37m
[44m [1;35m靜宜大學[;44m [1;36m水世界[;44m [1;36m [;44m [1;5;36mΥ[;44m [1;33m●[;44m [1;36m▊▊█ ▊▊█ [;44m [34m█[30;40m█ [37m
[44m [1;32mbbs.cs.pu.edu.tw [;44m [30m◢██◣ [1;37m◥◥◤ ◥◥◤ [;30;44m [34m█[30;40m█[37m
[0m[30;46m [33;1m140.128.9.94[0;30;46m [36;1m﹋[37m﹌[36m﹏[0;30m█[37;1m⊙[0;30m██[46m◣◢[37;1m﹏[36m﹋[37m﹌[36m﹏[0;46m [36m█[30;40m█[0m
[1;36m※ 飄流自 :[m[1;37m218-162-185-144.dynamic.hinet.net[m
程式可以正常執行。
但是為什麼我的寫法不能成功?
是我的寫法哪邊有問題嗎,還是說有可以有其它的方法?
以下是完整的程式碼
==========================================================================
public class test extends JFrame
implements ActionListener
{ JRadioButtonMenuItem change2Apanel, change2Bpanel;
Container c;
JPanel aPanel = new JPanel();
JPanel bPanel = new JPanel();
JLabel aLabel = new JLabel("Apanel");
JLabel bLabel = new JLabel("Bpanel");
public test()
{
c = getContentPane();
JMenuBar jmb = new JMenuBar();
setJMenuBar(jmb);
JMenu change = new JMenu("切換");
ButtonGroup buttongroup = new ButtonGroup();
change2Apanel = new JRadioButtonMenuItem("A_panel");
change.add(change2Apanel);
buttongroup.add(change2Apanel);
change2Apanel.addActionListener(this);
change2Bpanel = new JRadioButtonMenuItem("B_panel");
change2Bpanel.setSelected(true);
change.add(change2Bpanel);
buttongroup.add(change2Bpanel);
change2Bpanel.addActionListener(this);
jmb.add(change);
aPanel.add(aLabel);
bPanel.add(bLabel);
//c.add(aPanel);
//c.add(bPanel);
}
public void actionPerformed(ActionEvent evt)
{ if ( evt.getSource() == change2Apanel ) {
c.add(aPanel); [34m /*注解的地方想在這邊執行*/[m
System.out.println("切換到Apanel!!"); }
if ( evt.getSource() == change2Bpanel ) {
c.add(bPanel);
System.out.println("切換到Bpanel!!"); }
}
public static void main(String[] args)
{
test app = new test();
app.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent evt)
{ System.exit(0); }
});
app.setVisible(true);
}
}
--
[1;36m [34m▊▊█ ▊▊█ [36m [;30m██ [37m
[44m [1;35m靜宜大學[;44m [1;36m水世界[;44m [1;36m [;44m [1;5;36mΥ[;44m [1;33m●[;44m [1;36m▊▊█ ▊▊█ [;44m [34m█[30;40m█ [37m
[44m [1;32mbbs.cs.pu.edu.tw [;44m [30m◢██◣ [1;37m◥◥◤ ◥◥◤ [;30;44m [34m█[30;40m█[37m
[0m[30;46m [33;1m140.128.9.94[0;30;46m [36;1m﹋[37m﹌[36m﹏[0;30m█[37;1m⊙[0;30m██[46m◣◢[37;1m﹏[36m﹋[37m﹌[36m﹏[0;46m [36m█[30;40m█[0m
[1;36m※ 飄流自 :[m[1;37m218-162-185-144.dynamic.hinet.net[m