欢迎来到天天文库
浏览记录
ID:46496770
大小:92.00 KB
页数:10页
时间:2019-11-24
《java组件运用案例》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、组合框运用实例importjava.awt•*;importjava.awt.event.*;importjavax.swing.*;publicclassintBinaryJFrameextendsJFrameimplementsActionListener//操作数利运算结果//算术运算符privateJTextFieldtexts[];privateJComboBoxcombox;publicIntBinaryJFrame(){super(“十进制算术运算的竖式及二进制显示”);this.setSize(280,220);this.set
2、Resizable(true);Dimensiondim=getToolkit().getScreenSize();//获彳导屏幕分辨率this.setLocation((dim.width-this.getWidth())/2z(dim.height-this.getHeight())/2);//窗口居中this•setBackground(Color.lightGray);this.setDefaultCloseOperation(EXIT_ON_CLOSE);//结束上进无呈intnumber=3;//设置彳了数为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)//组合框,默认combox=newJComboBox(operators);不可编辑panel•ad
4、d(combox);}if(i==number-1){JButtonbutton=newJButton(n=n);panel.add(button);button・eddActionListener(this);//注刖单击事件监0T器卑}texts[i]=newJTextField("0"z5);texts[i]•setHorizontalAlignment(Swingconstants•RIGHT);//.文本彳亍彳i对齐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,符串不能转换成整数,请重新输入!");}finally{}}publicstaticvoidmain(Stringarg[]){newlntBinaryJFrame();}}计算器运用实例importjava>awt•*;importjavax.swing.*;importjava.awt•event•*;publicclassjisuanqiexten
8、dsJFrameimplementsActionListener{privateJTextFieldtext_math;privateJPanelto
此文档下载收益归作者所有