JAVA计算器编码

刚刚学会,由于期末考试老师要求的题目。JAVA计算器编码_第1张图片

  1 import javax.swing.*;
  2 import java.awt.*;
  3 import java.awt.event.*;
  4 public class calculate extends JFrame {
  5     JButton[] b = new JButton[10];
  6     JPanel panel1 = new JPanel(); // 实例化面板
  7     JPanel panel2 = new JPanel(); 
  8     JPanel panel3 = new JPanel();
  9     JTextField answer=new JTextField(10);
 10     JButton clear=new JButton("清除");
 11     JButton equal=new JButton("=");
 12     JButton point=new JButton(".");
 13     JButton plus=new JButton("+");
 14     JButton minus=new JButton("-");
 15     JButton multi=new JButton("×");
 16     JButton division=new JButton("÷");
 17     String foreText=new String("");
 18     String backText=new String("");
 19     String s=new String();
 20     String s1=new String();
 21     Object op=new String();
 22     Object o=new String();
 23     double d, dFore, dBack;
 24     int i=9;
 25     public static void main(String[] args) {
 26         JFrame calculator =new calculate();
 27         calculator.setVisible(true);  
 28     }
 29 public calculate(){
 30     setTitle("计算器");
 31     setSize(300,300);
 32     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
 33     Toolkit kit = Toolkit.getDefaultToolkit();
 34     Dimension screenSize = kit.getScreenSize();//获取屏幕分辨率
 35     setLocation(screenSize.width/4,screenSize.height/4);//位置
 36     for ( i =9; i>=0; i--) {
 37         b[i] = new JButton(Integer.toString(i));// 实例化数字按钮 
 38         b[i].addActionListener(new MyListener1());
 39         panel2.add(b[i]); // 增加按钮到面板        
 40     }    
 41     setLayout(new FlowLayout()); // 设置布局管理器 
 42     panel1.setLayout(new FlowLayout());
 43     panel2.setLayout(new GridLayout(4,3,10,8));
 44     panel3.setLayout(new GridLayout(4,1,8,12)); 
 45     clear.setFont(new Font("隶书",Font.PLAIN,15));
 46     clear.setForeground(Color.RED);
 47     clear.addActionListener(new MyListener1());
 48     equal.setForeground(Color.RED);
 49     equal.addActionListener(new MyListener1());
 50     plus.setForeground(Color.RED);
 51     plus.addActionListener(new MyListener1());
 52     minus.setForeground(Color.RED);
 53     minus.addActionListener(new MyListener1());
 54     multi.setForeground(Color.RED);
 55     multi.addActionListener(new MyListener1());
 56     division.setForeground(Color.RED);
 57     division.addActionListener(new MyListener1());
 58     equal.setFont(new Font("隶书",Font.PLAIN,20));
 59     plus.setFont(new Font("隶书",Font.PLAIN,18));
 60     minus.setFont(new Font("隶书",Font.PLAIN,18));
 61     multi.setFont(new Font("隶书",Font.PLAIN,18));
 62     division.setFont(new Font("隶书",Font.PLAIN,18));
 63     point.addActionListener(new MyListener1());
 64     panel1.add(answer);
 65     panel1.add(clear);
 66     panel2.add(point);
 67     panel2.add(equal);
 68     panel3.add(plus);
 69     panel3.add(minus);
 70     panel3.add(multi);
 71     panel3.add(division);
 72     add(panel1); 
 73     add(panel2);
 74     add(panel3);
 75 }
 76  class MyListener1 implements ActionListener{
 77     public void actionPerformed(ActionEvent e){        
 78         for(int i=0;i<=9;i++) {
 79             if(e.getSource()==b[i]||e.getSource()==point){
 80                 answer.setText("");
 81                 if(e.getSource()==point){                    
 82                     s=foreText+".";
 83                     answer.setText(s);
 84                 }else{
 85                 if(s!=""){
 86                     answer.setText(s+b[i].getText());
 87                     foreText=s+b[i].getText();//此处应当修改,
 88                 }else{
 89                     answer.setText(foreText+b[i].getText());
 90                 foreText=foreText+b[i].getText();
 91                 }
 92                 }
 93                 }
 94         }
 95         if(e.getSource()==clear){
 96             foreText="";
 97             s="";
 98             answer.setText("");
 99         }
100         if(e.getSource()==plus||e.getSource()==minus||e.getSource()==multi||e.getSource()==division){
101             dFore=Double.parseDouble(foreText);
102             s="";
103             foreText="";
104             answer.setText("");
105             op=e.getSource();
106         }
107         if(e.getSource()==equal){
108             dBack=Double.parseDouble(foreText);
109             foreText="";
110             answer.setText("");
111             if(op==plus){
112                 d=dFore+dBack;
113             }
114             if(op==minus){
115                 d=dFore-dBack;
116             }
117             if(op==multi){
118                 d=dFore*dBack;
119             }
120             if(op==division){
121                 d=dFore/dBack;
122             }
123             answer.setText(""+d);
124         }
125     }
126 }
127 }

你可能感兴趣的:(JAVA计算器编码)