JTextArea 不会显示以前的输入

我目前正在使用 GUI 开发计算器。

这就是我的意思以及它应该如何工作。

  1. 输入一串数字运算符号后,用户可以点击: a.'=',在这种情况下计算器必须显示: i. 第二个数字的最后一位数字后面的“=”符号 ii。操作的结果,在一个新的行上 iii。在“=”符号之后输入的任何其他内容都是新计算的一部分,并且必须显示在单独的行上

例如,用户点击:123.45+456.2=1”。屏幕应如下所示:

  • 123.45+ 由用户输入

  • 456.2= 由用户输入

  • 579.65 由您的程序计算和显示

这就是我希望计算器显示以前的输入并在单击数学运算符后转到新行的方式。请注意,我也尝试过追加,但没有成功。

这就是我的计算器的样子。

计算器:

http://img1.mukewang.com/61de932a000171b003830332.jpg

关于如何显示上面示例中的输出的任何帮助。



largeQ
浏览 381回答 3
3回答

喵喵时光机

只要它们是 0-9,您希望数字进入同一行,对吗?这会做到的。与其创建 10 个 JButton 对象,不如创建一个数组并使用循环初始化它们,并相应地向它们添加一个动作侦听器。private JButton[] buttons;buttons = new JButton[10];for(int i = 0; i < buttons.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; // Make all the buttons and add them to the panel&nbsp; &nbsp; &nbsp; &nbsp; panel1.add(buttons[i] = new JButton(String.valueOf(i)));&nbsp; &nbsp; &nbsp; &nbsp; // Add an actionlistener to each of them&nbsp; &nbsp; &nbsp; &nbsp; buttons[i].addActionListener(this);&nbsp; &nbsp; }以下是您如何为这些按钮使用 actionListener 接口(确保一开始就在 CalculatorFrame 类中实现它):@Overridepublic void actionPerformed(ActionEvent e) {&nbsp; &nbsp; for(int i = 0; i < buttons.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; // Check if the button pressed was a button0 - button 9&nbsp; &nbsp; &nbsp; &nbsp; if(e.getSource() == buttons[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // whichever button (0-9) was pressed, append its result into display string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display += String.valueOf(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Now set the result into your text area&nbsp; &nbsp; textArea.setText(display);}现在每次按下按钮时,它将在同一行而不是新的,因为您没有直接更改 textArea 的值,而是将一个字符串放入其中,每次按下时都会附加一个字符串按钮。所以最初,显示变量的值是什么。当你按下 1 时,它变为 1 并显示在 textArea 中。现在,当您按 2 时,显示的值变为 display = display + "2"。这一次,当您将显示变量传递给 textArea 时,它不仅会丢失值,因为它没有被直接编辑。您可以使用此逻辑来修复您的其他方法。此外,由于所有值都是字符串,因此要执行计算,您需要将字符串转换为整数。在这种情况下,您可以使用 Integer.valueOf(display)。希望这可以帮助。

翻过高山走不出你

要在上述操作后显示文本,您可以通过向 setText 方法添加必要的文本来实现:textArea.setText(display + "+ enter by user\n") 或类似的任何内容。但它不会帮助您获得结果,并且您会收到一条错误消息。为什么?因为您在读取第二个变量“equalTemp = Double.parseDouble(textArea.getText())”时遇到问题。事实上,此方法会取出 textArea 中显示的所有值/字符/等,因此会给您一条错误消息,因为无法将它们全部转换为 Double 格式。要解决此问题,您必须更改输入值的保存方式。例如,您可以将 textArea 中的所有文本转换为字符串,然后使用特定符号将其拆分(),然后将剩余的值保存为 Double 值。import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.util.regex.PatternSyntaxException;class CalculatorFrame extends JFrame {/**&nbsp;* All the buttons that will be used in the calculator have been initialized&nbsp;&nbsp;*/private JButton button1;private JButton button2;&nbsp;private JButton button3;private JButton button4;private JButton button5;private JButton button6;&nbsp;private JButton button7;private JButton button8;private JButton button9;private JButton button0;&nbsp;private JButton buttonEqual;private JButton buttonDot;private JButton buttonClearLast;private JButton buttonClearAll;private JButton buttonAdd;private JButton buttonSub;private JButton buttonMul;private JButton buttonDiv;private JTextArea textArea;&nbsp;private JScrollPane scrollPane;String display = "";String[] arr;private double result;Boolean additionBoolean = false;Boolean subtractionBoolean = false;Boolean multiplicationBoolean = false;Boolean divisionBoolean = false;Boolean equals = false;public CalculatorFrame(){&nbsp; &nbsp; JPanel panel2 = new JPanel();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; panel2.setLayout(new GridLayout(1,1));&nbsp; &nbsp; panel2.add(buttonClearLast = new JButton ("Clear Last"));&nbsp; &nbsp; panel2.add(buttonClearAll = new JButton ("Clear All"));&nbsp; &nbsp; add(panel2, BorderLayout.PAGE_START);&nbsp; &nbsp; JPanel panel3 = new JPanel();&nbsp; &nbsp; textArea = new JTextArea(10, 20);&nbsp; &nbsp; scrollPane = new JScrollPane(textArea);scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);&nbsp; &nbsp; add(scrollPane);&nbsp; &nbsp; add(panel3, BorderLayout.AFTER_LAST_LINE);&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; JPanel panel1 = new JPanel();&nbsp; &nbsp; panel1.setLayout(new GridLayout(4,4));&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; panel1.add(button7 = new JButton ("7"));&nbsp; &nbsp; panel1.add(button8 = new JButton ("8"));&nbsp; &nbsp; panel1.add(button9 = new JButton ("9"));&nbsp; &nbsp; panel1.add(buttonAdd = new JButton ("+"));&nbsp; &nbsp; panel1.add(button4 = new JButton ("4"));&nbsp; &nbsp; panel1.add(button5 = new JButton ("5"));&nbsp; &nbsp; panel1.add(button6 = new JButton ("6"));&nbsp; &nbsp; panel1.add(buttonSub = new JButton ("-"));&nbsp; &nbsp; panel1.add(button1 = new JButton ("1"));&nbsp; &nbsp; panel1.add(button2 = new JButton ("2"));&nbsp; &nbsp; panel1.add(button3 = new JButton ("3"));&nbsp; &nbsp; panel1.add(buttonMul = new JButton ("*"));&nbsp; &nbsp; panel1.add(button0 = new JButton ("0"));&nbsp; &nbsp; panel1.add(buttonDot = new JButton ("."));&nbsp; &nbsp; panel1.add(buttonEqual = new JButton ("="));&nbsp; &nbsp; panel1.add(buttonDiv = new JButton ("/"));&nbsp;&nbsp;&nbsp; &nbsp; add(panel1, BorderLayout.PAGE_END);&nbsp; &nbsp; pack();//&nbsp; buttonClearLast.addActionListener(new ListenToClearLast());&nbsp; &nbsp; buttonClearAll.addActionListener(new ListenToClearAll());&nbsp; &nbsp; button1.addActionListener(e -> {textArea.setText(display() + "1");});&nbsp; &nbsp; button2.addActionListener(e -> {textArea.setText(display() + "2");});&nbsp; &nbsp; button3.addActionListener(e -> {textArea.setText(display() + "3");});&nbsp; &nbsp; button4.addActionListener(e -> {textArea.setText(display() + "4");});&nbsp; &nbsp; button5.addActionListener(e -> {textArea.setText(display() + "5");});&nbsp; &nbsp; button6.addActionListener(e -> {textArea.setText(display() + "6");});&nbsp; &nbsp; button7.addActionListener(e -> {textArea.setText(display() + "7");});&nbsp; &nbsp; button8.addActionListener(e -> {textArea.setText(display() + "8");});&nbsp; &nbsp; button9.addActionListener(e -> {textArea.setText(display() + "9");});&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; button0.addActionListener(e -> {textArea.setText(display() + "0");});&nbsp; &nbsp; buttonAdd.addActionListener(e -> {textArea.setText(display() + "+ enterd by user\n"); additionBoolean = true;});&nbsp; &nbsp; buttonSub.addActionListener(e -> {textArea.setText(display() + "- enterd by user\n"); subtractionBoolean = true;});&nbsp; &nbsp; buttonMul.addActionListener(e -> {textArea.setText(display() + "* enterd by user\n"); multiplicationBoolean = true;});&nbsp; &nbsp; buttonDiv.addActionListener(e -> {textArea.setText(display() + "/ enterd by user\n"); divisionBoolean = true;});&nbsp; &nbsp; buttonDot.addActionListener(e -> {textArea.setText(display() + ".");});&nbsp; &nbsp; buttonEqual.addActionListener(e -> {calculation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textArea.setText(display() + "= enterd by user\n" + result + " this is your result");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp;}private String display() {&nbsp; &nbsp; display = textArea.getText();&nbsp; &nbsp; return display;&nbsp; &nbsp; }private void calculation() {&nbsp; &nbsp; String str = display();&nbsp; &nbsp; if (additionBoolean == true) {&nbsp; &nbsp; &nbsp; &nbsp; arr = str.split("\\+ enterd by user");&nbsp; &nbsp; &nbsp; &nbsp; result = Double.parseDouble(arr[0]) + Double.parseDouble(arr[1]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; else if (subtractionBoolean == true) {&nbsp; &nbsp; &nbsp; &nbsp; arr = str.split("- enterd by user");&nbsp; &nbsp; &nbsp; &nbsp; result = Double.parseDouble(arr[0]) - Double.parseDouble(arr[1]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; else if (multiplicationBoolean == true) {&nbsp; &nbsp; &nbsp; &nbsp; arr = str.split("\\* enterd by user");&nbsp; &nbsp; &nbsp; &nbsp; result = Double.parseDouble(arr[0]) * Double.parseDouble(arr[1]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; else if (divisionBoolean == true) {&nbsp; &nbsp; &nbsp; &nbsp; arr = str.split("/ enterd by user");&nbsp; &nbsp; &nbsp; &nbsp; result = Double.parseDouble(arr[0]) / Double.parseDouble(arr[1]);&nbsp; &nbsp; &nbsp; &nbsp; }}/**&nbsp;* This is where the action listener listens to all the button being pressed&nbsp;* Once heard, it will show case it to the TextArea of the calculator.&nbsp;&nbsp;*/public class ListenToClearAll implements ActionListener{&nbsp; &nbsp; public void actionPerformed (ActionEvent e){&nbsp; &nbsp; &nbsp; &nbsp; textArea.setText("");&nbsp; &nbsp; &nbsp; &nbsp; additionBoolean = false;&nbsp; &nbsp; &nbsp; &nbsp; subtractionBoolean = false;&nbsp; &nbsp; &nbsp; &nbsp; multiplicationBoolean = false;&nbsp; &nbsp; &nbsp; &nbsp; divisionBoolean = false;&nbsp; &nbsp; }}}&nbsp;例如,您将得到总和为 9.25 和 23.7 的下一个结果:9.25+ enterd by user23.7= enterd by user32.95 this is your result

至尊宝的传说

display&nbsp;=&nbsp;textArea.getText(); textArea.setText(display&nbsp;+&nbsp;"3");不要使用 getText() 和 setText()。它不是很有效。每次使用 getText() 时,文本区域都需要解析 Document 以创建字符串。每次使用 setText() 时,文本区域都需要解析字符串以创建文档。相反,您只需使用:textArea.append(&nbsp;"3"&nbsp;);更好的是,不要为每个按钮使用自定义侦听器。您可以为数字按钮共享一个通用侦听器。请参阅:如何在 java 中为 jbutton 添加快捷键?举个例子让你开始。我希望计算器显示以前的输入并在单击数学运算符后转到新行。然后,附加的 ActionListener (例如)将执行以下操作:textArea.append(&nbsp;"+\n"&nbsp;);您需要为所有运营商执行此操作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java