-3

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

    }
4

1 回答 1

2

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

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

于 2013-06-23T09:54:26.920 回答