0

これが、JTextFieldにリダイレクトされるSystem.inを取得した私の問題です。現在、ユーザーは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();
    }
}
}

あなたがより多くの情報を必要とするならば、コメントで尋ねてください!ご協力ありがとうございました

PS:アクションリスナーをドキュメントリスナーに変更しようとしましたが、常にイベントが発生するとは限らないため、希望どおりに動作しませんでした。

ロボットで試しましたが、テキストフィールドにフォーカスがないようです。キーを押すだけで何も起こりません。

//************************************************************************
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("");
}
//************************************************************************  
4

1 に答える 1

2

これが役立つかどうかわからない。しかし、あなたはロボットのクラスを試しましたか?

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

Enterキーの押下をシミュレートします。

これは役に立ちますか?

于 2012-05-25T15:39:48.230 に答える