java 板


LINE

我做了一個JCombobox,只要點選 JCombobox 的選項,就會觸發 itemStateChanged 的 事件處理,將版面更換成所選取的版面。 但只要程式呼叫執行 itemStateChanged 並做 控制 panel的事情時,就會發生例外處理 停止程式執行,只要把控制panel的程式碼移除,一切又正常。 我翻了兩本視窗程式設計的書,皆找不到答案,所以跪求板上的神人大 大們,幫我解惑,拜託了!! 以下是我的程式碼:(itemStateChanged 在最下面) import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.border.Border; public class MainFrame implements ActionListener, ItemListener{ JFrame fr; Container contenpan; //功能表 JMenuBar mb; JMenu file; JMenuItem importt; JMenuItem exit; JMenu help; JMenuItem aboutMI; JMenuItem test; //對話方塊 //關於的對話方塊 JDialog aboutMIDl; //組合方塊 JPanel machinepan; JLabel machinename; JComboBox machinecmbbox; DefaultComboBoxModel cmbModel; //ComboboxModel //分頁用panel JPanel tabbedpanel; //分頁鏈結串列 ChiefpanelList chfpl = new ChiefpanelList(); MainFrame(){ fr = new JFrame("流量監測系統"); //呼叫創建功能表的函數 buildMenuBar(); //取得mainframe的contenpan並設定排版為border; contenpan = fr.getContentPane(); BorderLayout border = new BorderLayout(1,1); contenpan.setLayout(border); //呼叫創建監測機器列表的method buildmachinepan(); //呼叫創建分頁的method //buildTabbedPane(); fr.setVisible(true); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fr.pack(); } //設定功能表的函數 private void buildMenuBar(){ mb = new JMenuBar(); file = new JMenu("檔案"); importt = new JMenuItem("匯入"); exit = new JMenuItem("關閉"); mb.add(file); file.add(importt); file.add(exit); importt.addActionListener(this); exit.addActionListener(this); help = new JMenu("幫助"); aboutMI = new JMenuItem("關於.."); test = new JMenuItem("測試用"); mb.add(help); help.add(aboutMI); help.add(test); aboutMI.addActionListener(this); test.addActionListener(this); fr.setJMenuBar(mb); } //創建組合方塊函數 private void buildmachinepan(){ machinepan = new JPanel(new GridLayout(1,2)); machinename = new JLabel("未載入"); cmbModel = new DefaultComboBoxModel(){}; machinecmbbox = new JComboBox(); machinecmbbox.setModel(cmbModel); machinecmbbox.addItemListener(this); machinepan.add(machinename); machinepan.add(machinecmbbox); machinepan.setBorder(BorderFactory.createTitledBorder("監測機器")); contenpan.add(machinepan, BorderLayout.NORTH); } //創建分頁及列表的函數 private void buildTabbedPane()throws IOException{ //讀取路徑 FileReader fr = new FileReader("logpath.txt"); BufferedReader bfr = new BufferedReader(fr); //檢查是否結束 FileReader test = new FileReader("logpath.txt"); BufferedReader bfrtest = new BufferedReader(test); while(bfrtest.readLine() != null){ String n = bfr.readLine(); String p = bfr.readLine(); chfpl.inster(n, p); //第一次取得機器名稱 , 第二次readLine取得路徑 cmbModel.addElement(n); bfrtest.readLine();//測試是否結束用 的 第二次位移換行 } fr.close(); bfrtest.close(); tabbedpanel = new JPanel(); contenpan.add(tabbedpanel, BorderLayout.CENTER); //tabbedpanel.add(chfpl.front.link.chiefpan); } public void actionPerformed(ActionEvent e){ //測試是哪一種類別呼叫事件處理 Class ca1 = new JMenuItem().getClass(); if(ca1 == e.getSource().getClass()){ //測試是哪一個物件呼叫事件處理 if(e.getSource() == test){ System.out.print("test~~"); } else if(e.getSource() == aboutMI){ aboutMIDl = new JDialog(fr, "關於..", true); JLabel authorSchool = new JLabel( "靜宜大學-CSIE",JLabel.CENTER); aboutMIDl.add(authorSchool); aboutMIDl.pack(); aboutMIDl.show(); } else if(e.getSource() == importt){ //呼叫匯入路徑的物件 new setpath(fr); //呼叫建立監測機器分頁的路徑檔 try { buildTabbedPane(); } catch (IOException e1) {e1.printStackTrace();} this.fr.pack(); } else if(e.getSource() == exit){ } System.out.println("MenuBar is OK"); } } //組合方塊處發事件處理 public void itemStateChanged(ItemEvent e){ //若選取改變時處理 if(e.getStateChange() == ItemEvent.SELECTED){ String nametemp = (String)e.getItem(); System.out.println("nametemp = " + nametemp); machinename.setText((String)cmbModel.getSelectedItem()); //更換為所選取的版面 tabbedpanel.add(chfpl.search(nametemp).chiefpan, BorderLayout.CENTER); System.out.println("itemStateChanged is called"); } } } --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.128.20.148 ※ 編輯: baconcsie 來自: 140.128.20.148 (10/06 00:58)
1F:推 chronoer:有exception log嗎? 10/06 09:58
2F:→ baconcsie:看不懂,你說的是什麼意思? 10/06 15:05
3F:→ sbrhsieh:先確定你的 ChiefpanelList 實做是正確(無問題)的。 10/06 20:32
4F:→ baconcsie:問題已經解決了~感謝板上大大解答 10/07 04:23







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP