简体   繁体   English

如何清除jtextarea中的文本,然后继续附加其他文本(使用相同的按钮)?

[英]How to clear text from a jtextarea, and then keep appending other text (with the same button)?

I am trying to develop a simple calculator, where when the user keeps on clicking numbers, they appear on the textarea. 我正在尝试开发一个简单的计算器,当用户不断单击数字时,它们会出现在文本区域中。 If they click an operator ("+", "-", "*", etc), the number that was on the textarea, and the operator they click, they get saved on a number and operator variables respectively. 如果他们单击一个运算符(“ +”,“-”,“ *”等),文本区域上的数字以及他们单击的运算符,则会分别保存在数字和运算符变量上。 When the user clicks another number after clicking the operator, it should remove the number that was at the jtextarea, and write the new number on it on it, so that I can store it on a second number variable and finally make an operation with both numbers. 当用户在单击运算符后单击另一个数字时,应删除jtextarea上的数字,并在其上写入新数字,以便我可以将其存储在第二个数字变量中,并最终对两个数字进行操作数字。

The problem I'm facing, is that when the user clicks a number for the second time, ie, after clicking the operator, I can't find a way to clear what was on the textarea, and also keep appending the numbers being clicked. 我面临的问题是,当用户第二次单击数字时,即单击操作员后,我找不到清除文本区域上内容的方法,并且还不断添加被单击的数字。 If I put a code to clear the textarea and the append a number, all it does, is clear the previous number, and write ONLY ONE NUMBER , for instance, if I wanted to write "857" It can only write "8", and if I click "5", "8" gets cleared and "5" is written. 如果我放一个代码来清除文本区域并附加一个数字,它所做的就是清除前一个数字,并一个数字 ,例如,如果我想写“ 857”,它只能写“ 8”,如果单击“ 5”,则清除“ 8”并写入“ 5”。

I understand and get why it doesn't work, because the clear and the append codes happen each time a user clicks a number. 我理解并理解为什么它不起作用,因为每次用户单击数字时,都会出现明码和附加代码。 So I wanted to know if there is a way around that, and also if it is possible to: clear the text, and then keep appending a text, ie, the second time a user clicks a number, it should append the number on the screen, not clear it. 因此,我想知道是否可以解决此问题,以及是否可以:清除文本,然后继续添加文本,即,用户第二次单击数字时,应将数字附加在屏幕,看不清。

I want to approach my answer to this problem with two concepts in program design: the first is the design dependence on an external feature, and the second is a state machine. 我想用程序设计中的两个概念来解决这个问题:第一个是设计对外部功能的依赖,第二个是状态机。

I'm making some guesses and assumptions here, but from your description it appears that you have fastened onto the "append" feature of a text area and are now programming so that you can use it in a couple of situations: adding a number to the number on the display, and putting a new number in the display ('appending' to an empty display). 我在这里做出一些猜测和假设,但是从您的描述看来,您已经固定到文本区域的“附加”功能,并且正在编程,以便可以在以下两种情况下使用它:显示屏上的数字,然后在显示屏上放置一个新的数字(“追加”到空白显示屏)。

That is a nice feature of textarea, it might be useful for your program, but you don't have to use it here. 这是textarea的一个不错的功能,它可能对您的程序很有用,但是您不必在这里使用它。

However you solve this problem, you have a 'state' to save -- when the user presses a key, the thing the program does depends on its state. 但是,只要解决了此问题,就可以保存一个“状态”,即当用户按下某个键时,程序执行的操作取决于其状态。 You have to be able to discern what state it's in so that you can program the response to the keypress correctly. 您必须能够辨别它处于什么状态,以便可以正确地编程对按键的响应。

So let me suggest this: have a variable that stores what you want in the display -- all of it. 因此,我建议您这样做:有一个变量可以存储您想要的显示内容-全部。 When someone presses a key, discern your state in whatever fashion and update that variable with what is supposed to be in the display. 当有人按下某个键时,以任何方式识别您的状态,并使用应该在显示中显示的内容更新该变量。 When you're done processing, set the text area to that variable's value. 完成处理后,将文本区域设置为该变量的值。

Note that this allows the program flow -- receive input, process, display -- to be the same in all cases. 请注意,这使程序流程(接收输入,处理,显示)在所有情况下都相同。 No part of the program has to figure out whether to 'clear' the display. 程序的任何部分都不必弄清楚是否要“清除”显示内容。 Because of that, there is less complexity in the state you are saving. 因此,您要保存的状态的复杂性降低了。

If your state is "accumulating numbers for first operand", and a number is pressed, then you append the number pressed to the string being displayed. 如果您的状态是“为第一个操作数累加数字”,并按下了一个数字,则将所按下的数字附加到显示的字符串上。

If your state is "accumulating..." and an operator is pressed, append the operator. 如果您的状态为“正在累积...”并且按下了运算符,请追加该运算符。

If your state is "operator just pressed" and another number is pressed, set the variable to the number pressed. 如果您的状态是“刚按下操作员”并且按下了另一个数字,则将变量设置为按下的数字。

and so forth. 等等。

These may or may not be the correct states to use, I just have them here as simple examples. 这些可能是正确的状态,也可能不是正确的状态,这里仅作为简单示例。 But done in some similar way, I think you'll find your program gets simpler to write and to understand -- you remove the code that interacts between the 'append' feature and the program's state, which was making your code more complicated than it needed to be. 但是以类似的方式进行操作,我认为您会发现程序变得更易于编写和理解-删除了“附加”功能与程序状态之间相互作用的代码,这使您的代码比它复杂需要。

It is very common, I've found, for programmers (including myself) to get attached to some implementation that we then work around -- in fact, we are always working around implementations to some extent, that's what we do. 我发现,对于程序员(包括我自己)来说,附加到我们随后要解决的某些实现上是很常见的-实际上,我们在某种程度上一直在围绕实现进行工作,这就是我们所做的。 But sometimes if we take a step back, we can see that we are doing more programming working around some implementation detail that we can eliminate, and by eliminating it we can simplify our solution. 但是有时候,如果我们退后一步,我们可以看到我们正在围绕一些我们可以消除的实现细节进行更多的编程,并且通过消除实现细节可以简化我们的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM