java组件运用案例

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

组合框运用实例importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassIntBinaryJFrameextendsJFrameimplementsActionListener{privateJTextFieldtexts[];//操作数和运算结果privateJComboBoxcombox;//算术运算符publicIntBinaryJFrame(){super(十进制算术运算的竖式及二进制显示);this.setSize(280,220);this.setResizable(true);Dimensiondim=getToolkit().getScreenSize();//获得屏幕分辨率this.setLocation((dim.width-this.getWidth())/2,(dim.height-this.getHeight())/2);//窗口居中this.setBackground(Color.lightGray);this.setDefaultCloseOperation(EXIT_ON_CLOSE);//结束主进程intnumber=3;//设置行数为3行this.getContentPane().setLayout(newGridLayout(number,1));texts=newJTextField[number*2];Objectoperators[]={+,-};for(inti=0;inumber;i++){JPanelpanel=newJPanel(newFlowLayout(FlowLayout.RIGHT));this.getContentPane().add(panel);if(i==1){combox=newJComboBox(operators);//组合框,默认不可编辑panel.add(combox);}if(i==number-1){JButtonbutton=newJButton(=);panel.add(button);button.addActionListener(this);//注册单击事件监听器}texts[i]=newJTextField(0,5);texts[i].setHorizontalAlignment(SwingConstants.RIGHT);//文本行右对齐panel.add(texts[i]);texts[i+number]=newJTextField(0,8);texts[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.parseInt(texts[0].getText(),10);Stringa=Integer.toBinaryString(x);inty=Integer.parseInt(texts[1].getText(),10);Stringb=Integer.toBinaryString(y);texts[3].setText(a);texts[4].setText(b);switch(combox.getSelectedIndex()){case0:x+=y;break;case1:x-=y;break;}texts[2].setText(x+);texts[5].setText(Integer.toString(x,2));}catch(NumberFormatExceptionnfe){JOptionPane.showMessageDialog(this,字符串不能转换成整数,请重新输入!);}finally{}}publicstaticvoidmain(Stringarg[]){newIntBinaryJFrame();}}计算器运用实例importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassjisuanqiextendsJFrameimplementsActionListener{privateJTextFieldtext_math;privateJPaneltop;privateJPanelcalckeysPanel;privatefinalString[]keys={sqrt,+/-,CE,C,7,8,9,/,4,5,6,*,1,2,3,-,0,.,=,+};privateJButtonk[]=newJButton[keys.length];privatebooleanfirst=true;privatedoublere_num=0;privateStringoperator==;privatebooleanoperatev=true;publicjisuanqi(){super(计算器);this.init();this.setBounds(300,200,300,300);this.setBackground(java.awt.Color.lightGray);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);this.pack();}privatevoidinit(){text_math=newJTextField(0);text_math.setHorizontalAlignment(JTextField.RIGHT);text_math.setBackground(Color.WHITE);top=newJPanel();top.setLayout(newBorderLayout());top.add(Center,text_math);calckeysPanel=newJPanel();calckeysPanel.setLayout(newGridLayout(5,4,3,3));for(inti=0;ikeys.length;i++){k[i]=newJButton(keys[i]);calckeysPanel.add(k[i]);}getContentPane().setLayout(newBorderLayout(3,5));getContentPane().add(North,top);getContentPane().add(Center,calckeysPanel);for(inti=0;ikeys.length;i++){k[i].addActionListener(this);}}publicstaticvoidmain(String[]args){newjisuanqi();}publicvoidactionPerformed(ActionEvente){Stringlabel=e.getActionCommand();if(label.equals(CE)){text_math.setText(0);}elseif(label.equals(C)){handleC();}elseif(0123456789..indexOf(label)=0){handlenum(label);}else{handleOperator(label);}}privatevoidhandlenum(Stringk){if(first){text_math.setText(k);}elseif((k.equals(.))&&(text_math.getText().indexOf(.)0)){text_math.setText(text_math.getText()+.);}elseif(!k.equals(.)){text_math.setText(text_math.getText()+k);}first=false;}privatevoidhandleC(){text_math.setText(0);first=true;operator==;}privatevoidhandleOperator(Stringk){if(operator.equals(/)){//除法运算//如果当前结果文本框中的值等于0if(getNumberFromText()==0){//操作不合法operatev=false;text_math.setText(除数不能为零);}else{re_num/=getNumberFromText();}}elseif(operator.equals(+)){//加法运算re_num+=getNumberFromText();}elseif(operator.equals(-)){//减法运算re_num-=getNumberFromText();}elseif(operator.equals(*)){//乘法运算re_num*=getNumberFromText();}elseif(operator.equals(sqrt)){//平方根运算re_num=Math.sqrt(re_num);}elseif(operator.equals(+/-)){//正数负数运算re_num=re_num*(-1);}elseif(operator.equals(=)){//赋值运算re_num=getNumberFromText();}if(operatev){//双精度浮点数的运算longt1;doublet2;t1=(long)re_num;t2=re_num-t1;if(t2==0){text_math.setText(String.valueOf(t1));}else{text_math.setText(String.valueOf(re_num));}}//运算符等于用户按的按钮operator=k;first=true;operatev=true;}privatedoublegetNumberFromText(){doubleresult=0;try{result=Double.valueOf(text_math.getText()).doubleValue();}catch(NumberFormatExceptione){}returnresult;}}表格运用实例importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.table.DefaultTableModel;publicclassFangZhengextendsJFrameimplementsActionListener{privateJTextFieldtextn;privateJButtonjb1,jb2;privateDefaultTableModeltablemodel;publicFangZheng(){super(循环移位方阵);Dimensiondim=getToolkit().getScreenSize();this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2);this.setBackground(java.awt.Color.lightGray);this.setDefaultCloseOperation(EXIT_ON_CLOSE);JPanelpanel=newJPanel();this.getCont

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功