java - 是否可以在 JTextfield 上虚拟地抛出 KeyEvent?

标签 java swing inputstream jtextfield keyevent

这是我的问题,我的 System.in 被重定向到 JTextField。现在用户可以按 Enter 键,它会将我的文本发送出去。但我的客户无法访问此 JTextfield,因此我想知道是否可以在代码中重新创建 Enter 键。

   public static JTextField jtfEntree  = new JTextField();
   public static TexfFieldStreamer ts = new TexfFieldStreamer(jtfEntree); 
   System.setIn(ts); 
   jtfEntree.addActionListener(ts);

    //************************************************************************
    private void commandLaunch(String command)               
    //************************************************************************
    {
        jtfEntree.setText(command);
        //here is where i want to fire the key Enter
    }
    //************************************************************************


class TexfFieldStreamer extends InputStream implements ActionListener{

private JTextField tf;
private String str = null;
private int pos = 0;

public TexfFieldStreamer(JTextField jtf) {
    tf = jtf;
}

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++);
}


public void actionPerformed(ActionEvent arg0)
{
    // TODO Auto-generated method stub
    str = tf.getText() + "\n";
    pos = 0;
    synchronized (this) {
        //maybe this should only notify() as multiple threads may
        //be waiting for input and they would now race for input
    this.notify();
    }
}
}

如果您需要更多信息,请在评论中询问! 谢谢你的帮助

P.S.:我确实尝试将操作监听器更改为文档监听器,但它并不总是触发事件,因此它的行为不像我想要的那样。

尝试使用机器人,但似乎文本字段没有获得焦点,因此只是按下了键,但没有任何反应

//************************************************************************
protected static void commandExecute(String Command)     //COMMAND EXECUTE
//************************************************************************
{
    jtfEntree.setText(Command);
    jtfEntree.requestFocus();
    Robot robot;
    try {
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_ENTER);
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    jtfEntree.setText("");
}
//************************************************************************  

最佳答案

不知道这是否有帮助。但是你尝试过机器人类(class)吗?

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);

将模拟按 Enter 键。

这有帮助吗?

关于java - 是否可以在 JTextfield 上虚拟地抛出 KeyEvent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10757227/

相关文章:

java - 嵌套类无法解析为类型

java - 如何根据 jSpinner 中超出最小/最大范围的文本字段的无效手动编辑来触发事件

java - 让 JButton 关闭窗口,但在关闭之前在 WindowClosing(WindowEvent e) 中运行代码

java - 我如何决定从输入流中读取多少字节?

java - 使用 Java 读取受密码保护的 Excel 文件(.xlsx)

java - 是否有可能在 Android 蓝牙和笔记本电脑蓝牙设备之间建立不安全的连接?

java - 如何从此哈希表获取信息

java - 具有多个图像的 Jtable 单个单元格

java - 从 InputStream 读取文本和二进制数据

android - 一次仅从mp3文件中解码一些PCM字节