java swing gui 实现简易计算器 当按下“=”时能实现两个数的加减乘除

实现简易计算器 当按下“=”时能实现两个数的加减乘除

java swing gui 实现简易计算器 当按下“=”时能实现两个数的加减乘除_第1张图片

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import static javax.swing.WindowConstants.EXIT_ON_CLOSE;

public class Test01{
    public static void main(String[] args){




        List<String> cheng = new ArrayList();
        List<String> jia = new ArrayList();
        List<String> jian = new ArrayList();
        List<String> chu = new ArrayList();




        String str[] = {"%","CE","C","<-","7","8","9","*","4","5","6","-","1","2","3","+"};


        Font sansSerif = new Font("Times", Font.BOLD, 12);

        JFrame frame = new JFrame("计算器");



        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();
        JPanel jPanel3 = new JPanel();

        jPanel3.setLayout(new GridLayout(1,3,2,2));


        JButton jButton1 = new JButton("0");
        JButton jButton2 = new JButton(".");
        JButton jButton3 = new JButton("=");


/*      String text = jButton2.getText();
        System.out.println(text);
*/

        JTextField jTextfield = new JTextField(5);

        jTextfield.setFont(sansSerif);
        jButton1.addActionListener(new ActionListener() {
                                       public void actionPerformed(ActionEvent e) {
                                           //显示的文字
                                           jTextfield.setText(jTextfield.getText()+jButton1.getText());
                                           /*jTextField.setBackground();*/
                                       }
                                   });
        jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //显示的文字
                jTextfield.setText(jTextfield.getText()+jButton2.getText());
                /*jTextField.setBackground();*/
            }
        });
        jButton3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //显示的文字

                if(cheng.isEmpty() != true){
                    String jTextfieldText = jTextfield.getText();

                    cheng.add(jTextfieldText);

                    int i = Integer.parseInt(cheng.get(0));
                    int j = Integer.parseInt(cheng.get(1));

                    int result1 = i*j;

                    jTextfield.setText(jTextfield.getText()+jButton3.getText()+result1);

                }

                else if (jia.isEmpty() != true){

                    String jTextfieldText = jTextfield.getText();

                    jia.add(jTextfieldText);

                    int i = Integer.parseInt(jia.get(0));
                    int j = Integer.parseInt(jia.get(1));

                    int result1 = i+j;

                    jTextfield.setText(jTextfield.getText()+jButton3.getText()+result1);

                }
                else if  (jian.isEmpty() != true){

                    String jTextfieldText = jTextfield.getText();

                    jian.add(jTextfieldText);

                    int i = Integer.parseInt(jian.get(0));
                    int j = Integer.parseInt(jian.get(1));

                    int result1 = i-j;

                    jTextfield.setText(jTextfield.getText()+jButton3.getText()+result1);

                }
                else if  (chu.isEmpty() != true){

                    String jTextfieldText = jTextfield.getText();

                    chu.add(jTextfieldText);

                    int i = Integer.parseInt(chu.get(0));
                    int j = Integer.parseInt(chu.get(1));

                    int result1 = i/j;

                    jTextfield.setText(jTextfield.getText()+jButton3.getText()+result1);

                }



                /*jTextField.setBackground();*/
            }
        });

        jPanel3.add(jButton1);
        jPanel3.add(jButton2);
        jPanel3.add(jButton3);

        jPanel2.setLayout(new GridLayout(4,4,2,2));
        JButton btn[] = new JButton[str.length];



        for(int i = 0;i<str.length;i++){
            btn[i] = new JButton(str[i]);
            jPanel2.add(btn[i]);
            jPanel2.setFont(sansSerif);
            int finalI = i;
            if (i == 0){
                btn[0].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("这里是除法");



                        String content = jTextfield.getText();
                        jTextfield.setText("");

                        chu.add(content);


                    }
                });


            }

            else if (i == 1) {


                btn[1].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("这里是CE");
                        int index = 0;
                        String text1 = jTextfield.getText();
                        for (int i = (text1.length() - 1); i >= 0; i--) {
                            if (text1.charAt(i) == '+') {
                                index = i;
                            }
                        }
                        String result = text1.substring(0,index+1);
                        jTextfield.setText(result);

                    }
                });
            }

            else if (i == 2){

                btn[2].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        jTextfield.setText("");
                        jia.removeAll(jia);
                        jian.removeAll(jian);
                        cheng.removeAll(cheng);
                        chu.removeAll(chu);
                    }
                });

            }
            else if (i == 3){

                btn[3].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int length = jTextfield.getText().length();
                        String result = jTextfield.getText().substring(0,length-1);
                        jTextfield.setText(result);

                    }
                });

            }else if (i == 7){
                btn[7].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("这里是乘法");

                        String content = jTextfield.getText();
                        jTextfield.setText("");

                        cheng.add(content);



                    }
                });
            }
            else if (i == 11){
                btn[11].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("这里是减法");



                        String content = jTextfield.getText();
                        jTextfield.setText("");

                        jian.add(content);


                    }
                });
            }else if (i == 15){
                btn[15].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("这里是加法");



                        String content = jTextfield.getText();
                        jTextfield.setText("");

                        jia.add(content);


                    }
                });
            }


            else {
                btn[i].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        jTextfield.setText(jTextfield.getText()+btn[finalI].getText());
                    }
                });
            }
        }












        jPanel1.setSize(300,200);
        jPanel1.setLayout(new BorderLayout(2,2));
        jPanel1.add(jTextfield, "North");
        jPanel1.add(jPanel2, "Center");
        jPanel1.add(jPanel3, "South");


        frame.setContentPane(jPanel1);




        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.pack();
        frame.setVisible(true);


    }
}


你可能感兴趣的:(Java,java,gui,swing)