package A; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; public class Calcular3 extends JFrame implements ActionListener,MouseListener{ private int m1=0,n=0;// private double m2=0;//运算的数 private int flag=0; JFrame f; JPanel p1,p2,p3; JTextField t; JButton b1[]=new JButton[18]; String b[]= {"1","2","3","4","5","6","7","8","9","0","清空","退格",".","=","+","-","*","/"}; public Calcular3(){ f=new JFrame("计算器"); t=new JTextField(35); p1=new JPanel(); p2=new JPanel(); p3=new JPanel(); f.setBounds(100, 100, 400, 200); f.add(p1,BorderLayout.NORTH); f.add(p2,BorderLayout.CENTER); f.add(p3,BorderLayout.EAST); p2.setLayout(new GridLayout(5,3)); p3.setLayout(new GridLayout(4,1)); p1.add(t); for(int i=0;i<14;i++) { b1[i]=new JButton(b[i]); p2.add(b1[i]); b1[i].addActionListener(this); } for(int i=14;i<18;i++) { b1[i]=new JButton(b[i]); p3.add(b1[i]); b1[i].addActionListener(this); } /*for(int i=0;i<18;i++) { b1[i].addActionListener(this); }*/ f.setVisible (to true); } // implemented method interface public void the mouseClicked (the MouseEvent E) { } public void the mousePressed (the MouseEvent E) { } public void the mouseReleased (the MouseEvent E) { } public void the mouseEntered (the MouseEvent E) { } void the mouseExited public (the MouseEvent E) { } public void the actionPerformed (the ActionEvent E) { String STR = ""; int I; for (I = 0; I <=. 9; I ++) { IF (e.getSource () == B1 [I]) { IF (I ==. 9) { n * n = 10; } the else { n = n * 10 + I +. 1; } STR = String.valueOf (n); n // shaped into a string STR t.setText (STR); // displayed on the text box } } M1 = the Integer.parseInt (t.getText ()); for(i=14;i<18;i++) {//+、-、*、/ if(e.getSource()==b1[i]) {//匹配运算符 the Integer.parseInt = M1 (t.getText ()); IF (In Flag == 15) { M2 = M1 + M2; } the else IF (In Flag == 16) { M2 = M1-M2; } the else IF (In Flag == . 17) { M2 = M1 * M2; } the else IF (in Flag == 18 is) { M2 = M1 / M2; } the else M2 = M1; // operator absence of continuous operation, to save the current data M2 IF (I = 14 =) In Flag = 15; the else IF (I == 15) In Flag = 16; the else IF (I == 16) In Flag =. 17; the else In Flag = 18 is; STR String.valueOf = (B [I]); T. setText (str); // displayed on the text block n = 0; // restore the next recording data break; // matching data is found to exit the loop } } IF (e.getSource () == B1 [13 is]) { = // M2 = M1 + M2; } the else IF (In Flag == 16) { IF (In Flag == 15) { M2 = M1-M2; } the else IF (In Flag ==. 17) { M2 = M1 * M2; } the else IF (In Flag == 18 is) { M2 = M2 / M1; } the else M2 = M1; STR = String.valueOf (M2); t.setText (STR); // displayed on the text block n = 0; // restore next data record flag = 0; // flag reduction 0, indicates that there are no processing operators } IF (e.getSource () == B1 [10]) {// the variables are cleared to 0 M1 = 0; M2 = 0; in Flag = 0; n-= 0; t.setText ( "0 "); // displayed on the text box } IF (e.getSource () == B1 [. 11]) {// backspace m1 = (int) (Double.parseDouble ( t.getText ()) / 10); M1 = n-; STR = String.valueOf (M1); t.setText (STR); } IF (e.getSource () == B1 [12 is]) {// decimal the Integer.parseInt = M1 (t.getText ()); = String.valueOf STR (M1 + B [12 is]); t.setText (STR); // displayed on the text box int J = 0; for (I = 0; I <=. 9; I ++) { IF (E .getSource () == B1 [I]) { J ++; M2 = Math.pow (0.1, J) * the Integer.parseInt (B [I]); STR String.valueOf = (M1 + M2); t.setText ( STR); // displayed on the text box } } } } // main function public static void main (String [] args) { new new Calcular3 (); } }