【发布时间】:2017-01-15 22:45:24
【问题描述】:
我遇到了一些小问题..
我正在尝试使用 Java 开发一个基本的 GUI 计算器。我使用JTextArea 输入数字并仅执行所有计算。我希望文本区域也显示最终结果,但 基本问题是当我按下加号按钮时。它显示错误并且没有显示任何结果。我知道问题出在textarea.settext(""),但我不知道如何克服它。
我指定的是下面代码的重要部分,而不是整个代码。
这是我目前的代码:
JTextArea ta = new JTextArea();
ta.setFont(new Font("Monospaced", Font.BOLD, 20));
ta.setBounds(10, 11, 319, 74);
contentPane.add(ta);
JButton button_14 = new JButton("1 ");
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.append("1");
}
JButton button_17 = new JButton("+");
button_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1=Float.parseFloat(ta.getText());
ta.setText("");
ta.setText(ta.getText());
num2=Float.parseFloat(ta.getText());
ans=num1+num2;
}
JButton button_11 = new JButton("=");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setText(String.valueOf(ans));
}
【问题讨论】:
-
这没有意义
ta.setText(""); ta.setText(ta.getText()); -
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建minimal reproducible example
-
“Plzz 我急需一些帮助..” 你需要的是更好的时间管理技能。不要来这里告诉我们您(对我们)的任意时间限制。我们通常更喜欢帮助能够更好地管理时间的人。 “我正在指定代码的重要部分” 发布 @xenteros 提到的 MCVE。 “它显示错误”总是复制/粘贴错误和异常输出!
-
是的,我为这样的声明道歉......后来我将我的代码修复为工作状态。谢谢@xenteros
标签: java swing user-interface textarea calculator