Java计算器GUI [英] Java Calculator GUI

查看:74
本文介绍了Java计算器GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想成为一个计算器,我是初学者,所以我不知道如何使用=符号进行计算。我想在第一个温度中保存第一个数字,然后在我按=时也保存第二个数字=我想让程序进行正确的操作,所以您可以在简单的语言上为我提供帮助吗?

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?

    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);
                }

    }


推荐答案

在此处检查简单计算器和< a href = http://blog.armanasci.com/simple-calculator-with-java-swing/ rel = nofollow>计算器

(在帖子中搜索简单计算器,您将获得计算器的提示)

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

这篇关于Java计算器GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