JTextArea 的可能替代方案吗?
我有这个程序,它通过 JOptionPane 接受用户的输入。我的程序正在做的是记录我从 JTextArea 中的 JOptionPane 输入的所有内容。现在,我想删除 JTextArea 中的一些输入,但我不知道如何操作。
我想一定有像 JList 这样的东西我可以使用而不是 JTextArea。但是,我对 JList 不太熟悉,它使用数组。我真的不知道数组 D:
感谢任何可以提供帮助的人!
I have this program which accepts input from the user through JOptionPane. What my program is doing is recording everything I input from the JOptionPane in the JTextArea. Now, I want to remove some of my input in the JTextArea but I don't know how.
I'm thinking that there must be something like a JList that I can use instead of a JTextArea. However, I'm not too familiar with JList and it uses arrays. I don't really know arrays D:
Thanks to anyone who can help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数组是编程中最基本的结构之一,我强烈建议尝试使用这些结构,因为这似乎是解决您的问题的最佳解决方案。 官方 Java 教程 还不错,但它是有点干。
基本上,数组是为了您的方便而存在的。它允许您在一行中声明一组变量的空间,而不是:
可以用一条语句声明所有一百个变量的空间:
并使用语法
var[1]、
var[2]
等。当您需要对数组的每个成员执行某些操作(例如初始化)时,它也会有所帮助。与其在自己的语句中初始化每个变量,如下所示:
相反,可以循环整个数组并使用单个语句来更新数组中的每个变量,如下所示:
它不仅节省了代码量,但是,如果需要更改变量的数量,这将更容易维护。
Arrays are one of the most fundamental constructs in programming, and I'd highly suggest experimenting with those as it does seem like this would be the best solution to your problem. The official Java tutorial is not too too bad, but it is a little dry.
Basically, arrays are there for your convenience. It allows you to declare the space for a set of variables in one line, so rather than:
One can declare the space for all one hundred variables with a single statement:
And reference them using the syntax
var[1]
,var[2]
, etc, instead. It also helps when you need to do something to each of the members of the array, say, initializing.Rather than initializing each in its own statement, like so:
Instead, one can loop over the whole array and use a single statement to update each of the variables in the array, like so:
Not only does it save on the amount of code, but this will be much easier to maintain if, say, one needs to change the number of variables.
不,它使用
ListModel
。查看
DefaultListModel
。您可以从模型中动态添加/删除元素。No, it uses a
ListModel
.Look at the
DefaultListModel
. You can dynamically add/remove elements from the model.