没有数字按钮的GUI计算器

时间:2014-12-13 14:13:22

标签: java swing user-interface

你好,我有这个计算器,我希望它是无按钮的,就像在文本字段中输入用户输入,但我不知道如何让它像那样工作。我想像这样简化它,但我不知道如何在没有数字按钮的情况下使其工作。

enter image description here

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class MP2_2 extends JFrame implements ActionListener {

Container c;
JTextField result;
JPanel p = new JPanel();
JButton b[] = new JButton[16];
String s[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "/", "*", "=", "C"};
//----------------------Me------------------------
String Screen = "", monitor1 = "", monitor2 = "", OperationOnScreen = "";
boolean CommandEmpty = true, switcher = true;
double R = Integer.MIN_VALUE, L = Integer.MIN_VALUE;

//------------------------------------------------

public MP2_2() {
    super("MP2_2");
    c = getContentPane();
    result = new JTextField();
    result.setEditable(true);

    p.setLayout(new GridLayout(4, 4));
    for (int i = 0; i < 16; i++) {
        b[i] = new JButton(s[i]);
        b[i].addActionListener(this);
        p.add(b[i]);
    }
    c.add(result, BorderLayout.NORTH);
    c.add(p);

}//End Constructor

public static void main(String[] args) {
    MP2_2 calcu = new MP2_2();
    calcu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    calcu.setSize(300, 300);
    calcu.setVisible(true);
}

//------------------------------------------------              

public void actionPerformed(ActionEvent event) {
    for (int i = 0; i <= 9; i++)//Numbers
    {
        if (event.getSource() == b[i]) {
            Screen += i;
            result.setText("");
            result.setText(Screen);
        }
    }

    for (int i = 10; i <= 14; i++)//Commands
    {
        if (event.getSource() == b[i]) {
            if (result.getText().lastIndexOf(OperationOnScreen) != -1)//prevent exception
            {
                result.setText(result.getText().substring(0, result.getText().lastIndexOf(OperationOnScreen)) + s[i]);
            } else {
                result.setText(result.getText() + s[i]);
            }
            OperationOnScreen = s[i];

            if (switcher) {
                monitor1 = s[i];
                switcher = false;
            } else {
                monitor2 = s[i];
                switcher = true;
            }

            if (monitor1 != monitor2 && monitor2 != "") {
                if (switcher) //execute older,send sign newer
                {
                    Calc(event, monitor1.charAt(0), monitor2);
                } else {
                    Calc(event, monitor2.charAt(0), monitor1);
                }
            }
            if (s[i] != "=") //calc returns 0
            {
                Calc(event, s[i].charAt(0), s[i]);
            }
        }
    }

    if (event.getSource() == b[15]) //Clear
    {
        Screen = "";
        monitor1 = "";
        monitor2 = "";
        switcher = true;
        CommandEmpty = true;
        result.setText("");
    }
}//end actionPerformed

public void Calc(ActionEvent event, char OpType, String Operator) {
    if (Operator == "=") {
        Operator = "";
    }

    if (CommandEmpty && Screen == "") {
        return;
    } else if (CommandEmpty && Screen != "") {
        R = Integer.parseInt(Screen);
        result.setText(Screen + Operator);
        Screen = "";
        CommandEmpty = false;
    } else if (!CommandEmpty && Screen != "") {
        L = Integer.parseInt(Screen);
        R = Operations(R, L, OpType);//calculate
        Screen = "";
        result.setText("");
        result.setText(R + Operator);
    }
}//End Calc             

public static double Operations(double R, double L, char op) {
    switch (op) {
        case '+':
            return R + L;
        case '-':
            return R - L;
        case '*':
            return R * L;
        case '/':
            return R / L;
    }
    return 0;
}
}//end class
编辑:我想我的问题不是很清楚抱歉。我希望我的程序也支持键盘输入,我该怎么做?所以我最终可以删除图片中的数字按钮。

1 个答案:

答案 0 :(得分:0)

当您删除按钮时,您需要通过调用result.getText()来恢复按钮的操作。

以下是适用于您的数字按钮的代码。它设置了屏幕的值。

for (int i = 0; i <= 9; i++)//Numbers
{
    if (event.getSource() == b[i]) {
        Screen += i;
        result.setText("");
        result.setText(Screen);
    }
}

因此,当您点击操作按钮时,您需要设置屏幕。 这是你的命令循环。我添加了Screen = result.getText();在else块中。它对我有用。

for (int i = 10; i <= 14; i++)//Commands
{
    if (event.getSource() == b[i]) {
        if (result.getText().lastIndexOf(OperationOnScreen) != -1)//prevent exception
        {
            result.setText(result.getText().substring(0, result.getText().lastIndexOf(OperationOnScreen)) + s[i]);
        } else {
            Screen = result.getText();
            result.setText(result.getText() + s[i]);
        }