无法在同一课程的文本区域打印?

时间:2014-10-20 12:00:11

标签: java textarea

我想在名为arena的文本区域中打印。我从同一个类调用append方法到目前为止没有运气。如何在文本区域中打印字符串?

public class Chat_window extends javax.swing.JFrame implements Runnable {

    public static DataInputStream in = null;
    public static PrintStream out = null;
    private static Socket cs = null;
    private static BufferedReader zz;
    private static boolean alive = true;

    public Chat_window() {    
        initComponents();
    }

    public static void main(String args[]) {
          java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
               Chat_window w=new Chat_window();
                w.setVisible(true);

            }
        });
          try {
            cs = new Socket("localhost", 3333);
            out = new PrintStream(cs.getOutputStream());
            zz = new BufferedReader(new InputStreamReader(System.in));
            in = new DataInputStream(cs.getInputStream());
        } catch (Exception ex) {
            System.out.println(ex);
        }
        if (cs != null && out != null && in != null) {
            try {
                new Thread(new Chat_window()).start();
                while (alive) {
                    out.println(zz.readLine().trim());                 
                }
                out.close();
                in.close();
                cs.close();
            } catch (Exception error) {
                System.out.println(error);

            }
        }      
    }
   @Override

    public void run() {      
        try {
            while ((m = in.readLine()) != null) {             
                **arena.append(m);**             //This line doesnt work             
                System.out.println(m);               
            }         
        } catch (Exception e) {
            System.out.println(e);
        }    
    }

    // Variables declaration                      
    private java.awt.TextArea arena;

    private java.awt.TextArea textArea2;
    private java.awt.TextArea usrchat;
    // End of variables declaration                   

}

1 个答案:

答案 0 :(得分:0)

尝试将其替换为arena.setText(arena.getText()+"\n"+m);

或者 arena.setText(arena.getText()+m); 如果您不想在其间添加新行:)