JMenuBar使用的問題?

看板java作者時間19年前 (2006/11/15 04:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/2 (看更多)
請問一下,我想用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); /*注解的地方想在這邊執行*/ 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); } } -- ▊▊█ ▊▊█ ██ 靜宜大學 水世界 Υ ▊▊█ ▊▊█ bbs.cs.pu.edu.tw ◢██◣ ◥◥◤ ◥◥◤ 140.128.9.94 ██◣◢ ※ 飄流自 :218-162-185-144.dynamic.hinet.net
文章代碼(AID): #15MY1_00 (java)
文章代碼(AID): #15MY1_00 (java)