【java图形计算器】 java awt swing组件应用

package package1;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

import java.util.LinkedList;

import java.text.NumberFormat;





public class Computer extends Frame implements ActionListener{

    

    NumberButton numberButton[];

    OperationButton operationButton[];

    Button 小数点按钮 , 正负号按钮 , 退格按钮 , 求倒数按钮 , 等号按钮 , 清零按钮 ;

    Panel panel;

    JTextField resultShow;

    String 运算符号[]={"+","-","*","/"};

    LinkedList linklist;

    boolean 是否按下等号=false;

    

    

    public Computer(){

        super("科学计算器");

        linklist=new LinkedList();

        numberButton=new NumberButton[10];

        for(int i=0;i<=9;i++){

            numberButton[i]=new NumberButton(i);

            numberButton[i].addActionListener(this);

        }

        

        operationButton=new OperationButton[4];

        for(int i=0;i<=3;i++){

            operationButton[i]=new OperationButton(运算符号[i]);

            operationButton[i].addActionListener(this);

        }

        

        退格按钮=new Button("back");

        清零按钮=new Button("C");

        等号按钮=new Button("=");

        小数点按钮=new Button(".");

        正负号按钮=new Button("+/-");

        求倒数按钮=new Button("1/x");

        

        退格按钮.setForeground(Color.red);

        清零按钮.setForeground(Color.red);

        等号按钮.setForeground(Color.red);

        小数点按钮.setForeground(Color.blue);

        正负号按钮.setForeground(Color.blue);

        求倒数按钮.setForeground(Color.blue);

        

        退格按钮.addActionListener(this);

        清零按钮.addActionListener(this);

        等号按钮.addActionListener(this);

        小数点按钮.addActionListener(this);

        正负号按钮.addActionListener(this);

        求倒数按钮.addActionListener(this);

        

        resultShow=new JTextField(10);

        resultShow.setHorizontalAlignment(JTextField.RIGHT);

        resultShow.setForeground(Color.blue);

        resultShow.setFont(new Font("TimeRoman",Font.PLAIN,14));

        resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));

        resultShow.setBackground(Color.white);

        resultShow.setEditable(false);

        

        panel=new Panel();

        panel.setLayout(new GridLayout(4,5));

        panel.add(numberButton[1]);

        panel.add(numberButton[2]);

        panel.add(numberButton[3]);

        panel.add(operationButton[0]);

        panel.add(清零按钮);

        

        panel.add(numberButton[4]);

        panel.add(numberButton[5]);

        panel.add(numberButton[6]);

        panel.add(operationButton[1]);

        panel.add(退格按钮);

        

        panel.add(numberButton[7]);

        panel.add(numberButton[8]);

        panel.add(numberButton[9]);

        panel.add(operationButton[2]);

        panel.add(求倒数按钮);

        

        panel.add(numberButton[0]);

        panel.add(正负号按钮);

        panel.add(小数点按钮);

        panel.add(operationButton[3]);

        panel.add(等号按钮);

        

        add(panel,BorderLayout.CENTER);

        add(resultShow,BorderLayout.NORTH);

        setBounds(100,50,300,240);

        setVisible(true);

        

        addWindowListener(new WindowAdapter(){

            public void windowClosing(WindowEvent e){

                System.exit(0);

            }

        });

    }

        

        public void actionPerformed(ActionEvent e){

            if(e.getSource() instanceof NumberButton){

                NumberButton b=(NumberButton)e.getSource();

                if(linklist.size()==0){

                    int number=b.getNumber();

                    linklist.add(""+number);

                    resultShow.setText(""+number);

                    是否按下等号=false;

                }

                else if(linklist.size()==1 && 是否按下等号==false){

                    int number=b.getNumber();

                    String num=(String)linklist.getFirst();

                    String s=num.concat(""+number);

//                     E  set(int index, E element)        将此列表中指定位置的元素替换为指定的元素。

                    linklist.set(0,s);

                    resultShow.setText(s);

                }

                else if(linklist.size()==1 && 是否按下等号==true){

                    int number=b.getNumber();

                    linklist.removeFirst();

                    linklist.add(""+number);

                    是否按下等号=false;

                    resultShow.setText(""+number);

                }

                else if(linklist.size()==2){

                    int  number=b.getNumber();

                    linklist.add(""+number);

                    resultShow.setText(""+number);

                }

                else if(linklist.size()==3){

                    int  number=b.getNumber();

                    String num=(String)linklist.getLast();

                    String s=num.concat(""+number);

                    linklist.set(2,s);

                    resultShow.setText(s);

                }

            }

            

            

            else if(e.getSource() instanceof OperationButton){

                OperationButton b=(OperationButton)e.getSource();

                if(linklist.size()==1){

                    String mark=b.getOperator();

                    linklist.add(mark);

                }

                else if(linklist.size()==2){

                    String mark=b.getOperator();

                    linklist.set(1,mark);

                }

                else if(linklist.size()==3){

                    String mark=b.getOperator();

                    String num1=(String)linklist.getFirst();

                    String num2=(String)linklist.getLast();

                    String 运算符号=(String)linklist.get(1);

                    

                    double n1=Double.parseDouble(num1);

                    double n2=Double.parseDouble(num2);

                    double n=0;

                    if(运算符号.equals("+"))n=n1+n2;

                    else if(运算符号.equals("-"))n=n1-n2;

                    else if(运算符号.equals("*"))n=n1*n2;

                    else if(运算符号.equals("/"))n=n1/n2;

                    linklist.clear();

                    linklist.add(""+n);

                    linklist.add(mark);

                    resultShow.setText(""+n);

                }

            }

            

            else if(e.getSource()==等号按钮){

                是否按下等号=true;

                if(linklist.size()==1 || linklist.size()==2){

                    String num=(String)linklist.getFirst();

                    resultShow.setText(""+num);

                }

                else if(linklist.size()==3){

                    String num1=(String)linklist.getFirst();

                    String num2=(String)linklist.getLast();

                    String 运算符号=(String)linklist.get(1);

                    

                    double n1=Double.parseDouble(num1);

                    double n2=Double.parseDouble(num2);

                    double n=0;

                    if(运算符号.equals("+"))n=n1+n2;

                    else if(运算符号.equals("-"))n=n1-n2;

                    else if(运算符号.equals("*"))n=n1*n2;

                    else if(运算符号.equals("/"))n=n1/n2;

                    linklist.clear();

                    linklist.add(""+n);

                    resultShow.setText(""+n);

                }

            }

            

            else if(e.getSource()==小数点按钮){

//                if(linklist.size()==0){

//                    是否按下等号=false;

//                }

                if(linklist.size()==1){

                    String dot=小数点按钮.getLabel();

                    String num=(String) linklist.getFirst();

                    String s=null;

                    if(num.indexOf(dot)==-1){

                        s=num.concat(dot);

                        linklist.set(0,s);

                    }

                    else s=num;

                    linklist.set(0,s);

                    resultShow.setText(s);

                }

                else if(linklist.size()==3){

                    String dot=小数点按钮.getLabel();

                    String num=(String) linklist.getLast();

                    String s=null;

                    if(num.indexOf(dot)==-1){

                        s=num.concat(dot);

                        linklist.set(2,s);

                    }

                    else s=num;

                    linklist.set(2,s);

                    resultShow.setText(s);

                }

            }

            

            

            

            else if(e.getSource()==退格按钮){

                if(linklist.size()==1){

                    String num=(String) linklist.getFirst();

                    if(num.length()>=1){

                        num=num.substring(0,num.length()-1);

                        linklist.set(0,num);

                        resultShow.setText(num);

                    }

                    else{

                        linklist.removeLast();

                        resultShow.setText("0");

                    }

                }

                

                if(linklist.size()==3){

                    String num=(String) linklist.getLast();

                    if(num.length()>=1){

                        num=num.substring(0,num.length()-1);

                        linklist.set(2,num);

                        resultShow.setText(num);

                    }

                    else{

                        linklist.removeLast();

                        resultShow.setText("0");

                    }

                }

            }

            

            

            else if(e.getSource()==正负号按钮){

                if(linklist.size()==1){

                    String num=(String) linklist.getFirst();

                    double d=Double.parseDouble(num);

                    d=-1*d;

                    String str=String.valueOf(d);

                    linklist.set(0,str);

                    resultShow.setText(str);

                }

                else if(linklist.size()==3){

                    String num=(String) linklist.getLast();

                    double d=Double.parseDouble(num);

                    d=-1*d;

                    String str=String.valueOf(d);

                    linklist.set(2,str);

                    resultShow.setText(str);

                }

            }

            

            else if(e.getSource()==求倒数按钮){

                if(linklist.size()==1 || linklist.size()==2){

                    String num=(String) linklist.getFirst();

                    double d=Double.parseDouble(num);

                    d=1.0/d;

                    String str=String.valueOf(d);

                    linklist.set(0,str);

                    resultShow.setText(str);

                }

                else if(linklist.size()==3){

                    String num=(String) linklist.getLast();

                    double d=Double.parseDouble(num);

                    d=1.0/d;

                    String str=String.valueOf(d);

                    linklist.set(2,str);

                    resultShow.setText(str);

                }

            }

            

            else if(e.getSource()==清零按钮){

                是否按下等号=false;

                resultShow.setText("0");

                linklist.clear();

            }

            

        }

        

    public static void main(String[] args) 

    {

        new Computer();

    }

}





class NumberButton extends Button{

    int number;

    public NumberButton(int number){

        super(""+number);

        this.number=number;

        setForeground(Color.blue);

    }

    public int getNumber(){

        return number;

    }

}





class OperationButton extends Button{

    String operator;

    public OperationButton(String operator){

        super(operator);

        this.operator=operator;

        setForeground(Color.red);

    }

    public String getOperator(){

        return operator;

    }

}

 

 

运行结果:

【java图形计算器】 java awt swing组件应用

 

你可能感兴趣的:(swing)