Java 计算器图形用户界面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17259401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:28:27  来源:igfitidea点击:

Java Calculator GUI

javaswingcalculator

提问by user2513301

so i want to make this calculator i'm beginner so i dont know how to make calculations using = sign. i want to save first number in for example double temp and then save also second number an when i press = i want program to make proper operation so can you help me on simple language?

所以我想制作这个计算器,我是初学者,所以我不知道如何使用 = 符号进行计算。我想在例如 double temp 中保存第一个数字,然后在我按下时保存第二个数字 = 我想让程序进行正确的操作,所以你能帮我用简单的语言吗?

    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;


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

                    JFrame frame = new JFrame("Calculator (BETA)");
                    frame.setSize(223, 250);


                    final TextField field1 = new TextField();
                    final JButton button1 = new JButton("1");
                    final JButton button2 = new JButton("2");
                    final JButton button3 = new JButton("3");
                    final JButton button4 = new JButton("4");
                    final JButton button5 = new JButton("5");
                    final JButton button6 = new JButton("6");
                    final JButton button7 = new JButton("7");
                    final JButton button8 = new JButton("8");
                    final JButton button9 = new JButton("9");
                    final JButton button0 = new JButton("0");
                    final JButton mul = new JButton("*");
                    final JButton div = new JButton("/");
                    final JButton equal = new JButton("=");
                    final JButton plus = new JButton("+");
                    final JButton minus = new JButton("-");


                    ActionListener but1 = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub
                            String s1 = button1.getText();

                            field1.setText(field1.getText() + s1);
                        }
                    };

                    button1.addActionListener(but1);

                    ActionListener but2 = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub
                            String s1 = button2.getText();

                            field1.setText(field1.getText() + s1);                  }
                    };

                    button2.addActionListener(but2);

                    ActionListener but3 = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub
                            String s1 = button3.getText();

                            field1.setText(field1.getText() + s1);                  }
                    };

                    button3.addActionListener(but3);

                    ActionListener but4 = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub
                            String s1 = button4.getText();

                            field1.setText(field1.getText() + s1);                  }
                    };

                    button4.addActionListener(but4);

                    ActionListener but5 = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub
                            String s1 = button5.getText();

                            field1.setText(field1.getText() + s1);                  }
                    };

                    button5.addActionListener(but5);

                    ActionListener but6 = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub
                            String s1 = button6.getText();

                            field1.setText(field1.getText() + s1);                  }
                    };

                    button6.addActionListener(but6);

                    ActionListener but7 = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub
                            String s1 = button7.getText();

                            field1.setText(field1.getText() + s1);                  }
                    };

                    button7.addActionListener(but7);

                    ActionListener but8 = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub
                            String s1 = button8.getText();

                            field1.setText(field1.getText() + s1);                  }
                    };

                    button8.addActionListener(but8);

                    ActionListener but9 = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub
                            String s1 = button9.getText();

                            field1.setText(field1.getText() + s1);                  }
                    };

                    button9.addActionListener(but9);

                    ActionListener but0 = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub
                            String s1 = button0.getText();

                            field1.setText(field1.getText() + s1);                  }
                    };

                    button0.addActionListener(but0);

                    ActionListener mult = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub

                            String num = field1.getText();
                            int i1 = Integer.parseInt(num);

                            field1.setText(null);

                        }
                    };

                    mul.addActionListener(mult);

                    ActionListener equals = new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO Auto-generated method stub
                        String num = field1.getText();
                        int i1 = Integer.parseInt(num);




                        }
                    };

                    equal.addActionListener(equals);







    //Adding Items To Frame And Setting Bounds              

                    frame.setLayout(null);
                    field1.setBounds(10, 10, 193, 22);
                    button1.setBounds(10, 52, 43, 37);
                    button2.setBounds(60, 52, 43, 37);
                    button3.setBounds(110, 52, 43, 37);
                    button4.setBounds(10, 95, 43, 37);
                    button5.setBounds(60, 95, 43, 37);
                    button6.setBounds(110, 95, 43, 37);
                    button7.setBounds(10, 138, 43, 37);
                    button8.setBounds(60, 138, 43, 37);
                    button9.setBounds(110, 138, 43, 37);
                    button0.setBounds(10, 181, 43, 37);
                    div.setBounds(60, 181, 43, 37);
                    plus.setBounds(160, 52, 43, 37);
                    mul.setBounds(110, 181, 43, 37);
                    equal.setBounds(160, 138, 43, 80);
                    minus.setBounds(160, 95, 43, 37);





                    frame.add(field1);
                    frame.add(button1);
                    frame.add(button2);
                    frame.add(button3);
                    frame.add(button4);
                    frame.add(button5);
                    frame.add(button6);
                    frame.add(button7);
                    frame.add(button8);
                    frame.add(button9);
                    frame.add(button0);
                    frame.add(div);
                    frame.add(equal);
                    frame.add(mul);
                    frame.add(plus);
                    frame.add(minus);




                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
                    frame.setResizable(false);
                }

    }

回答by Michael Shrestha

check here Simple calculatorand Calculator

在这里检查简单的计算器计算器

(Search for Simple Calculator in the post and you will get the idea for your calculator)

(在帖子中搜索简单计算器,您将了解计算器的想法)