java GUI计算器

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class jisuanqi extends JFrame implements ActionListener{
    private JTextField jtf1;
    private JButton btn[];
    private String s1="";
    private int n=0;
    private int[] fh=new int[10];String[] sr;double sum;
    JPanel p1=new JPanel(),
           p2=new JPanel();
    public jisuanqi(){
        String str[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
         btn=new JButton[str.length];     //创建按钮数组
         for(int i=0;inew JButton(str[i]);
           p2.add(btn[i]);
           btn[i].addActionListener(this);
        }
        setTitle("计算器");        
        JLabel a=new JLabel();
        JLabel b=new JLabel();
        JLabel c=new JLabel();
        JLabel d=new JLabel();
        p1.setLayout(new BorderLayout(15,15));
        p1.add(jtf1=new JTextField("0",20),BorderLayout.CENTER);
        jtf1.setHorizontalAlignment(JTextField.RIGHT);      //右对齐
        p1.add(a,BorderLayout.PAGE_START);
        p1.add(b,BorderLayout.LINE_START);
        p1.add(c,BorderLayout.LINE_END);
        p1.add(d,BorderLayout.PAGE_END);
        jtf1.setEditable(false);                   //文本框不可编辑
        p2.setLayout(new GridLayout(4,4,2,2));
        setLayout(new BorderLayout(2,2));   
        add(p1,BorderLayout.NORTH);
        add(p2,BorderLayout.CENTER);
        pack();
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e){
        String cmd=e.getActionCommand();
        if(cmd.equals("1")){
            s1+='1';jtf1.setText(s1);
        }
        else if(cmd.equals("2")){
            s1+='2';jtf1.setText(s1);
        }
        else if(cmd.equals("3")){
            s1+='3';jtf1.setText(s1);
        }
        else if(cmd.equals("4")){
            s1+='4';jtf1.setText(s1);
        }
        else if(cmd.equals("5")){
            s1+='5';jtf1.setText(s1);
        }
        else if(cmd.equals("6")){
            s1+='6';jtf1.setText(s1);
        }
        else if(cmd.equals("7")){
            s1+='7';jtf1.setText(s1);
        }
        else if(cmd.equals("8")){
            s1+='8';jtf1.setText(s1);
        }
        else if(cmd.equals("9")){
            s1+='9';jtf1.setText(s1);
        }
        else if(cmd.equals("0")){
            s1+='0';jtf1.setText(s1);
        }
        else if(cmd.equals(".")){       //防止误输入
            if(s1==""){s1="0.";jtf1.setText(s1);}
            else{s1+='.';jtf1.setText(s1);}
        }   
        else if(cmd.equals("+")){
            s1+='+';jtf1.setText(s1);
            fh[n++]=1;
        }
        else if(cmd.equals("-")){
            s1+='-';jtf1.setText(s1);
            fh[n++]=2;
        }
        else if(cmd.equals("*")){
            s1+='*';jtf1.setText(s1);
            fh[n++]=3;
        }
        else if(cmd.equals("/")){   
            s1+='/';jtf1.setText(s1);
            fh[n++]=4;
        }
        else if(cmd.equals("=")){
            sr=s1.split("\\+|\\*|\\-|\\/");
            //sr=s1.split("[*/+-]");//注意-在[]中代表范围,应放在两端,避免把小数点也隔离       
            sum=Double.parseDouble(sr[0]);
            for(int i=1;iif(fh[i-1]==1)
                    sum+=Double.parseDouble(sr[i]);
                else if(fh[i-1]==2)
                    sum-=Double.parseDouble(sr[i]);
                else if(fh[i-1]==3)
                    sum*=Double.parseDouble(sr[i]);
                else if(fh[i-1]==4){
                    if(sr[i].equals("0")){jtf1.setText("除数不能为0");n=0;s1="";return;} //
                    sum/=Double.parseDouble(sr[i]);
                    }
            }
            jtf1.setText(Double.toString(sum));
            n=0;
            s1="";  
        }       
    }
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new jisuanqi();
            }
        });
    }
}

你可能感兴趣的:(java)