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灯, 水草

请输入看板名称,例如:iOS站内搜寻

TOP