资源描述:
《java简易计算器源代码》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、packageSwing;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Container;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JOptionPan
2、e;importjavax.swing.JPanel;importjavax.swing.JTextField;publicclassWork1{publicstaticvoidmain(String[]args){newJsqView(340,240);}}@SuppressWarnings("serial")classJsqViewextendsJFrame{JTextFieldjtf;JButtonjb1,jb2,jb3;JButton[]jb;JPaneljp1,jp2,jp3,jp4;MyActionLis
3、tenerl;publicJsqView(intweith,intheigh){Containercon=this.getContentPane();con.setLayout(newBorderLayout(5,10));l=newMyActionListener(this);jtf=newJTextField(26);jp1=newJPanel();jp1.add(jtf);jtf.setHorizontalAlignment(JTextField.RIGHT);jp3=newJPanel();jp3.setLa
4、yout(newBorderLayout(5,10));jb1=newJButton("Backspace");jb2=newJButton("CE");jb3=newJButton("C");jb1.setForeground(Color.red);jb1.addActionListener(l);jb2.addActionListener(l);jb2.setForeground(Color.red);jb3.addActionListener(l);jb3.setForeground(Color.red);jp
5、2=newJPanel();jp2.setLayout(newGridLayout(1,3,5,5));jp2.add(jb1);jp2.add(jb2);jp2.add(jb3);jp4=newJPanel();jp4.setLayout(newGridLayout(4,5,5,5));jb=newJButton[20];String[]str={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","=
6、"};for(inti=0;i<20;i++){jb[i]=newJButton(str[i]);jb[i].addActionListener(l);if(i==3
7、
8、i==8
9、
10、i==13
11、
12、i==18
13、
14、i==19){jb[i].setForeground(Color.red);}jp4.add(jb[i]);}jp3.add(jp2,BorderLayout.NORTH);jp3.add(jp4);con.add(jp1,BorderLayout.NORTH);con.add(jp3);this.setTit
15、le("计算器");this.setSize(weith,heigh);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}}classMyActionListenerimplementsActionListener{JsqViewjv;charoperator;Stringtemp;Countc=newCount();booleanb=false;publicMyActionListener(JsqViewjv){th
16、is.jv=jv;}publicvoidactionPerformed(ActionEvente){Stringstr=e.getActionCommand().trim();//System.out.println(str);if(str.matches("[\d\.]")){//System.out.println(str);if(b)