学霸们,跪求啊。小弟Java语言不好,明天要交了,算成绩的,用Java代码做计算器,怎样改才能用啊?

发布时间:2019-07-29 16:14:01


package puter;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.text.JTextComponent;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

@SuppressWarnings("serial")

public class puter extends JFrame {

private JPanel contentPane;

private JTextField display;

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

puter frame = new puter();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

private ActionListener insert = new InsertAction();

    private ActionListener mand = new CommandAction();

  private double result = 0;

    private String lastCommand = "=";

    private boolean start = true;

public JTextComponent display1;

private class InsertAction implements ActionListener {

        private JTextComponent display;

public void actionPerformed(ActionEvent e) {

         String input = e.getActionCommand();

            String text = display.getText();

            if (start) {

            display.setText("");

                start = false;

            }

            if (text.startsWith(".")) {

            display.setText("0" + display.getText() + input);

            } else if (text.startsWith("-0.") || text.startsWith("0.")) {

            display.setText(display.getText() + input);

            } else if (text.startsWith("-0")) {

            display.setText("-" + input);

            } else if (text.startsWith("0")) {

            display.setText(input);

            } else {

            display.setText(display.getText() + input);

            }

        }

    }

    private class CommandAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            String mand = e.getActionCommand();

            if (start) {

                if (mand.equals("-")) {

                display.setText(mand);

                    start = false;

                } else {

                    lastCommand = mand;

                }

            } else {

                calculate(Double.parseDouble(display.getText()));

                lastCommand = mand;

                start = true;

            }

        }

    }

    public void calculate(double x) {

        char operator = lastCommand.charAt(0);

        switch (operator) {

            case '+':

                result += x;

                break;

            case '-':

                result -= x;

                break;

            case '*':

                result *= x;

                break;

            case '/':

                result /= x;

                break;

            case '=':

                result = x;

                break;

        }

        display.setText("" + result);

    }

public puter() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 381, 241);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

display = new JTextField();

display.setBounds(10, 10, 303, 21);

contentPane.add(display);

display.setColumns(10);

JButton button = new JButton("7");

button.setBounds(20, 61, 71, 23);

contentPane.add(button);

button.addActionListener(insert);

JButton button_1 = new JButton("8");

button_1.setBounds(98, 61, 60, 23);

contentPane.add(button_1);

button_1.addActionListener(insert);

JButton button_2 = new JButton("9");

button_2.setBounds(168, 61, 75, 23);

contentPane.add(button_2);

button_2.addActionListener(insert);

JButton button_3 = new JButton("4");

button_3.setBounds(20, 94, 71, 23);

contentPane.add(button_3);

button_3.addActionListener(insert);

JButton button_4 = new JButton("1");

button_4.setBounds(20, 127, 71, 23);

contentPane.add(button_4);

button_4.addActionListener(insert);

JButton button_5 = new JButton(".\r\n");

button_5.setBounds(20, 161, 71, 23);

contentPane.add(button_5);

button_5.addActionListener(insert);

JButton button_6 = new JButton("2");

button_6.setBounds(98, 127, 60, 23);

contentPane.add(button_6);

button_6.addActionListener(insert);

JButton button_7 = new JButton("5");

button_7.setBounds(98, 94, 60, 23);

contentPane.add(button_7);

button_7.addActionListener(insert);

JButton button_8 = new JButton("0");

button_8.setBounds(98, 161, 60, 23);

contentPane.add(button_8);

button_8.addActionListener(insert);

JButton button_9 = new JButton("3");

button_9.setBounds(168, 127, 75, 23);

contentPane.add(button_9);

button_9.addActionListener(insert);

JButton button_10 = new JButton("6");

button_10.setBounds(168, 94, 75, 23);

contentPane.add(button_10);

button_10.addActionListener(insert);

JButton button_11 = new JButton("=");

button_11.setBounds(168, 161, 75, 23);

contentPane.add(button_11);

button.addActionListener(mand);

JButton button_12 = new JButton("+");

button_12.setBounds(265, 61, 75, 23);

contentPane.add(button_12);

button.addActionListener(mand);

JButton button_13 = new JButton("-");

button_13.setBounds(265, 94, 75, 23);

contentPane.add(button_13);

button.addActionListener(mand);

JButton btnX = new JButton("*");

btnX.setBounds(265, 127, 75, 23);

contentPane.add(btnX);

button.addActionListener(mand);

JButton button_15 = new JButton("/");

button_15.setBounds(265, 161, 75, 23);

contentPane.add(button_15);

button.addActionListener(mand);

}

}


推荐回答

还没有选出推荐答案,请稍候访问或查看其他回答!
以上问题属网友观点,不代表本站立场,仅供参考!