我正在使用Jcrafts JSch Library 构建终端应用程序。
它工作正常并且按照它说的做,但是我必须将系统重定向到各种摆动组件,以便它在 GUI 应用程序中工作。
我注意到 System.in 重定向它依赖于按键将文本提交到 System.in,这很好,直到您需要使用 CRTL 键。也向上箭头表示以前的命令。
System.in 重定向和 KeyListener 提交的类
public class TextAreaStream extends InputStream implements KeyListener {
private JTextArea ta;
private String str = null;
private int pos = 0;
public TextAreaStream(JTextArea jtf) {
ta = jtf;
}
//gets triggered everytime that "Enter" is pressed on the textfield
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_ENTER)
{
str = ta.getText() + "\n";
pos = 0;
ta.setText("");
synchronized (this) {
this.notifyAll();
}
}
}
@Override
public int read() {
if(str != null && pos == str.length()){
str =null;
return java.io.StreamTokenizer.TT_EOF;
}
while (str == null || pos >= str.length()) {
try {
synchronized (this) {
this.wait();
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
return str.charAt(pos++);
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
我的问题是“有没有办法让 System.in 使用 JSch 识别向上箭头和 CRTL- 命令,或者有没有更好的方法在 Java Swing 应用程序中显示控制台屏幕?”
实施
//System input redirection from TextArea
TextAreaStream ts = new TextAreaStream(textAreaInput);
textAreaInput.addKeyListener(ts);
System.setIn(ts);