我刚开始学习Java中的GUI,我想知道是否有人可以帮助解决我遇到的问题。我试图做一个计算器,但问题是每当我减去两个数字,然后单击等号按钮,它似乎添加2个数字,而不是减法。计算器正在添加数字而不是减去
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JPanel;
class Colorwindow extends JFrame implements ActionListener {
private JButton clear, addition, subtract, divide, multiply, zero, one, two, three, four, five, six, seven, eight, nine, ten, equal;
private JTextField name, name2, name3;
private String inputing, solution, solution2, solution3, solution4;
private boolean AddStatement, SubtractStatement, MultiplyStatement, DivideStatement, statement;
private JButton SButtonList[] = new JButton[6];
private JButton NButtonList[] = new JButton[10];
private String SymbolList[] = {
"+",
"-",
"/",
"*",
"=",
"C"
};
private String NumberList[] = {
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"
};
private String NumberList2[] = {
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"
};
private double result[] = new double[10];
private double number[] = new double[10];
//AddStatement = false;
Colorwindow() {
super();
setSize(500, 500); //sets size of window
getContentPane().setBackground(Color.GRAY); //sets backgroundcolor to yellow
rows 3 column
JPanel textfont = new JPanel();
name = new JTextField(30);
textfont.add(name); //adds textfield
name.setBackground(Color.CYAN);
Font bigFont = name.getFont().deriveFont(Font.PLAIN, 70 f);
name.setFont(bigFont);
name2 = new JTextField(30);
//textfont.add(name2);//adds textfield
add(textfont, BorderLayout.NORTH);
JPanel rows = new JPanel();
rows.setLayout(new GridLayout(2, 4));
for (int i = 0; i < 10; i++) {
NButtonList[i] = new JButton(NumberList[i]);
rows.add(NButtonList[i]); //add's the buttons
NButtonList[i].addActionListener(this);
add(rows, BorderLayout.CENTER);
}
for (int i = 0; i < 6; i++) {
SButtonList[i] = new JButton(SymbolList[i]);
rows.add(SButtonList[i]); //add's the buttons
SButtonList[i].addActionListener(this);
add(rows, BorderLayout.CENTER);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes window button when pressing the x(EXIT_ON_CLOSE)
//add(button);//adds a button to the window//adds componenets to jframe//event
setTitle("Calculator"); //sets title on top of window
}
private static double stringToDouble(String stringObject) {
return Double.parseDouble(stringObject.trim());
}
public void actionPerformed(ActionEvent e) {
try {
PassesCorrect(e);
} catch (NumberFormatException e2) {
name.setText("Please re-press on a number");
}
}
public void PassesCorrect(ActionEvent e) {
String ButtonString = e.getActionCommand();
for (int i = 0; i < 10; i++) {
if (e.getSource() == NButtonList[i]) {
name.setText(name.getText() + NumberList[i]); //appends text
}
}
for (int i = 0; i < 6; i++) {
if (e.getSource() == SButtonList[i]) {
//number = Double.parseDouble(name.getText());
name2.setText(SymbolList[i]);
}
}
if (e.getSource() == SButtonList[0]) //checks if it's addition
{
for (int i = 0; i < 10; i++) {
number[i] = Double.parseDouble(name.getText()); //stores the number that has been entered into an array
}
//solution=name.getText();//gets the text and adds it into a string
name.setText("+"); //sets the number from string and input's it on screen
AddStatement = true;
} else if (e.getSource() == SButtonList[1]) //checks if subtraction
{
for (int i = 0; i < 10; i++) {
number[i] = Double.parseDouble(name.getText());
}
//solution=name.getText();//gets the text and adds it into a string
name.setText("-"); //sets the number from string and input's it on screen
SubtractStatement = true;
} else if (e.getSource() == SButtonList[2]) {
for (int i = 0; i < 10; i++) {
number[i] = Double.parseDouble(name.getText());
}
//solution=name.getText();//gets the text and adds it into a string
name.setText("/"); //sets the number from string and input's it on screen
DivideStatement = true;
} else if (e.getSource() == SButtonList[3]) {
for (int i = 0; i < 10; i++) {
number[i] = Double.parseDouble(name.getText());
}
//solution=name.getText();//gets the text and adds it into a string
name.setText("*"); //sets the number from string and input's it on screen
MultiplyStatement = true;
} else if (e.getSource() == SButtonList[5]) {
name.setText("");
SubtractStatement = false;
AddStatement = false;
DivideStatement = false;
MultiplyStatement = false;
} else if (e.getSource() == SButtonList[4]) //checks if it's equal sign
{
for (int i = 0; i < 10; i++) {
result[i] = Double.parseDouble(name.getText());
}
if (SubtractStatement == true) {
for (int i = 0; i < 10; i++) {
result[i] = number[i] - result[i];
name.setText(Double.toString(result[i]));
}
} else if (AddStatement == true) {
for (int i = 0; i < 10; i++) {
result[i] = number[i] + result[i];
name.setText(Double.toString(result[i]));
}
//result+=number;
} else if (MultiplyStatement == true) {
} else if (DivideStatement == true) {
//result=number/result;
//name.setText(Double.toString(result));
}
SubtractStatement = false;
AddStatement = false;
DivideStatement = false;
MultiplyStatement = false;
}
}
}
public class GUI2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Colorwindow W1 = new Colorwindow();
W1.setVisible(true);
}
很抱歉,但你的代码风格是如此糟糕,我拒绝读它。缩进是一团糟,换行符错误,符号之间的空白符号是错误的,标识符约定被违反,代码注释堆积如山。请阅读Java风格指南并解决此问题...如果您希望其他人花时间阅读您的代码以帮助您。 –
缩小你的代码 –
我建议学习GUI编程之前了解一些IDE调试,然后使用这些技能在这里找出错误,或者创建一个[MCVE],请 –