我目前正在使用 Swing 开发 GUI 计算器程序。我已经完成了实际的界面 here
但是,每当我单击其中一个数字按钮时,操作数文本区域中不会出现任何内容,而结果文本区域中会出现 0.0,如 here 所示。 .
这是我的代码:
`
public class Calculator extends JFrame implements ActionListener
{
private static JTextArea operandArea;
private static JTextArea resultArea;
private static JButton[] buttons = new JButton[10];
public Calculator()
{
setTitle("Calculator");
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
BorderLayout calculatorLayout = new BorderLayout();
setLayout(calculatorLayout);
JPanel wholeCalculator = new JPanel();
add(wholeCalculator);
JPanel textAreas = new JPanel();
wholeCalculator.add(textAreas, calculatorLayout.NORTH);
textAreas.setPreferredSize(new Dimension(800, 300));
JPanel numberPad = new JPanel();
wholeCalculator.add(numberPad, calculatorLayout.SOUTH);
numberPad.setPreferredSize(new Dimension(800, 300));
operandArea = new JTextArea();
operandArea.setPreferredSize(new Dimension(600,75));
resultArea = new JTextArea();
resultArea.setPreferredSize(new Dimension(600,75));
textAreas.setLayout(new GridLayout(4, 1));
JLabel operand = new JLabel("Operand");
JLabel result = new JLabel("Result");
textAreas.add(operand);
operand.setHorizontalAlignment(JLabel.CENTER);
textAreas.add(operandArea);
textAreas.add(result);
result.setHorizontalAlignment(JLabel.CENTER);
textAreas.add(resultArea);
numberPad.setLayout(new GridLayout(1, 2));
JPanel numbers = new JPanel();
numberPad.add(numbers);
numbers.setLayout(new GridLayout(4, 3));
numbers.setPreferredSize(new Dimension(400, 400));
JPanel operations = new JPanel();
numberPad.add(operations);
operations.setPreferredSize(new Dimension(200, 400));
JButton minus = new JButton("-");
minus.setActionCommand("minus");
operations.add(minus);
JButton plus = new JButton("+");
plus.setActionCommand("plus");
operations.add(plus);
JButton multiply = new JButton("X");
multiply.setActionCommand("multiply");
operations.add(multiply);
JButton divide = new JButton("/");
divide.setActionCommand("divide");
operations.add(divide);
JButton decimal = new JButton(".");
operations.add(decimal);
JButton reset = new JButton("reset");
reset.setActionCommand("reset");
operations.add(reset);
JButton clear = new JButton("clear");
operations.add(clear);
for (int i = 1; i <= 9; i++)
{
JButton temp = new JButton(i + " ");
buttons[i] = temp;
temp.addActionListener(this);
numbers.add(temp);
}
numbers.add(new JButton("0"));
}
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
double initializer = 0.0;
String operand = "";
resultArea.setText(Double.toString(initializer));
switch (action)
{
case "0":
operandArea.append("0");
operand = operand + "0";
break;
case "1":
operandArea.append("1");
operand = operand + "1";
break;
case "2":
operandArea.append("2");
operand = operand + "2";
break;
case "3":
operandArea.append("3");
operand = operand + "3";
break;
case "4":
operandArea.append("4");
operand = operand + "4";
break;
case "5":
operandArea.append("5");
operand = operand + "5";
break;
case "6":
operandArea.append("6");
operand = operand + "6";
break;
case "7":
operandArea.append("7");
operand = operand + "7";
break;
case "8":
operandArea.append("8");
operand = operand + "8";
break;
case "9":
operandArea.append("9");
operand = operand + "9";
break;
case ".":
operandArea.append(".");
operand = operand + ".";
break;
case "plus":
initializer += Double.parseDouble(operand);
resultArea.setText(Double.toString(initializer));
break;
case "minus":
initializer -= Double.parseDouble(operand);
resultArea.setText(Double.toString(initializer));
break;
case "multiply": initializer *= Double.parseDouble(operand);
resultArea.setText(Double.toString(initializer));
break;
case "divide":
try
{
if (operand == "0")
{
throw new DivideByZeroException();
}
initializer /= Double.parseDouble(operand);
resultArea.setText(Double.toString(initializer));
}
catch (DivideByZeroException ex)
{
resultArea.setText("Cannot divide by 0");
}
break;
case "clear":
operandArea.setText("");
break;
case "reset":
operandArea.setText("");
resultArea.setText("");
initializer = 0.0;
break;
}
}
public static void main(String[] args)
{
Calculator frame = new Calculator();
frame.setVisible(true);
}
}
`
我认为使用追加命令会添加到空白处?如果有任何意见,我将不胜感激。
最佳答案
您的问题是一个复合问题。
首先,您使用...创建按钮
JButton temp = new JButton(i + " ");
默认情况下,将 actionCommmand
设置为 i_
<- 而不是空格! (所以不允许我添加尾随空格,所以我使用 _
来突出显示)
然后你使用
switch (action)
{
case "0":
//...
要检查actionCommand
,但该命令不是(作为示例)[0
],而是[0_
]
相反,使用类似...的东西
JButton temp = new JButton(Integer.toString(i));
您还需要修复:
if (operand == "0")
as ==
不是您应该比较 Strings 的方式(一个好的 IDE 应该警告您这一点)。而是使用
equals`,例如...
if ("0".equals(operand))
关于java - JTextArea 未附加到 swing GUI 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59328551/