2014-10-20 63 views
-1

我想在名爲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     

} 
+0

我知道,這是有關創建重複文本區哪裏一個不可見,但如何使它在正文區域打印? – Parth 2014-10-20 12:04:45

+1

*不起作用*是什麼意思? – Dici 2014-10-20 12:06:35

+0

該行不打印任何文本區域 – Parth 2014-10-29 10:45:02

回答

0

嘗試與arena.setText(arena.getText()+"\n"+m);

或者 arena.setText(arena.getText()+m); 更換如果你不希望添加新的行之間:)

+0

感謝您的回覆,但它不起作用。只有當你選擇文本區域並按任意鍵時,文本區域纔會顯示。只顯示最後一行。我想打印它在控制檯上的打印方式。 – Parth 2014-10-29 10:55:51