如何解决Swing计算器GUI的十进制错误?

huangapple go评论78阅读模式
英文:

How can I solve the Swing Calculator GUI Decimal Error?

问题

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class CalculatorEvolved extends JFrame {
    // ... (Variable declarations)

    // ... (Constructor, main method, etc.)

    private void operandOneMethod(JButton x) {
        sum1 += x.getText();
        lblNewLabel.setText(sum1);
        operandOne.add(x.getText());
        System.out.println(operandOne);
    }
    
    private void operandTwoMethod(JButton x) {
        sum2 += x.getText();
        lblNewLabel.setText(sum2);
        operandTwo.add(x.getText());
        System.out.println(operandTwo);

        if (subtraction) {
            lblNewLabel.setText(sum1 + " - " + sum2);
        }
        if (OP4 == 1) {
            lblNewLabel.setText(sum1 + " + " + sum2);
        }
        if (multiplication) {
            lblNewLabel.setText(sum1 + " X " + sum2);
        }
        if (division) {
            lblNewLabel.setText(sum1 + " / " + sum2);
        }
    }
}

Note: The provided code is a section from a Java program that seems to be a calculator GUI with decimal calculations. Since you requested a translation of the code only, I've removed the non-code content and provided the translated code above. If you have any questions or need further assistance, feel free to ask!

英文:

