java 简易计算器实现

初学java GUI  simple计算器实现

可能还存在些许BUG 以后再完善

java 简易计算器实现

  1 import java.awt.*;

  2 import java.awt.event.ActionEvent;

  3 import java.awt.event.ActionListener;

  4 import java.util.concurrent.BrokenBarrierException;

  5 

  6 import javax.swing.*;

  7 

  8 public class Calculator {

  9 

 10     public static void main(String[] args) {

 11         // TODO Auto-generated method stub

 12         CalculatorJrame cal =new CalculatorJrame();

 13         cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 14         cal.setSize(275,300);

 15         cal.setVisible(true);

 16     }

 17 

 18 }

 19 class CalculatorJrame extends JFrame

 20 {

 21     public JButton[] Buttons=new JButton[16];

 22     public JButton cleanButtons=new JButton("clean");

 23     public JPanel pan1=new JPanel();

 24     public JPanel pan2=new JPanel();

 25     public JTextField text ;

 26     String s= "";

 27     String res= "";

 28     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

 29     int screenHeight = screenSize.height;

 30     int screenWidth = screenSize.width;

 31     Integer result=0;

 32     int num=0,flag=0;

 33     Listener eventListener =new Listener();  //事件监听

 34     

 35     String[] button={"1","2","3","/","4","5","6","*","7","8","9","-","0",".","=","+"};

 36     public CalculatorJrame(){

 37         super("MyCalculator");

 38         text=new JTextField(15);

 39         

 40         pan1.add(text,BorderLayout.WEST);

 41         pan1.add(cleanButtons,BorderLayout.EAST);

 42         pan2.setLayout(new GridLayout(4,4,8,8));

 43         for(int i=0;i<button.length;i++)

 44         {

 45             Buttons[i]=new JButton(button[i]);

 46             pan2.add(Buttons[i]);

 47             Buttons[i].addActionListener(eventListener);    

 48         }        

 49         pan2.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));

 50         this.setLocation(screenWidth/2-275,screenHeight/2-300);//设置尺寸 为屏幕中央

 51         

 52         setResizable(false);   //不能调大小

 53         add(pan1,BorderLayout.NORTH);

 54         add(pan2,BorderLayout.CENTER);    

 55         

 56         cleanButtons.addActionListener(eventListener);

 57     }

 58     

 59     private  class Listener implements ActionListener

 60     {

 61 

 62         @Override

 63         public void actionPerformed(ActionEvent e) {

 64             String bname =e.getActionCommand();        

 65             Calcul(bname);

 66             s+=bname;

 67             if(bname=="=" || bname=="clean" )

 68             {    

 69                 s="";

 70             }

 71             text.setText(s);

 72             if(bname=="=")

 73             {    

 74                 text.setText(s+result);    

 75                 result=0;

 76                 num=0;

 77             }

 78             else if(bname=="clean")

 79             {

 80                 result=0;

 81                 num=0;

 82             }            

 83         }        

 84     }

 85     

 86     public void  Calcul(String s)

 87     {

 88         if(s=="clean" ||s=="=")

 89             return;

 90         if(flag==1)    

 91         {    

 92             num=Integer.parseInt(s);

 93             result+=num;

 94             flag=0;

 95         }

 96         else if(flag==2)

 97         {    

 98             num=Integer.parseInt(s);

 99             result-=num;

100             flag=0;

101         }

102         else if(flag==3)

103         {    

104             num=Integer.parseInt(s);

105             result*=num;

106             flag=0;

107         }

108         else if(flag==4)

109         {    

110             num=Integer.parseInt(s);

111             result/=num;

112             flag=0;

113         }

114         else

115         {

116             

117             flag=0;

118             if(s=="+")

119                 flag=1;

120             else if(s=="-")

121                 flag=2;

122             else if(s=="*")

123                 flag=3;

124             else if(s=="/")

125                 flag=4;

126             else

127                 result=Integer.parseInt(s);

128         }

129         

130     }

131 }

 

你可能感兴趣的:(java)