欢迎来到天天文库
浏览记录
ID:39550204
大小:44.50 KB
页数:9页
时间:2019-07-06
《java用GUI设计简单的计算器》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、importjavax.swing.*;importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassCalculatorextendsJFrameimplementsActionListener{JPaneljpl;JTextFieldjtf;JFramec;Stringitr="";Stringf="";booleancom=false;inti=0;intsymbol=0;doublebutnum=0;JButton[]b=newJ
2、Button[20];publicstaticvoidmain(String[]args){Calculatormycalculator=newCalculator();}publicCalculator(){c=newJFrame("我的计算器");jtf=newJTextField("0",200);jpl=newJPanel();jtf.setHorizontalAlignment(JTextField.RIGHT);jtf.setPreferredSize(newDimension(0,35));jpl.se
3、tLayout(newGridLayout(5,4));String[]str={"←","CE","C","+","7","8","9","-","4","5","6","*","1","2","3","/","+/-","0",".","="};for(inti=0;i4、dowClosing(WindowEvente){System.exit(0);}});c.add(jtf,BorderLayout.NORTH);c.add(jpl,BorderLayout.CENTER);c.setBounds(100,100,300,350);c.setVisible(true);for(inti=0;i<20;i++){b[i].addActionListener(this);}}@OverridepublicvoidactionPerformed(ActionEvente){//TODOA5、uto-generatedmethodstubif(e.getSource()==b[4]){if(com6、7、i==0){jtf.setText(f+"7");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"7");}}elseif(e.getSource()==b[5]){if(com8、9、i==0){jtf.setText(f+"8");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"8");10、}}elseif(e.getSource()==b[6]){if(com11、12、i==0){jtf.setText(f+"9");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"9");}}elseif(e.getSource()==b[8]){if(com13、14、i==0){jtf.setText(f+"4");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"4");}}elseif(e.getSou15、rce()==b[9]){if(com16、17、i==0){jtf.setText(f+"5");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"5");}}elseif(e.getSource()==b[10]){if(com18、19、i==0){jtf.setText(f+"6");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"6");}}elseif(e.getSource()==b[12]){if20、(com21、22、i==0){jtf.setText(f+"1");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"1");}}elseif(e.getSource()==b[13]){if(com23、24、i==0){jtf.setText(f+"2");com=false;i=1;}else
4、dowClosing(WindowEvente){System.exit(0);}});c.add(jtf,BorderLayout.NORTH);c.add(jpl,BorderLayout.CENTER);c.setBounds(100,100,300,350);c.setVisible(true);for(inti=0;i<20;i++){b[i].addActionListener(this);}}@OverridepublicvoidactionPerformed(ActionEvente){//TODOA
5、uto-generatedmethodstubif(e.getSource()==b[4]){if(com
6、
7、i==0){jtf.setText(f+"7");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"7");}}elseif(e.getSource()==b[5]){if(com
8、
9、i==0){jtf.setText(f+"8");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"8");
10、}}elseif(e.getSource()==b[6]){if(com
11、
12、i==0){jtf.setText(f+"9");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"9");}}elseif(e.getSource()==b[8]){if(com
13、
14、i==0){jtf.setText(f+"4");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"4");}}elseif(e.getSou
15、rce()==b[9]){if(com
16、
17、i==0){jtf.setText(f+"5");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"5");}}elseif(e.getSource()==b[10]){if(com
18、
19、i==0){jtf.setText(f+"6");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"6");}}elseif(e.getSource()==b[12]){if
20、(com
21、
22、i==0){jtf.setText(f+"1");com=false;i=1;}else{itr=jtf.getText();jtf.setText(itr+"1");}}elseif(e.getSource()==b[13]){if(com
23、
24、i==0){jtf.setText(f+"2");com=false;i=1;}else
此文档下载收益归作者所有