JTextArea 未附加到 swing GUI 中
JTextArea not being appended to in swing GUI
我目前正在使用 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);
}
}
`
我认为使用追加命令会添加到空白处 space?我将不胜感激。
你的问题是一个复合问题。
首先,您使用...创建按钮...
JButton temp = new JButton(i + " ");
默认情况下,这会将 actionCommmand
设置为 i_
<- 而不是 space! (所以不会让我输入尾随 space,所以我使用 _
来突出显示)
然后你使用
switch (action)
{
case "0":
//...
要检查 actionCommand
,但命令不是(例如)[0
],它是 [0_
]
相反,使用类似...
JButton temp = new JButton(Integer.toString(i));
您还想修复:
if (operand == "0")
因为 ==
不是您比较 Strings (and a good IDE should be warning you about it). Instead use
equals 的方式,例如...
if ("0".equals(operand))
我目前正在使用 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);
}
}
`
我认为使用追加命令会添加到空白处 space?我将不胜感激。
你的问题是一个复合问题。
首先,您使用...创建按钮...
JButton temp = new JButton(i + " ");
默认情况下,这会将 actionCommmand
设置为 i_
<- 而不是 space! (所以不会让我输入尾随 space,所以我使用 _
来突出显示)
然后你使用
switch (action)
{
case "0":
//...
要检查 actionCommand
,但命令不是(例如)[0
],它是 [0_
]
相反,使用类似...
JButton temp = new JButton(Integer.toString(i));
您还想修复:
if (operand == "0")
因为 ==
不是您比较 Strings (and a good IDE should be warning you about it). Instead use
equals 的方式,例如...
if ("0".equals(operand))