私は取る方法があるかどうか知りたかったString
-言いましょう:
String str = "blabla";
そして、やります:
System.in.setText(str);
私はこれが機能しないことを知っています-これを行う方法があるかどうか知りたかったのです。そして、同じ文字列を送信します。コンソールに書き込んで。を押すのと同じようにEnter。
String
これはサーバーソケットを備えたプログラムであり、他のアプリケーションがそれをどう処理するかを認識できるように、ポートを介して送信しようとしています。
編集: ユーザーがSystem.inを介して送信するテキストフィールドにユーザーが書き込むときに、入力ストリームをテキストフィールドにリダイレクトする方法を見つけました。
import java.io.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class TextfieldInputStream extends InputStream implements DocumentListener {
private JTextField tf;
private String str = null;
private int pos = 0;
public TextfieldInputStream(JTextField jtf) {
tf = jtf;
}
@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++);
}
public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}
public void insertUpdate(DocumentEvent e){
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.notifyAll();
}
}
public void removeUpdate(DocumentEvent e){
// TODO Auto-generated method stub
}
}