I have been working on my swing calculator GUI, but when it comes to the use of decimals, I seem to have some bugs. For instance, when you subtract a non decimal from a decimal the results automatically turns out to be negative. Furthermore, the calculator doesn't seem to be able to calculate decimal and non-decimal figures together. I was wondering whether you guys could take a look at it and give me some feedback. Thanks in advance!

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class CalculatorEvolved extends JFrame {
private int OP4 = 0;
boolean secondOp, subtraction, multiplication, division;
boolean comma = false;
boolean firstOp = true;
private double a, b;
private double divisionA, divisionB, commaSum1, commaSum2;
private JPanel contentPane;
private JLabel lblNewLabel;
private JButton btnAddition;
private JButton btnSubtraction;
private JButton btnMultiplication;
private JButton btnDivision;
private JButton btnSquared;
private JButton btn0;
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btn5;
private JButton btn6;
private JButton btn7;
private JButton btn8;
private JButton btn9;
private JButton btnEquals;
private JButton btnClear;
private JButton btnComma;
private ArrayList<String> operandOne = new ArrayList<>();
private ArrayList<String> operandTwo = new ArrayList<>();
private String sum1 = "";
private String sum2 = "";
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalculatorEvolved frame = new CalculatorEvolved();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CalculatorEvolved() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 345, 382);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
getContentPane().setLayout(null);
lblNewLabel = new JLabel("");
lblNewLabel.setBounds(16, 6, 304, 29);
getContentPane().add(lblNewLabel);
btn8 = new JButton("8");
btn8.setBounds(16, 47, 78, 41);
getContentPane().add(btn8);
btn8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstOp) {
operandOneMethod(btn8);
}
if (secondOp) {
operandTwoMethod(btn8);
}
}
});
btn2 = new JButton("2");
btn2.setBounds(16, 199, 78, 41);
getContentPane().add(btn2);
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstOp) {
operandOneMethod(btn2);
}
if (secondOp) {
operandTwoMethod(btn2);
}
}
});
btn5 = new JButton("5");
btn5.setBounds(16, 120, 78, 41);
getContentPane().add(btn5);
btn5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstOp) {
operandOneMethod(btn5);
}
if (secondOp) {
operandTwoMethod(btn5);
}
}
});
btn0 = new JButton("0");
btn0.setBounds(16, 272, 78, 41);
getContentPane().add(btn0);
btn0.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstOp) {
operandOneMethod(btn0);
}
if (secondOp) {
operandTwoMethod(btn0);
}
}
});
btn9 = new JButton("9");
btn9.setBounds(106, 47, 78, 41);
getContentPane().add(btn9);
btn9.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstOp) {
operandOneMethod(btn9);
}
if (secondOp) {
operandTwoMethod(btn9);
}
}
});
btn6 = new JButton("6");
btn6.setBounds(106, 120, 78, 41);
getContentPane().add(btn6);
btn6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstOp) {
operandOneMethod(btn6);
}
if (secondOp) {
operandTwoMethod(btn6);
}
}
});
btn3 = new JButton("3");
btn3.setBounds(106, 199, 78, 41);
getContentPane().add(btn3);
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstOp) {
operandOneMethod(btn3);
}
if (secondOp) {
operandTwoMethod(btn3);
}
}
});
btn1 = new JButton("1");
btn1.setBounds(106, 272, 78, 41);
getContentPane().add(btn1);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstOp) {
operandOneMethod(btn1);
}
if (secondOp) {
operandTwoMethod(btn1);
}
}});
btnClear = new JButton("CLEAR");
btnClear.setBounds(196, 47, 78, 41);
getContentPane().add(btnClear);
btnClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lblNewLabel.setText("");
sum1 = "";
sum2 = "";
firstOp = true;
secondOp = false;
operandOne.clear();
operandTwo.clear();
a = 0;
b = 0;
divisionA = 0;
divisionB = 0;
OP4 = 0;
subtraction = false;
multiplication = false;
division = false;
comma = false;
commaSum1 = 0;
commaSum2 = 0;
}
});
btn7 = new JButton("7");
btn7.setBounds(196, 120, 78, 41);
getContentPane().add(btn7);
btn7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstOp) {
operandOneMethod(btn7);
}
if (secondOp) {
operandTwoMethod(btn7);
}
}
});
btn4 = new JButton("4");
btn4.setBounds(196, 199, 78, 41);
getContentPane().add(btn4);
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstOp) {
operandOneMethod(btn4);
}
if (secondOp) {
operandTwoMethod(btn4);
}
}
});
btnComma = new JButton(",");
btnComma.setBounds(196, 272, 78, 41);
getContentPane().add(btnComma);
btnComma.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstOp) {
comma = true;
sum1 += ".";
lblNewLabel.setText(sum1);
operandOne.add(btnComma.getText());
System.out.println(operandOne);
}
if (secondOp) {
comma = true;
sum2 += ".";
if (OP4 ==1) {
lblNewLabel.setText(commaSum1 + " + " + sum2);
}
else if (subtraction) {
lblNewLabel.setText(commaSum1 + " - " + sum2);
}
else if (multiplication) {
lblNewLabel.setText(commaSum1 + " * " + sum2);
}
else if (division) {
lblNewLabel.setText(commaSum1 + " / " + sum2);
}
operandTwo.add(btnComma.getText());
System.out.println(operandTwo);
}
}
});
btnAddition = new JButton("+");
btnAddition.setBounds(286, 47, 53, 41);
getContentPane().add(btnAddition);
btnAddition.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstOp = false;
if (!comma) {
lblNewLabel.setText(sum1 + " + ");
a = Double.parseDouble(sum1);
}
else if (comma) {
commaSum1 = Double.parseDouble(sum1);
lblNewLabel.setText(commaSum1 + " + ");
}
secondOp = true;
OP4++;
}
});
btnSubtraction = new JButton("-");
btnSubtraction.setBounds(286, 120, 53, 41);
getContentPane().add(btnSubtraction);
btnSubtraction.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstOp = false;
if (!comma) {
lblNewLabel.setText(sum1 + " - ");
a = Double.parseDouble(sum1);
}
else if (comma) {
commaSum1 = Double.parseDouble(sum1);
lblNewLabel.setText(commaSum1 + " - ");
}
secondOp = true;
subtraction = true;
}
});
btnMultiplication = new JButton("X");
btnMultiplication.setBounds(286, 199, 53, 41);
getContentPane().add(btnMultiplication);
btnMultiplication.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstOp = false;
if (!comma) {
lblNewLabel.setText(sum1 + " X ");
a = Double.parseDouble(sum1);
}
else if (comma) {
commaSum1 = Double.parseDouble(sum1);
lblNewLabel.setText(commaSum1 + " X ");
}
secondOp = true;
multiplication = true;
}
});
btnDivision = new JButton("/");
btnDivision.setBounds(286, 272, 53, 41);
getContentPane().add(btnDivision);
btnDivision.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstOp = false;
if (!comma) {
lblNewLabel.setText(sum1 + " / ");
a = Integer.parseInt(sum1);
}
else if (comma) {
commaSum1 = Double.parseDouble(sum1);
lblNewLabel.setText(commaSum1 + " / ");
}
secondOp = true;
division = true;
}
});
btnEquals= new JButton("=");
btnEquals.setBounds(286, 313, 53, 41);
getContentPane().add(btnEquals);
btnEquals.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DecimalFormat x = new DecimalFormat("#0.000");
if (OP4==1) {
if (comma) {
commaSum2 = Double.parseDouble(sum2);
double sum = commaSum2 + commaSum1;
lblNewLabel.setText(commaSum1 + " + " + commaSum2 + " = " + x.format(sum));
}
else {
b = Integer.parseInt(sum2);
double sum = a+b;
lblNewLabel.setText(a + " + " + b + " = " + x.format(sum));
}
}
if (subtraction) {
if (comma) {
commaSum2 = Double.parseDouble(sum2);
double sum = commaSum2 - commaSum1;
lblNewLabel.setText(commaSum1 + " - " + commaSum2 + " = " + x.format(sum));
}
else {
b = Integer.parseInt(sum2);
double sum = a-b;
lblNewLabel.setText(a + " - " + b + " = " + x.format(sum));
}
}
if (multiplication) {
if (comma) {
commaSum2 = Double.parseDouble(sum2);
double sum = commaSum2 + commaSum1;
lblNewLabel.setText(commaSum1 + " X " + commaSum2 + " = " + x.format(sum));
}
else {
b = Integer.parseInt(sum2);
double sum = a * b;
lblNewLabel.setText(a + " X " + b + " = " + x.format(sum));
}
}
if (division) {
if (comma) {
commaSum2 = Double.parseDouble(sum2);
double sum = commaSum2 + commaSum1;
lblNewLabel.setText(commaSum1 + " / " + commaSum2 + " = " + x.format(sum));
}
else {
b = Integer.parseInt(sum2);
double sum = a/ b;
lblNewLabel.setText(a + " / " + b + " = " + x.format(sum));
}
}
}
});
}
private void operandOneMethod(JButton x) {
sum1 += x.getText();
lblNewLabel.setText(sum1);
operandOne.add(x.getText());
System.out.println(operandOne);
}
private void operandTwoMethod(JButton x) {
sum2 += x.getText();
lblNewLabel.setText(sum2);
operandTwo.add(x.getText());
System.out.println(operandTwo);
if (subtraction) {
lblNewLabel.setText(sum1 + " - " + sum2);
}
if (OP4==1) {
lblNewLabel.setText(sum1 + " + " + sum2);
}
if (multiplication) {
lblNewLabel.setText(sum1 + " X " + sum2);
}
if (division) {
lblNewLabel.setText(sum1 + " / " + sum2);
}
}
}

答案1

得分: 1

你只需要调试你的代码,即使用调试器来运行它。每个好的集成开发环境都配有调试器。

你会发现你代码中的这行是错误的:

double sum = commaSum2 - commaSum1;

它应该是:

double sum = commaSum1 - commaSum2;

你想要从第一个操作数中减去第二个操作数。

请参考btnEqualsActionListener

英文:

All you need to do is debug your code, i.e. use a debugger to run it. Every good IDE has a debugger.

You would see that this line of your code is wrong:

double sum = commaSum2 - commaSum1;

It should be:

double sum = commaSum1 - commaSum2;

You want to subtract the second operand from the first.

Refer to the ActionListener for btnEquals.

huangapple
  • 本文由 发表于 2020年8月28日 18:27:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63632004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定