JavaFx 2.0 FXML项目如何访问变量和项目结构

时间:2014-12-09 21:32:00

标签: javafx structure fxml

我正在javafx fxml项目中创建一个IRC客户端 我很难理解项目的结构。我得到了MVC模式但是我不知道我必须在哪里定位客户端的主代码部分(对象初始化和通信开始)。
我希望能够在服务器发送消息时使用来自serverConnection对象的String 更新textarea
那么我在哪里定位代码以及如何通知我的控制器它必须同时更新其文本传递字符串?

这是应用程序类:

package jircclient;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.concurrent.*;
public class JircClient extends Application
{   

@Override
public void start(Stage stage) throws Exception
{
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Scene scene = new Scene(root);
    stage.setTitle("jircClient");
    stage.setScene(scene);
    stage.show(); 
    System.out.println("Stage SET!");

    //This is where i thought that i could write the main part of the program
    //but the stage will not load unless the start method finishes
    //Creating a serverConnetion is not a problem since it's only an object
    //but starting the communication does not let the method we are in to exit
    //since it sticks in a while loop forever until i probably cancel it using a 
    //button (not implemented yet)
    //serverConnection server1 = new serverConnection();
    //server1.startCommunication();

}

public static void main(String[] args) throws Exception
{   
    //In this method i cannot go further unless i close the stage-window
    //The purpose of it is to launch the start method
    Application.launch(args);
}
}

这是控制器类:

package jircclient;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;

public class FXMLDocumentController implements Initializable
{  
@FXML
private TextArea txt;

@FXML
private void handleButtonAction(ActionEvent event)
{
    //In here i will be handling events, however i will need to have
    //access to serverConnection objects
    System.out.println("You clicked me!");
}

@Override
public void initialize(URL url, ResourceBundle rb)
{

}
}

这是serverConnection类:

package jircclient;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

public class serverConnection 
{
//VARIABLES
private clientInfo client;
private String server_to_connect = "someserver";
private String channel_to_connect = "#somechannel";
private String serv_resp;
private int port = 6667;
private Socket socket;
private BufferedWriter writer;
private BufferedReader reader;
private ArrayList<channel> channels;

//DEFAULT CONSTRUCTOR
public serverConnection() {client = new clientInfo("test1", "test1", "test1");}

//FULL CONSTRUCTOR
public serverConnection(String server_to_connect, int port, String channel_to_connect) throws IOException
{
    client = new clientInfo("test1", "test1", "test1");
    this.server_to_connect = server_to_connect;
    this.port = port;
    this.channel_to_connect = server_to_connect;

    try
    {    
        //Creating socket connection
        this.socket = new Socket(this.server_to_connect,port);

        //Socket output writer
        writer = new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream()));

        //Socket input writer
        reader = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));

        serv_resp = null;

        System.out.println("Connection established.");
    }
    catch(UnknownHostException exc)
    {
        System.out.println("ERROR: "+ exc.toString());
    }
    catch(IOException exc)
    {
        System.out.println("ERROR: "+ exc.toString());
    }
    finally
    {
        System.out.println("Closing connection.");
        socket.close();
    }
}

//server response getter
public String getServerResponse()
{
    return serv_resp;
}

//Introduction to server and listen
public void startCommunication() throws IOException
{

    this.socket = new Socket(this.server_to_connect,port);
    writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));  
    reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
    serv_resp = null;

    writer.write("NICK " + client.getClientNickname() + "\r\n");
    writer.write("USER " + client.getClientLogin() + " 0 * : " + 
    client.getClientRealName() + "\r\n");
    while ((serv_resp = reader.readLine()) != null)
    {
        System.out.println(serv_resp);
        //FXMLDocumentController.txt.setText(serv_resp);
        if (serv_resp.indexOf("004") >= 0)
        {
            break;
        } 
        else if (serv_resp.indexOf("433") >= 0)
        {
            System.out.println("Nickname is already in use.");
            return;
        }
    }

    //Get channel list
    writer.write("LIST \r\n");
    writer.flush();

    //Join desired client
    writer.write("JOIN " + channel_to_connect + "\r\n");
    writer.flush();


    //keep listening
    while ((serv_resp = reader.readLine()) != null)
    {
        //FXMLDocumentController.txt.setText(serv_resp);
        if (serv_resp.startsWith("PING "))
        {
            this.pingPong();
        } else
        {
            System.out.println(serv_resp);

        }
    }
}

//Ping respond
public void pingPong() throws IOException
{
    writer.write("PONG " + serv_resp.substring(5) + "\r\n");
    writer.flush();
}
}

我认为没有必要添加fxml文档,因为它很大而且没有必要。 我还必须声明oracle教程没有帮助,因为它们只使用控制器中的事件处理,并且它们不会实现任何其他逻辑。

1 个答案:

答案 0 :(得分:0)

以下是一种可能的方法:

在收到消息时,为您的ServerConnection课程提供回电电话:

public class ServerConnection {

    private Consumer<String> messageCallback ;

    public void setMessageCallback(Consumer<String> messageCallback) {
        this.messageCallback = mesasgeCallback ;
    }

    // other fields and methods as before...

    public void startCommunication() throws IOException {

        // code as before ...


        while ((servResp = reader.readLine()) !=null) {
            if (messageCallback != null) {
                messageCallback.accept(servResp);
            }
            // etc....
        }

        // etc
    }
}

现在从控制器实例化你的ServerConnection类并传递一个回调:

public class FXMLDocumentController {

    ServerConnection serverConnection ;

    // ...

    public void initialize() {
        serverConnection = new ServerConnection();
        serverConnection.setMessageCallback(message -> 
            Platform.runLater(() -> txt.appendText(message+"\n")));
        Thread serverThread = new Thread(() -> serverConnection.startListening());
        serverThread.setDaemon(true); // thread will not stop application from exiting
        serverThread.start();
    }

    // ...
}