首页 > 解决方案 > 应用程序构造函数中的异常 - 无法启动类

问题描述

**我无法从“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);
    }
}

标签: javajavafxconstructor

解决方案


遵循模型-视图-控制器设计模式,模型不应持有对其控制器的引用。如果控制器需要响应模型数据的变化,那么这可以通过属性和监听器来完成。该模型拥有一个属性(此处为StringProperty),控制器监听该属性的更改。

对于您的代码,这意味着msgStringProperty. 控制器在构建模型后,附加一个在模型接收到消息时ChangeListener调用的函数。displayMsg

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

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。(通常,避免在构造函数之外传递this。在构造函数返回之前,对象不会完全构造。)

TheModel 看起来像这样:

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);
}

推荐阅读