IO流到JPanel,在JPanel的JTextField和JTextArea中带有输入流和输出流的GUI



我正在尝试编写一个GUI,其中用户在JTextArea中看到系统输出,并在JTextField中写入输入,这两个对象都在JPanel中。如何将系统输出流连接到JTextArea,将系统输入流连接到JTextField?我已经谷歌和搜索这些论坛,但还没有找到解决方案。如果有人能帮我,我会很高兴的。

我有一个Master类,它用GUI调用JPanel,稍后我将在不同的线程中执行工作,但现在我正在努力解决将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());
}

}

我是新的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());}
        }
    });

相关内容

  • 没有找到相关文章