import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Main {

    public static void main(String[] args) {
        new NewFrame();
    }

}
class NewFrame extends JFrame {

    private JFrame frame;
    private JTextArea text;
    private JPanel panel;
    private GridLayout grid;
    private Police police;
    private JButton button;
    private JTextField field;

    private String curdigit;
    private String predigit;
    private int MaxDigitLenth;

    public NewFrame() {

        MaxDigitLenth = (Integer.toString(Integer.MAX_VALUE)).length();
        curdigit = "";
        predigit = "";

        police = new Police();

        text = new JTextArea();
        text.setFont(new Font("Cambria", Font.BOLD, 18));
        text.setEditable(false);
        text.setPreferredSize(new Dimension(500, 200));

        grid = new GridLayout(4, 4);
        grid.setHgap(20);
        grid.setVgap(10);

        panel = new JPanel();
        panel.setLayout(grid);

        String[] buttonname = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "%", "=", "+", "-", "*", "/" };
        for (int count = 0; count < buttonname.length; count++) {
            button = new JButton(buttonname[count]);
            button.setName(buttonname[count]);
            button.setFont(new Font("Cambria", Font.BOLD, 27));
            button.addActionListener(police);
            panel.add(button);
        }

        frame = new JFrame("NewFrame");
        frame.setBounds(500, 200, 500, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        frame.add(text, BorderLayout.NORTH);
        frame.add(panel, BorderLayout.CENTER);

        frame.validate();
        frame.setVisible(true);
    }

    class Police implements ActionListener {

        Character method = null;

        public void actionPerformed(ActionEvent e) {

            JButton button = (JButton) (e.getSource());
            String name = button.getName();
            System.out.println(curdigit.length());

            if (Character.isDigit(name.charAt(0))) {
                if (curdigit.length() >= MaxDigitLenth)
                    return;
                curdigit = new StringBuffer(curdigit).append(name).toString();
                text.setText(curdigit);
                return;
            }

            if (name.charAt(0) == '=') {
                int cur = Integer.parseInt(curdigit);
                int pre = Integer.parseInt(predigit);
                String ans = null;
                if (method == '+') {
                    ans = Integer.toString(cur + pre);
                }else if (method == '-') {
                    ans = Integer.toString(pre - cur); 
                }else if (method == '*') {
                    ans = Integer.toString(pre * cur);
                }else {
                    ans = Integer.toString(pre/cur);
                }
                text.setText(ans);
                curdigit = "";
                predigit = "";
                method = null;
            }

            if (name.charAt(0) == '+') {
                predigit = curdigit;
                curdigit = "";
                text.setText("+");
                method = '+';
            } else if (name.charAt(0) == '-') {
                predigit = curdigit;
                curdigit = "";
                text.setText("-");
                method = '-';
            } else if (name.charAt(0) == '*') {
                predigit = curdigit;
                curdigit = "";
                text.setText("*");
                method = '*';
            } else if (name.charAt(0) == '/') {
                predigit = curdigit;
                curdigit = "";
                text.setText("/");
                method = '/';
            }



        }

    }

}

0
Posted in Java

Leave a Comment:

电子邮件地址不会被公开。