我在netbeans中創建了一個服務器線程類,並使用netbeans swing自動生成的Jframe創建了一個調用該類的GUI應用程序。我想從線程類的字符串值附加到我的JtextArea,但我的Jtextarea上顯示的值爲null。字符串不返回請幫助我。代碼示例如下從線程類追加字符串值到jtextArea
public class simpletestserver1 extends Thread {
String message,mess;
public void run(){
.
.//some coding here
.
.
DataOutputStream outToClient = new DataOutputStream(c.getOutputStream());
Scanner r = new Scanner(c.getInputStream());
outToClient.writeBytes(m+'\n');
mess=r.nextLine();
// THIS IS THE MESSAGE THAT NEEDS TO BE APPENDED TO
// MY JTEXTAREA IN MY JFRAME CLASS
現在我有另一個客戶端線程發送數據到服務器。當程序在另一個動作事件上運行時,服務器線程已經開始偵聽。現在,NN按下一個按鈕動作,我的客戶端線程開始發送數據到我的服務器和文本應添加到我的JTextArea JFrame類如下:
package sdiappgui;
import java.util.*;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import java.awt.Window;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SendEmail extends javax.swing.JFrame {
public SendEmail() {
initComponents();
}
. //some coding here for other generated components
.at this point my server thread has already started on a previously clicked button action and it is already listening ,i start my client thread and the data sent to server should be appended to my jtextarea
.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
final simpletestserver1 th1=new simpletestserver1();
final simpletestclient1 th2=new simpletestclient2();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
th2.start();
jTextArea2.append("received: " +th1.mess +'\n');
}
});
}
但是我JTextArea是沒有收到返回的字符串由客戶。當我運行程序時,jtextArea上顯示一個空值。請幫助我。
爲更好地幫助更快張貼[SSCCE(http://sscce.org/) – mKorbel
這[主題](http://stackoverflow.com/q/9240308/1057230),可能是一些幫助,關於給定的主題。 –