Java计算器历史功能

标签 java history calculator

我希望有人可以帮助我提供一个功能,该功能可以将计算器的历史记录存储为字符串,并在用户按下 H 键时显示历史记录。我正在寻找它来记录按下的每个按钮,包括运算符(operator)。任何帮助表示赞赏。谢谢。

public class GUI extends JPanel implements KeyListener {


private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;
private JButton b7;
private JButton b8;
private JButton b9;
private JButton b0;
private JButton add;
private JButton subtract;
private JButton multiply;
private JButton divide;
private JButton solve;
private JButton clear;
private JButton decimal;
private JButton sqrt;
private JButton recip;
private JButton backSpace;
private JButton ms;
private JButton mr;
private JButton mc;
private JButton percent;
private JButton love;
private JTextField jtf;
String display = "";
String mStore;
String history;
boolean multiplyBool = false;
boolean divideBool = false;
boolean addBool = false;
boolean subtractBool = false;
boolean percentBool = false;
ImageIcon heart = new ImageIcon("heartt.png");

private CalcLogic cl = new CalcLogic();


public GUI(){
setLayout(new BorderLayout());

JPanel p3 = new JPanel(new BorderLayout());
p3.setFont(new Font("Arial", Font.BOLD, 30));
p3.add(jtf = new JTextField("0",12));
jtf.setEditable(false);
jtf.setHorizontalAlignment(JTextField.RIGHT);
jtf.setFont(new Font("Arial", Font.BOLD, 40));




JPanel p2 = new JPanel();
p2.setBackground(Color.CYAN);

p2.setLayout(new GridLayout(5,5,2,2));


//row 1 buttons


p2.add(mc = new JButton("MC"));
mc.setFont(new Font("Arial", Font.BOLD, 12));

p2.add(mr = new JButton("MR"));
mr.setFont(new Font("Arial", Font.BOLD, 12));

p2.add(ms = new JButton("MS"));
ms.setFont(new Font("Arial", Font.BOLD, 12));

p2.add(divide = new JButton("\u00F7"));
divide.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(backSpace = new JButton("\u2190"));
backSpace.setFont(new Font("Arial Bold", Font.BOLD, 18));


//row 2 buttons


p2.add(b7 = new JButton("7"));
b7.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(b8 = new JButton("8"));
b8.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(b9 = new JButton("9"));
b9.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(multiply = new JButton("\u00D7"));
multiply.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(sqrt = new JButton("\u221A"));
sqrt.setFont(new Font("Arial", Font.PLAIN, 20));


//row 3 buttons


p2.add(b4 = new JButton("4"));
b4.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(b5 = new JButton("5"));
b5.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(b6 = new JButton("6"));
b6.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(subtract = new JButton("\u2212"));
subtract.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(recip = new JButton("1/x"));
recip.setFont(new Font("Arial", Font.PLAIN, 14));


//row 4 buttons


p2.add(b1 = new JButton("1"));
b1.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(b2 = new JButton("2"));
b2.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(b3 = new JButton("3"));
b3.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(add = new JButton("+"));
add.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(percent = new JButton("%"));
percent.setFont(new Font("Arial", Font.PLAIN, 20));


//row 5 buttons


p2.add(b0 = new JButton("0"));
b0.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(decimal = new JButton("."));
decimal.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(clear = new JButton("C"));
clear.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(solve = new JButton("="));
solve.setFont(new Font("Arial", Font.PLAIN, 20));

p2.add(love = new JButton(heart));
love.addActionListener(new LoveListener());

add(p3, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
this.setFocusable(true);
addKeyListener(this);

ActionListener buttonListener = new buttonListener();

ActionListener ClearListener = new ClearListener();



b7.addActionListener(buttonListener);
b8.addActionListener(buttonListener);
b9.addActionListener(buttonListener);
add.addActionListener(new addListener());
b4.addActionListener(buttonListener);
b5.addActionListener(buttonListener);
b6.addActionListener(buttonListener);
subtract.addActionListener(new subtractListener());
b1.addActionListener(buttonListener);
b2.addActionListener(buttonListener);
b3.addActionListener(buttonListener);
multiply.addActionListener(new multiplyListener());
b0.addActionListener(buttonListener);
decimal.addActionListener(buttonListener);
clear.addActionListener(ClearListener);
solve.addActionListener(new solveListener());
divide.addActionListener(new divideListener());
sqrt.addActionListener(new sqrtListener());
recip.addActionListener(new recipListener());
backSpace.addActionListener(new backSpaceListener());
ms.addActionListener(new msListener());
mr.addActionListener(new mrListener());
mc.addActionListener(new mcListener());
percent.addActionListener(new percentListener());

} //GUI()

    private void action_clear(){

jtf.setText("");
display = jtf.getText();
//op = "";
cl.setTotal("0");
multiplyBool = false;
addBool = false;
subtractBool = false;
divideBool = false;

    }




    public class buttonListener implements ActionListener
    {   
public void actionPerformed(ActionEvent e)
{       
    String digit = e.getActionCommand();


    jtf.setText(display + digit );
    display = jtf.getText();
    playSound("Click.wav");
}
    }//buttonListener

    public class multiplyListener implements ActionListener
    {
private CalcLogic cl = new CalcLogic();

public void actionPerformed(ActionEvent e)
{   
    if(multiplyBool == true)
    {   
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
        }
        cl.multiply(display);
        jtf.setText(cl.getString());
        display = "";
    }else if(divideBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
        }
        cl.divide(display);
        jtf.setText(cl.getString());
        display = "";
    }else if(addBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
            cl.percMult(display);
            display = cl.getPercent();

        }

        cl.add(display);

        jtf.setText(cl.getString());
        display = "";
    }else if (subtractBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
            cl.percMult(display);
            display = cl.getPercent();
        }
        cl.subtract(display);
        jtf.setText(cl.getString());
        display = "";
    }else 
    {
    cl.setTotal(display);
    jtf.setText("");
    multiplyBool = true;
    divideBool = false;
    addBool = false;
    subtractBool = false;
    percentBool = false;
    display = "";
    }

    multiplyBool = true;
    divideBool = false;
    addBool = false;
    subtractBool = false;
    percentBool = false;
}

    }

    public class divideListener implements ActionListener
   {
public void actionPerformed(ActionEvent e)
{
    if(multiplyBool == true)
    {   
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
        }
        cl.multiply(display);
        jtf.setText(cl.getString());
        display ="";
    }else if(divideBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
        }
        cl.divide(display);
        jtf.setText(cl.getString());
        display = "";
    }else if(addBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
            cl.percMult(display);
            display = cl.getPercent();

        }

        cl.add(display);

        jtf.setText(cl.getString());
        display = "";
    }else if (subtractBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
            cl.percMult(display);
            display = cl.getPercent();
        }
        cl.subtract(display);
        jtf.setText(cl.getString());
        display = "";
    }else
    {
    cl.setTotal(display);
    jtf.setText("");
    divideBool = true;
    multiplyBool = false;
    addBool = false;
    subtractBool = false;
    percentBool = false;
    display = "";
    }

    multiplyBool = false;
    divideBool = true;
    addBool = false;
    subtractBool = false;
    percentBool = false;
}
    }
    public class addListener implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{
    if(multiplyBool == true)
    {   
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
        }
        cl.multiply(display);
        jtf.setText(cl.getString());
        display ="";
    }else if(divideBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
        }
        cl.divide(display);
        jtf.setText(cl.getString());
        display = "";
    }else if(addBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
            cl.percMult(display);
            display = cl.getPercent();

        }

        cl.add(display);

        jtf.setText(cl.getString());
        display = "";
    }else if (subtractBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
            cl.percMult(display);
            display = cl.getPercent();
        }
        cl.subtract(display);
        jtf.setText(cl.getString());
        display = "";
    }else
    {
    cl.setTotal(display);
    jtf.setText("");
    divideBool = false;
    multiplyBool = false;
    addBool = true;
    subtractBool = false;
    percentBool = false;
    display = "";
    }

    divideBool = false;
    multiplyBool = false;
    subtractBool = false;
    addBool = true;
    percentBool = false;

}
    }//addListener

    public class subtractListener implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{
    if(multiplyBool == true)
    {   
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
        }
        cl.multiply(display);
        jtf.setText(cl.getString());
        display ="";
    }else if(divideBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
        }
        cl.divide(display);
        jtf.setText(cl.getString());
        display = "";
    }else if(addBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
            cl.percMult(display);
            display = cl.getPercent();

        }

        cl.add(display);

        jtf.setText(cl.getString());
        display = "";
    }else if (subtractBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
            cl.percMult(display);
            display = cl.getPercent();
        }
        cl.subtract(display);
        jtf.setText(cl.getString());
        display = "";
    }else
    {
    cl.setTotal(display);
    jtf.setText("");
    divideBool = false;
    multiplyBool = false;
    addBool = false;
    subtractBool = true;
    percentBool = false;
    display = "";
    }
    divideBool = false;
    multiplyBool = false;
    addBool = false;
    subtractBool = true;
    percentBool = false;

}
    }//subtractListener

    public class sqrtListener implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{
    cl.setTotal(display);
    cl.squareRoot(display);
    jtf.setText(cl.getString());
    playSound("Click.wav");
}
    }

    public class recipListener implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{
    cl.setTotal(display);
    cl.reciprical(display);
    jtf.setText(cl.getString());
    playSound("Click.wav");
}
    }

    public class backSpaceListener implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{
    jtf.setText("");
    display = "";
}


    }

    public class msListener implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{
    mStore = jtf.getText();
}
    }

    public class mrListener implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{
    jtf.setText(mStore);
    display = mStore;

}
    }

    public class mcListener implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{
    mStore = "";
}
    }

    public class percentListener implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{

    percentBool = true;
}
    }


    public class solveListener implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{
    playSound("that_was_easy.wav");

    if(multiplyBool == true)
    {   
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
        }
        cl.multiply(display);
        jtf.setText(cl.getString());
    }else if(divideBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
        }
        cl.divide(display);
        jtf.setText(cl.getString());
    }else if(addBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
            cl.percMult(display);
            display = cl.getPercent();

        }

        cl.add(display);

        jtf.setText(cl.getString());
    }else if (subtractBool == true)
    {
        if (percentBool==true)
        {
            cl.percentage(display);
            display = cl.getPercent();
            cl.percMult(display);
            display = cl.getPercent();
        }
        cl.subtract(display);
        jtf.setText(cl.getString());
    }
    multiplyBool = false;
    addBool = false;
    subtractBool = false;
    divideBool = false;
    percentBool = false;

    //display = jtf.getText();

}

    }


