嘿,我正在使用Swing和Apache Commons制作终端应用程序。我能够很容易地将System.out
和System.err
重定向到JTextArea
,但我该怎么做System.in
?我需要覆盖Inputstream
方法吗?我是否需要将String
从JTextArea
转换为字节数组,然后将它传递给InputStream
?代码示例会很好。重定向System.in到摆动组件
1
A
回答
4
我最近尝试同样的事情,这是我的代码:
class TexfFieldStreamer extends InputStream implements ActionListener {
private JTextField tf;
private String str = null;
private int pos = 0;
public TexfFieldStreamer(JTextField jtf) {
tf = jtf;
}
//gets triggered everytime that "Enter" is pressed on the textfield
@Override
public void actionPerformed(ActionEvent e) {
str = tf.getText() + "\n";
pos = 0;
tf.setText("");
synchronized (this) {
//maybe this should only notify() as multiple threads may
//be waiting for input and they would now race for input
this.notifyAll();
}
}
@Override
public int read() {
//test if the available input has reached its end
//and the EOS should be returned
if(str != null && pos == str.length()){
str =null;
//this is supposed to return -1 on "end of stream"
//but I'm having a hard time locating the constant
return java.io.StreamTokenizer.TT_EOF;
}
//no input available, block until more is available because that's
//the behavior specified in the Javadocs
while (str == null || pos >= str.length()) {
try {
//according to the docs read() should block until new input is available
synchronized (this) {
this.wait();
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
//read an additional character, return it and increment the index
return str.charAt(pos++);
}
}
,并使用它像这样:
JTextField tf = new JTextField();
TextFieldStreamer ts = new TextFieldStreamer(tf);
//maybe this next line should be done in the TextFieldStreamer ctor
//but that would cause a "leak a this from the ctor" warning
tf.addActionListener(ts);
System.setIn(ts);
已经有一段时间,因为我编写的Java,所以我可能不会上调最新的模式。您应该也可以重载int available()
,但我的示例仅包含最低限度使其与BufferedReader
的readLine()
函数配合使用。
编辑:对于这一个JTextField
必须使用implements KeyListener
而不是implements ActionListener
,然后在你的文本区使用addKeyListener(...)
工作。你需要的actionPerformed(...)
,而不是功能是public void keyPressed(KeyEvent e)
,然后你要测试if (e.getKeyCode() == e.VK_ENTER)
和而不是使用你的光标之前只使用一个子从最后一行的全部文本与
//ignores the special case of an empty line
//so a test for \n before the Caret or the Caret still being at 0 is necessary
int endpos = tf.getCaret().getMark();
int startpos = tf.getText().substring(0, endpos-1).lastIndexOf('\n')+1;
的输入字符串。因为否则,每次按Enter时都会读取整个TextArea。
1
您需要创建自己的实现的InputStream
这需要从任何你想要的Swing组件的输入...基本上有您复制文本从您的Swing组件的缓冲区,并且充当用于InputStream
源(其中如果没有输入可用,则需要阻止课程)。
相关问题
- 1. 如何将System.in重定向到JavaFX TextField?
- 2. 摆动重叠组件
- 3. 摆动定制组件dummylistener
- 4. 摆动自定义组件
- 5. 摆脱301重定向
- 6. 向前摆动和向后摆动jQuery
- 7. 从webview重定向到ReactNative组件
- 8. 附加组件域重定向到HTTPS
- 9. 从后端重定向到angular2组件
- 10. 重置System.in/out
- 11. Java重定向System.in - InputStream.read调用,但没有“返回”
- 12. 301重定向以摆脱斜杠
- 13. 不显示摆动组件
- 14. 重定向到行动MVC
- 15. 从自定义摆动组件返回到父类的值
- 16. .htaccess重定向到文件
- 17. coredump重定向到文件
- 18. Fuelphp重定向到文件
- 19. .htaccess文件重定向到
- 20. Bash重定向到文件
- 21. 移动版本通过htaccess重定向:也重定向到PC?
- 22. ASP.NET MVC重定向到动作重定向循环
- 23. 登录后重新导航组件重定向到另一个组件Angular 2
- 24. 重定向组合
- 25. 如何使用react-router的重定向组件重定向到动态生成的路由?
- 26. 重定向到
- 27. 重定向到
- 28. 重定向到
- 29. 在摆动组布局手动添加自定义部件
- 30. 重定向到www .htaccess不重定向
Cool Ill只需更改它,以便它占用JTextArea的最后一行。非常感谢 – Giannis 2012-02-11 21:51:04
@latusaki,因为'JTextArea'不会生成'ActionEvent'afaik,所以不需要修改它。你可以创建一个不相关的按钮并添加动作侦听器,或者重写'JTextArea'并捕获键盘事件以查找'Ctrl-Enter'或类似的东西,然后自己触发'ActionEvent'。 – PeterT 2012-02-11 21:58:40
在使用文本字段进行测试时,读取方法不会因某种原因而被调用。 – Giannis 2012-02-11 22:36:21