IO流到JPanel,带有InputStream的GUI和JPanels JTextField和JTextArea中的OutputStream

时间:2013-03-06 23:31:33

标签: user-interface inputstream jtextfield jtextarea outputstream

我正在尝试编写一个GUI,用户在JTextArea中看到System输出,并将输入写入JTextField,这两个对象都在JPanel中。 如何将系统输出流连接到JTextArea和将系统输入流连接到JTextField?我用Google搜索并搜索了这些论坛,但他们找到了解决方案。如果有人可以帮助我,我会很高兴。

我有一个使用GUI调用JPanel的Master类,稍后我将在不同的线程中执行工作,但是现在我很难解决将IO流连接到JPanel的基本问题。下面是两个类:

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

}

1 个答案:

答案 0 :(得分: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());}
        }
    });