Java Swing,textarea 作为输入/输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/234587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:33:21  来源:igfitidea点击:

Java Swing, textarea as input/output?

javaswing

提问by Jean Paul Galea

I want to load up a window using the java swing, something like this

我想使用 java swing 加载一个窗口,就像这样

However I want to be able to use it for text input also not just to dump data into it..

但是我希望能够将它用于文本输入,而不仅仅是将数据转储到其中..

any idea how to do it?

知道怎么做吗?

回答by Paul Brinkley

JTextAreas are editable by default, so input is trivial. Just put one into a test UI and see for yourself.

JTextAreas 默认是可编辑的,因此输入很简单。只需将一个放入测试 UI 中并亲自查看即可。

From a design perspective, using the same JTextArea for both input and output strikes me as unwise. The only example I can think of is a shell interface, and that imposes stronger limits on how input and output works than a JTextArea will provide out of the box.

从设计的角度来看,对输入和输出使用相同的 JTextArea 使我感到不明智。我能想到的唯一示例是 shell 接口,它对输入和输出的工作方式施加了比 JTextArea 开箱即用的更强的限制。

回答by micro

I'm not sure if I got the point of your question, but you can get the text the user has typed in using the getText() method:

我不确定我是否明白您的问题,但是您可以使用 getText() 方法获取用户输入的文本:

JTextArea ta = new JTextArea(); String input = ta.getText();

JTextArea ta = new JTextArea(); 字符串输入 = ta.getText();

回答by Anon

Check out this Sun toturial:

看看这个Sun totutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/textarea.html

http://java.sun.com/docs/books/tutorial/uiswing/components/textarea.html

In particular, scroll down to the "TextAreaDemo" example.

特别是,向下滚动到“TextAreaDemo”示例。