应用程序构造函数中的异常-无法启动类

时间:2019-01-06 00:30:39

标签: java javafx constructor

**我无法从“ GUIController”创建构造函数。如果我删除此行,程序将运行 “ GUIController(){myModel = new TheModel(this)” 但我仍然需要其他部分。请帮忙!

**

package theclient;

import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;


public class GUIController extends Application {

    TheModel myModel;

    GUIController(){
        myModel = new TheModel(this);
    }

    //public void init(){}

    public TextArea myTextArea;
    public TextField myTextField;

    // Button and text field actions
    public void myButtonAction() {
        sendMsg();
    }

    public void myTextFieldAction() {
        sendMsg();
    }

    // Append coming message
    public void displayMsg(String comingMSG) {
        System.out.println("Receive 01");
        myTextArea.appendText(comingMSG);
    }

    public void sendMsg() {
        try {
            System.out.println("Send 01");
            myModel.myChatServer.tellOthers(myTextField.getText());
        } catch (RemoteException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

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

        Scene scene = new Scene(root, 600, 400);

        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

    public static void main(String[] args) throws Exception {
        new GUIController();
        launch(args);
    }
}

第二等。如果您可以建议对代码进行任何修改,我们将不胜感激。在此先感谢您的努力。

package theclient;

import common.ChatServerInt;
import common.ClientInt;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TheModel implements ClientInt {

    public GUIController myCtrl;
    ChatServerInt myChatServer;

    TheModel(GUIController myCtrl) {
        this.myCtrl = myCtrl;
    }

    public ChatServerInt connection() {
        if (myChatServer == null) {
            try {
                Registry reg = LocateRegistry.getRegistry(1111);
                myChatServer = (ChatServerInt) reg.lookup("ChatService");
                myChatServer.register(this);
                myChatServer.tellOthers("I'm here!");
            } catch (RemoteException | NotBoundException ex) {
                Logger.getLogger(TheModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        } return myChatServer;
    }

    @Override
    public void receive(String msg) throws RemoteException {
        myCtrl.displayMsg(msg);
    }
}

1 个答案:

答案 0 :(得分:2)

遵循Model-view-controller设计模式,该模型不应持有对其控制器的引用。如果控制器需要响应模型数据的更改,则可以使用属性和侦听器来完成。该模型拥有一个属性(此处为StringProperty),控制器监听该属性的更改。

对于您的代码,这意味着将msg存储在StringProperty中。构造模型后,控制器将附加一个ChangeListener,该displayMsg将在模型收到消息时调用TheModel

GUIController使用属性和侦听器,不再存储对GUIController的引用,并且在其构造函数中不将GUIController作为参数。

public class GUIController extends Application { ... TheModel myModel; ... GUIController(){ myModel = new TheModel(); // Listen for changes in the msg StringProperty and call displayMessage when it changes myModel.getMsgProperty().addListener(msg -> this.displayMsg(msg)); } ... 看起来像这样:

GUIController

请注意,this的构造函数不再需要将TheModel传递给构造函数TheModel。 (通常,avoid passing this outside of the constructor。在构造函数返回之前,不会完全构造对象。)

public class TheModel implements ClientInt { ... private StringProperty msgProperty; ... // remember to add a getter and setter for msgProperty! ... @Override public void receive(String msg) throws RemoteException { // When a message is received, listeners attached to msgProperty will execute when setValue is called msgProperty.setValue(msg); } 看起来像这样:

volatile bool pleaseQuitNow = false;