0

ユーザーが JTextArea でシステム出力を表示し、JTextField で入力を書き込む GUI を作成しようとしています。どちらも JPanel 内のオブジェクトです。システム出力ストリームを JTextArea に接続し、システム入力ストリームを JTextField に接続するにはどうすればよいですか? これらのフォーラムをグーグルで検索しましたが、解決策が見つかりませんでした。誰かがこれを手伝ってくれたらとてもうれしいです。

GUI を使用して JPanel を呼び出すマスター クラスがあり、後で別のスレッドで作業を実行する予定ですが、現在、IO ストリームを JPanel に接続するという基本的な問題に苦労しています。以下は 2 つのクラスです。

public class MainTest {

public static void main(String[] args) throws IOException {

    JPanelOUT testpanel = new JPanelOUT();
    JFrame frame = new JFrame();
    frame.add(testpanel);
    frame.setVisible(true);
    frame.pack();

    /*
    System.setOut(CONVERT TEXTAREA TO AN OUTPUTSTREAM SOMEHOW??(JPanelOUT.textArea)));

    System.setIn(CONVERT STRING TO AN INPUTSTREAM SOMEHOW?? JPanelOUT.textField);
    */
    String text = Sreadinput();
    System.out.println(text);   
}

public static String Sreadinput() throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(JPanelOUT.is));
    String input=in.readLine();
    return input;
}

}

public class JPanelOUT extends JPanel implements ActionListener {
protected static JTextField textField;
protected static JTextArea textArea;
public static InputStream is;
private final static String newline = "\n";

public JPanelOUT() throws UnsupportedEncodingException, FileNotFoundException {
    super(new GridBagLayout());

    JLabel label1 = new JLabel("OUTPUT:");;
    JLabel label2 = new JLabel("INPUT:");;

    textField = new JTextField(20);
    textField.addActionListener(this);
    textArea = new JTextArea(10, 20);
    textArea.setEditable(false);
    textArea.setBackground(Color.black);
    textArea.setForeground(Color.white);
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setPreferredSize(new Dimension(500,200));

    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.fill = GridBagConstraints.HORIZONTAL;
    add(label1, c);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    add(scrollPane, c);
    c.weightx = 0;
    c.weighty = 0;
    c.fill = GridBagConstraints.HORIZONTAL;
    add(label2, c);
    c.fill = GridBagConstraints.HORIZONTAL;
    add(textField, c);

    String WelcomeText1 = "Hello and welcome to the TEST";
    String WelcomeText2 = "Trying to get the input field below to become the System.in and this output";
    String WelcomeText3 = "field to become the System.out (preferrably both with UTF-8 encoding where";
    String WelcomeText4 = "the scrollpane automatically scrolls down to the last output line)!";
    textArea.append(WelcomeText1 + newline + newline + WelcomeText2 + newline + WelcomeText3 + newline + WelcomeText4 + newline + newline);

    String text = textField.getText();
    is =new ByteArrayInputStream(text.getBytes("UTF-8"));
}

public void actionPerformed(ActionEvent evt) {
    String text2 = textField.getText();

    textArea.append(text2 + newline);
    textField.selectAll();
    textArea.setCaretPosition(textArea.getDocument().getLength());
}

}

4

1 に答える 1

0

私は Java が初めてで、ストリームも処理しようとしています :) 英語が下手で申し訳ありません。私はロシア出身です。このコードが役立つかもしれません。

public class NewJFrame extends javax.swing.JFrame {

/**
 * Creates new form NewJFrame
 */
public MyPrintStream myPrintStream;
public NewJFrame()throws FileNotFoundException{
    initComponents();
    this.myPrintStream = new MyPrintStream("string");
}
private class MyPrintStream extends PrintStream {        
    MyPrintStream(String str)throws FileNotFoundException{
        super(str);
    }
    public void println(String s){
        textArea1.append(s+'\n');
    }
} .. continuation class code

主な方法:

public static void main(String args[]){
    /* Set the Nimbus look and feel... */

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run(){
            try{
            NewJFrame myJFrame = new NewJFrame();
            myJFrame.setVisible(true);                    
            System.setOut(myJFrame.myPrintStream);
            System.out.println("its work");
            System.out.println("its work2");
            System.out.print("str"); //does not work, need to override
            }catch (FileNotFoundException e){System.out.println (e.getMessage());}
        }
    });
于 2013-06-01T20:07:41.243 に答える