简体   繁体   English

从Java JTextArea读取

[英]Reading from Java JTextArea

Okay, thus may seen kind of odd, but I wanted to get some suggestions from everyone here. 好的,因此可能看起来有些奇怪,但是我想从这里的所有人那里获得一些建议。 I am a beginning Java developer (after 2 years of ASP.NET web development) and I have recently began working on my first Java project - a calculator. 我是一名新手Java开发人员(经过2年的ASP.NET Web开发),最近我开始从事我的第一个Java项目-计算器。 I realize that their are tons of calculators out there, but I thought it would be a good beginner project. 我意识到他们那里有很多计算器,但是我认为这将是一个不错的初学者项目。

Anyway, here is what I need help with. 无论如何,这是我需要帮助的地方。 Currently, I am using a Scrolling JTextArea for display (instead of a simple JTextField) that is approximately 5 rows tall. 当前,我正在使用大约5行高的Scrolling JTextArea进行显示(而不是简单的JTextField)。 I want the user to be able to scroll through the list to see previous entries and such. 我希望用户能够滚动列表以查看以前的条目等。 The format of the box will be equation on one line and the program will generate the answer on the next and so on. 框的格式将在一行上是等式,而程序将在下一行上生成答案,依此类推。

My real question is, how is the best way to implement this? 我的真正问题是,实现此目标的最佳方法是什么? My fist idea was to read through the JTextArea when equals is pressed, down to the last line and try to search that line for the operator (+, -, etc.) and the operands. 我的第一个想法是在按下equals时通读JTextArea,直到最后一行,并尝试在该行中搜索运算符(+,-等)和操作数。 Is this the best way to go about this? 这是解决此问题的最佳方法吗? Although, this would work would work, I think it could get cumbersome and sounds very inefficient. 尽管这行得通,但我认为这样做可能很麻烦并且听起来效率很低。 I am open to any suggestions, even possibly replacing the JTextArea is some other component would work better. 我乐于接受任何建议,甚至可能替换JTextArea还是其他一些组件会更好。

Thanks! 谢谢!

There's no need to read through the JTextArea contents - use JTextArea.append() to add to the end. 无需通读JTextArea内容-使用JTextArea.append()添加到末尾。 Here are some examples of JTextArea content manipulation: 以下是JTextArea内容操作的一些示例:

JTextArea ta = new JTextArea("Initial Text");

// Insert some text at the beginning
int pos = 0;
ta.insert("some text", pos);

// Insert some text after the 5th character
pos = 5;
ta.insert("some text", pos);

// Append some text
ta.append("some text");

// Replace the first 3 characters with some text
int start = 0;
int end = 3;
ta.replaceRange("new text", start, end);

// Delete the first 5 characters
start = 0;
end = 5;
ta.replaceRange(null, start, end);

If you are open to different interfaces, you might want to try something like a JTextField at the top of your view, from which you can receive as input your 'new' inputted equation, and then below it with the same width a JList that would scroll to have all of the previous equations and their results. 如果您对不同的接口开放,则可能要尝试在视图顶部尝试使用类似JTextField的方法,从中可以接收“新”输入方程式的输入,然后在其下方以相同的宽度包含JList滚动以获取所有先前的方程式及其结果。 That would make parsing of the current formula much easier, and you would also have an easy time of keeping your previous formula and their results in a scrollable list, with the easy option of keeping the most recent on top. 这将使当前公式的解析变得更加容易,并且您还可以轻松地将以前的公式及其结果保存在可滚动的列表中,并且可以轻松选择将最新的公式放在最前面。

your idea is interesting. 你的想法很有趣。 so you would have a line such as. 所以你会有这样的一行。

2+2 2 + 2

then when pressing calculate would add the line 然后按计算时将添加行

4 4

and so on then you could type in another equation. 依此类推,您可以输入另一个方程式。

it could work but as you said it wouldn't be the most efficient implementation... but that's just a tradeoff of getting the desired functionality. 它可能会起作用,但是正如您所说的,这并不是最有效的实现方式……但这只是获得所需功能的权衡。

If i were going to implement it the way you discribed (with a JTextArea) I'd use scanner, and scan the value string a line at a time. 如果我打算用您描述的方式(使用JTextArea)实现它,我将使用扫描仪,并一次一行扫描值字符串。

if the line has +/- in it then do the calculation and add both the original line and the answer to a string. 如果行中有+/-,则进行计算,并将原始行和答案都添加到字符串中。

the new string is the new value of the text field. 新字符串是文本字段的新值。

this method would get pretty cumbersom as you would be continually recalculating the users old entries more were added. 由于您将不断重新计算用户的旧条目,因此此方法将变得很麻烦。

I guess if you continually stored the last line of the document, when you run out of lines, calculate the last stored and append the answer, then it wouldn't be so bad. 我猜如果您连续存储文档的最后一行,当行数用尽时,计算最后存储的内容并追加答案,那还不错。


Here's what I would do: 这就是我要做的:

use a JTextField to enter in the calculations, and a JList to display the old ones and their answers. 使用JTextField输入计算结果,并使用JList显示旧计算及其答案。

You could treat each line as a single operation. 您可以将每行视为一个单独的操作。 That way you could use the String array returned directly by: 这样,您可以使用直接由以下方式返回的String数组:

String [] operations = textArea.getText().split("\n");

And then you'll know that exactly each one of them as a complete operation ( may be invalid, but that' another story ) 然后,您将知道它们中的每一个都作为一个完整的操作(可能是无效的,但这是另一回事了)

Is this what you asked or do I totally misread you? 这是您的要求吗?还是我完全误读了您?

I think a simpler solution would actually use two components. 我认为一个更简单的解决方案实际上将使用两个组件。 A TextArea to hold the "history" of what's happened so far, and a textfield where the user inputs new entries. 一个TextArea用来保存到目前为止发生的事情的“历史”,还有一个Textfield用户可以在其中输入新条目的地方。

Thanks to everyone who replied. 感谢所有答复。 You all gave me some ideas to think about. 你们都给了我一些想法。 I think right now, I am going to go with my original idea of using a single JTextArea and try to find ways to optimize the process. 我想现在,我将沿用最初的想法,即使用单个JTextArea并尝试找到优化流程的方法。 If that gets too difficult (which is very possible), I will follow the majority's advice and use two separate fields. 如果这太困难了(这很有可能),我将遵循多数意见并使用两个单独的字段。 Thanks for replying everyone! 感谢大家的回覆!

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

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