2012-02-11 26 views
1

嘿,我正在使用Swing和Apache Commons制作终端应用程序。我能够很容易地将System.outSystem.err重定向到JTextArea,但我该怎么做System.in?我需要覆盖Inputstream方法吗?我是否需要将StringJTextArea转换为字节数组,然后将它传递给InputStream?代码示例会很好。重定向System.in到摆动组件

回答

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(),但我的示例仅包含最低限度使其与BufferedReaderreadLine()函数配合使用。

编辑:对于这一个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。

+0

Cool Ill只需更改它,以便它占用JTextArea的最后一行。非常感谢 – Giannis 2012-02-11 21:51:04

+0

@latusaki,因为'JTextArea'不会生成'ActionEvent'afaik,所以不需要修改它。你可以创建一个不相关的按钮并添加动作侦听器,或者重写'JTextArea'并捕获键盘事件以查找'Ctrl-Enter'或类似的东西,然后自己触发'ActionEvent'。 – PeterT 2012-02-11 21:58:40

+0

在使用文本字段进行测试时,读取方法不会因某种原因而被调用。 – Giannis 2012-02-11 22:36:21

1

您需要创建自己的实现的InputStream这需要从任何你想要的Swing组件的输入...基本上有您复制文本从您的Swing组件的缓冲区,并且充当用于InputStream源(其中如果没有输入可用,则需要阻止课程)。