2016-02-05 30 views
2
package client; 

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.URL; 
import java.net.UnknownHostException; 
import java.util.ResourceBundle; 
import javafx.concurrent.Task; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextArea; 
import javafx.scene.control.TextField; 

public class HomeController implements Initializable { 

    @FXML 
    private Button btn; 

    @FXML 
    private TextArea txtarea; 

    @FXML 
    private TextField txtmsg; 
    Socket sock; 
    DataInputStream di; 
    DataOutputStream dout; 

    public void initialize(URL location, ResourceBundle resources) { 

     try { 
      sock = new Socket(InetAddress.getByName("localhost"), 8081); 
      System.out.println("Client Started...."); 
      txtarea.setText("Client Started..."); 

      di = new DataInputStream(sock.getInputStream()); 
      dout = new DataOutputStream(sock.getOutputStream()); 

      btn.setOnAction(new EventHandler<ActionEvent>() { 

       public void handle(ActionEvent event) { 

        try { 
         dout.writeUTF(txtmsg.getText().trim()); 
         txtmsg.clear(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      }); 

      Task<Void> t = new Task<Void>() { 

       @Override 
       protected Void call() throws Exception { 
        // TODO Auto-generated method stub 

        String msg = di.readUTF().trim(); 
        System.out.println(msg); 
        updateMessage("\nServer says : " + msg); 
        txtarea.appendText("\nServer says : " + msg); 
        return null; 
       } 

      }; 
      new Thread(t).start(); 
      // Executors.newSingleThreadExecutor().submit(t); 


     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

} 

我在javaFX中創建聊天應用程序...上面的代碼是爲客戶端.. 當服務器發送一些消息時,它不會出現在客戶端textarea.houghough消息在控制檯中收到。爲什麼textarea在javaFx中沒有得到更新?

你能否給我提供一些解決方案?

回答

0

您正在更新不正確的後臺線程內的TextArea。您必須在Platform.runLater()內更新它,這將強制它在JavaFX應用程序線程上執行。

這是一個快速演示。注意:本例中我使用的是Service,因爲它是reusable Worker

import javafx.application.Application; 
import javafx.beans.binding.Bindings; 
import javafx.concurrent.Task; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextArea; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(Stage stage) { 
     Button btn = new Button("Append"); 
     TextArea txtarea = new TextArea("Hi "); 

     Service<Void> service = new Service<Void>() { 
      @Override 
      protected Task<Void> createTask() { 
       return new Task<Void>() { 
        @Override 
        protected Void call() throws Exception { 
         Platform.runLater(() -> txtarea.appendText("message ")); 
         return null; 
        } 
       }; 
      } 
     }; 

     btn.setOnAction(new EventHandler<ActionEvent>() { 
      public void handle(ActionEvent event) { 
       /* 
       *You can add conditions to check the status of 
       *the service and execute proper methods. 
       */ 
       service.restart(); 
      } 
     }); 

     VBox root = new VBox(txtarea, btn); 
     root.setAlignment(Pos.CENTER); 
     Scene scene = new Scene(root, 500, 500); 
     stage.setScene(scene); 
     stage.show(); 
    } 
} 
+0

好的,謝謝它的工作... bt我的文本區域沒有被追加...任何解決方案? –

+0

您可以使用'Bindings.concat',就像我在示例中所示。 – ItachiUchiha

+0

@IchichiUchiha這不適用於你按下按鈕的第二次和以後的時間。由於'txtArea.getText()'在計算綁定時立即返回當前文本,因此它總是將最後一條消息與原始文本連接起來。 (另外,由於對消息的更新是合併的,因此不保證以這種方式查看每條消息。)這裏需要使用'Platform.runLater()'。 –

相關問題