public class ClearListener implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{
    action_clear();
    playSound("Click.wav");
}//ClearListener


    }
public void keyTyped(KeyEvent e){

if(e.getKeyCode() == KeyEvent.VK_H){
    System.out.println(history);
)
public void keyTyped(KeyEvent e){

}
public void keyReleased(KeyEvent e){   

}
}

最佳答案

只需将每个单行计算器命令作为字符串存储到数组中,然后再删除它。然后当您按 H 时显示它。

例如:

Array array = new Array(5) (You could also use collection classes to make it dynamic and more efficient)

123 (Line#1 in calculator) >> array[0]
+ (Line#2 in calculator) >> array[1]
34567 (Line#3 in calculator) >> array[2]
% (Line#4 in calculator) >> array [3]
345 (Line#5 in calculator) >> array [4]

依此类推。

问候,

关于Java计算器历史功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16115230/

相关文章:

java - 如何在应用程序上下文之前评估 spring ApplicationArguments?

git - 我可以让 hg log 以相反的顺序打印历史记录吗?

multithreading - “Steam Method” THE'-Multiprogramming System”中的 “Structure of ' Dijkstra是什么意思?

C++ 计算器最后显示和额外的 0

java - Android:如何为 Activity.onNewIntent() 设置监听器?

java - Web服务动态数据传输和验证

linux - 如何查找自机器创建以来 shell 命令的历史记录?

c# - 需要一些指导来使用 C# 制作 GUI 计算器吗?

java - 使用 JFrame 的 JAVA 贷款计算器出现异常?

java - "install"如何在eclipse中简单化?