資源描述:
《java組件運(yùn)用案例》由會(huì)員上傳分享,免費(fèi)在線(xiàn)閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫(kù)。
1、組合框運(yùn)用實(shí)例importjava.awt?*;importjava.awt.event.*;importjavax.swing.*;publicclassintBinaryJFrameextendsJFrameimplementsActionListener//操作數(shù)利運(yùn)算結(jié)果//算術(shù)運(yùn)算符privateJTextFieldtexts[];privateJComboBoxcombox;publicIntBinaryJFrame(){super(“十進(jìn)制算術(shù)運(yùn)算的豎式及二進(jìn)制顯示”);this.setSize(280,220);this.set
2、Resizable(true);Dimensiondim=getToolkit().getScreenSize();//獲彳導(dǎo)屏幕分辨率this.setLocation((dim.width-this.getWidth())/2z(dim.height-this.getHeight())/2);//窗口居中this?setBackground(Color.lightGray);this.setDefaultCloseOperation(EXIT_ON_CLOSE);//結(jié)束上進(jìn)無(wú)呈intnumber=3;//設(shè)置彳了數(shù)為3彳J:this?get
3、:Content:Pane().setLayout(newGridLayout(number,1));texts=newJTextField[number*2];Objectoperators[]={n+n,for(inti=0;icnumber;i++)JPanelpanel=newJPanel(newF1owLayout(FlowLayout.RIGHT));this?getContentPane().add(panel);if(i==l)//組合框,默認(rèn)combox=newJComboBox(operators);不可編輯panel?ad
4、d(combox);}if(i==number-1){JButtonbutton=newJButton(n=n);panel.add(button);button?eddActionListener(this);//注刖單擊事件監(jiān)0T器卑}texts[i]=newJTextField("0"z5);texts[i]?setHorizontalAlignment(Swingconstants?RIGHT);//.文本彳亍彳i對(duì)齊panel?add(texts[i]);texts[i+number]=newJTextField(n0n,8);tex
5、ts[i+number].setHorizontalAlignment(Swingconstants.RIGHT);texts[i+number].setEditable(false);panel?add(texts[i+number]);}texts[number-1].setEditable(false);//只能顯示,不允許編輯this.setVisible(true);}publicvoidactionPerformed(ActionEvente)//按鈕單擊事件處理方法{try{intx=Integer.parselnt(texts[
6、0].getText(),10);Stringa=Integer.toBinaryString(x);inty=Integer.parselnt(texts[1].getText(),10);Stringb=Integer?toBinA:rySt:ring(y);texts[3].setText(a);texts[4]?setText(b);switch(combox.getSelectedlndex()){case0:x+=y;break;case1:x-=y;break;}texts[2].setText(x+"");texts[5].se
7、tText(Integer?toString(x,2));}catch(NumberFormatExceptionnfe){JOptionPane.showMessageDialog(this,符串不能轉(zhuǎn)換成整數(shù),請(qǐng)重新輸入!");}finally{}}publicstaticvoidmain(Stringarg[]){newlntBinaryJFrame();}}計(jì)算器運(yùn)用實(shí)例importjava>awt?*;importjavax.swing.*;importjava.awt?event?*;publicclassjisuanqiexten
8、dsJFrameimplementsActionListener{privateJTextFieldtext_math;privateJPanelto