java - 应用程序构造函数中的异常 - 无法启动类
问题描述
**我无法从“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);
}
}
解决方案
遵循模型-视图-控制器设计模式,模型不应持有对其控制器的引用。如果控制器需要响应模型数据的变化,那么这可以通过属性和监听器来完成。该模型拥有一个属性(此处为StringProperty),控制器监听该属性的更改。
对于您的代码,这意味着msg
将StringProperty
. 控制器在构建模型后,附加一个在模型接收到消息时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);
}
推荐阅读
- php - 添加新产品时,在 Woocommerce 中自动提高 10% 的价格
- time-complexity - 大于 1 的常数的大 O 表示法
- php - 使用 Codeigniter 将带有会话数据的数据插入数据库
- youtube-iframe-api - 如何从 youtube iframe 播放器获取 TimeRanges?
- c# - 解析 json 响应 c#
- javascript - 如何使 HTML 表格的 tbody 滚动?
- excel - 气候:计算连续降雨天数
- dart - text justify 无法与阿拉伯语的 RichText 一起正常工作
- perl - 我有 csv 文件我可以知道如何在 linux 中用下划线替换列标题中的空格吗
- azure-logic-apps - 如何使用多个 HTTP 请求调用程序创建单个逻辑应用程序