问题描述
packagecom.ibm.iwt.example;importjava.awt.*;importjavax.swing.*;importcom.ibm.iwt.operateFile.OperateFile;importcom.ibm.iwt.operateFile.OperateRSS;importcom.ibm.iwt.window.IWindowComponent;importcom.sun.syndication.io.FeedException;importjava.awt.event.*;importjava.io.IOException;importjava.util.Vector;classChangebusiness2implementsItemListener{JComboBoxbusiness2=newJComboBox();Stringstr=null;Changebusiness2(JComboBoxbusiness2){this.business2=business2;}publicvoiditemStateChanged(ItemEvente){//TODOAuto-generatedmethodstubif(e.getStateChange()==e.DESELECTED){JComboBoxbusiness=(JComboBox)e.getSource();if(business.getSelectedIndex()!=0){business2.removeAllItems();business2.addItem("二级行业分类");for(inti=0;i<VectorBusiness.bus2.size()/2;i++){str=(String)VectorBusiness.bus2.get(i);//System.out.println(str);if((str.split("#")[0]).equals(business.getSelectedItem())){business2.addItem(str.split("#")[1]);}}//System.out.println("nn");}}}}classChangebusiness3implementsItemListener{JComboBoxbusiness3=newJComboBox();JComboBoxbusiness=newJComboBox();Stringstr=null;Vectorvv=newVector();Changebusiness3(JComboBoxbusiness3,JComboBoxbusiness){this.business3=business3;this.business=business;}publicvoiditemStateChanged(ItemEvente){//TODOAuto-generatedmethodstubif(e.getStateChange()==e.DESELECTED){JComboBoxbusiness2=(JComboBox)e.getSource();if(business2.getSelectedIndex()!=0){business3.removeAllItems();business3.addItem("三级行业分类");for(inti=0;i<VectorBusiness.bus3.size()/2;i++){str=(String)VectorBusiness.bus3.get(i);//System.out.println(str);if((str.split("#")[0]).equals(business.getSelectedItem())&&(str.split("#")[1]).equals(business2.getSelectedItem())){business3.addItem(str.split("#")[2]);}}}}}}publicclassSetbusinessextendsJFrameimplementsActionListener{privatestaticfinallongserialVersionUID=1L;JButtonchoise=newJButton("选择");Vectorvec=newVector();JComboBoxbusiness3;JComboBoxbusiness;JComboBoxbusiness2;JTextAreabtext=newJTextArea();JButtonb_ok=newJButton("确定");JButtonb_cancel=newJButton("取消");JButtonb_rss=newJButton("添加RSS");VectorBusinessvb=newVectorBusiness();publicSetbusiness(){//this就是w设置窗体属性1this.setSize(250,420);this.setTitle("设置行业");this.setBackground(Color.BLUE);this.setResizable(false);//使不改变大小//使置于屏幕中间intwidth=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();intheight=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();this.setLocation((width-350)/2,(height-250)/2);//设置一大堆组件2vec.add("一级行业分类");for(inti=0;i<VectorBusiness.bus1.size()/2;i++){vec.add(VectorBusiness.bus1.get(i));}business=newJComboBox(vec);business2=newJComboBox();business2.addItem("二级行业分类");business3=newJComboBox();business2.addItem("三级行业分类");business.setPreferredSize(newDimension(100,20));business2.setPreferredSize(newDimension(100,20));business3.setPreferredSize(newDimension(100,20));choise.setPreferredSize(newDimension(100,20));btext.setBorder(BorderFactory.createLineBorder(Color.gray,1));btext.setPreferredSize(newDimension(200,150));btext.setEditable(false);//加监听事件Changebusiness2chb=newChangebusiness2(business2);Changebusiness3chb3=newChangebusiness3(business3,business);business.addItemListener(chb);business2.addItemListener(chb3);choise.addActionListener(this);b_ok.addActionListener(this);b_cancel.addActionListener(this);b_rss.addActionListener(this);//business.setSelectedIndex(-1);//business2.setSelectedIndex(-1);//business3.setSelectedIndex(-1);//布置选项面板JPanelp_box=newJPanel();p_box.setLayout(newGridLayout(3,2,5,10));p_box.add(business);p_box.add(newLabel("请选择感兴趣的行业"));p_box.add(business2);p_box.add(newLabel());p_box.add(business3);p_box.add(choise);p_box.setPreferredSize(newDimension(250,110));//布置选项面板JPanelp_boxtext=newJPanel();p_boxtext.setLayout(newBorderLayout());p_boxtext.add(p_box,BorderLayout.NORTH);p_boxtext.add(btext,BorderLayout.CENTER);p_boxtext.setPreferredSize(newDimension(250,320));//布置按钮面板JPanelp_button=newJPanel();p_button.setLayout(newFlowLayout());p_button.add(b_ok);p_button.add(b_cancel);//布置窗体this.setLayout(newBorderLayout());this.add(p_boxtext,BorderLayout.NORTH);this.add(p_button,BorderLayout.CENTER);}publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubif(e.getSource()==choise){Stringstr=(String)business3.getSelectedItem();Stringstr1=(String)business.getSelectedItem();Stringstr2=(String)business2.getSelectedItem();if(str!=null&&str!="三级行业分类"){btext.append(str1+"#"+str2+"#"+str+"n");}else{MessageDialogmd=newMessageDialog(this.getX(),this.getY()-30);md.openInformation("请选择行业分类!");md.setVisible(true);}}if(e.getSource()==b_ok){Stringstr1="未设置";Stringstr2="未设置";Stringstr3="未设置";str1=(String)business3.getSelectedItem();str2=(String)business.getSelectedItem();str3=(String)business2.getSelectedItem();String[]str=btext.getText().split("n");if(str.length>=4){OperateFile.WriteTxt(str);this.setVisible(false);IWindowComponent.setRssText(0);}else{if(str1.equals(null)||str1.equals("三级行业分类")){intx=this.getX();inty=this.getY();MessageDialogmd=newMessageDialog(x,y);md.setVisible(true);md.openInformation("请选择行业");}else{Stringstr4[]=newString[4];if(str.length<=1){for(inti=0;i<4;i++){str4[i]=str2+"#"+str3+"#"+str1;}}else{for(inti=str.length;i<4;i++){str4[i]=str2+"#"+str3+"#"+str1;}}str=str4;OperateFile.WriteTxt(str);this.setVisible(false);IWindowComponent.setRssText(0);}}}if(e.getSource()==b_cancel){Stringstr[]=newString[4];for(inti=0;i<4;i++){str[i]="未设置#未设置#未设置";}OperateFile.WriteTxt(str);Stringstr2[]=newString[10];for(inti=0;i<10;i++){str2[i]="未设置";}this.setVisible(false);JFrame.setDefaultLookAndFeelDecorated(false);ShowBusinessw=newShowBusiness(str2,str);w.setVisible(true);}if(e.getSource()==b_rss){AddRssar=newAddRss(business,business2,business3,this.getX(),this.getY());ar.setVisible(true);}}/*publicstaticvoidmain(Stringargs[]){JFrame.setDefaultLookAndFeelDecorated(true);Setbusinessw=newSetbusiness();w.setVisible(true);}*/}
解决方案
解决方案二:
显示两次什么意思??
解决方案三:
我的程序里有三个JComboBox,分别为business,business1,buisness2。每次选择business中的某一项是business1中显示相应的内容,而每次business1里显示的项都显示两遍.
解决方案四:
该回复于2010-05-16 11:23:23被版主删除
解决方案五:
而且在程序里用publicstaticvoidmain(Stringargs[]){JFrame.setDefaultLookAndFeelDecorated(true);Setbusinessw=newSetbusiness();w.setVisible(true);}不会显示两次,可是从其他类中调用这个类就显示两次。
解决方案六:
你有病啊,乱放别人的相片上去
解决方案七:
一次是旧的失去选择,一次是新的获得选择:JFramejf=newJFrame("Test");JComboBoxjcb=newJComboBox(newObject[]{1,2,3});jf.add(jcb);jcb.addItemListener(newItemListener(){@OverridepublicvoiditemStateChanged(ItemEvente){Stringtype=e.getStateChange()==ItemEvent.DESELECTED?"失去选择":"获得选择";System.out.println(e.getItem()+type);}});jf.setSize(200,200);jf.setVisible(true);jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
解决方案八:
newItemListener()换成newActionListener()就好了