javafx - 每个 fxml (JavaFX) 的控制器
问题描述
我想为每个 fxml 设置一个控制器。这些控制器应该能够交互,但我总是得到一个NullPointerException
. 我有一个带有 a 的Button
fxml 和一个带有TextField
. 如果我按Button
,TextField
应该附加。我已经减少了一点代码。我知道这些问题被问了很多次,但是对我没有任何用处。
这是我的代码
:LogStage.fxml
. 这是TextArea
我设置文本的地方。
public class LogController{
@FXML private TextArea logTextAreaFx;
public void handlelogTextAreaFX(ActionEvent event, String text){
logTextAreaFx.appendText(text);
}
}
的控制器MainStage.fxml
。主窗口,有一个Button
谁应该设置TextArea
如果激活。
public class MainController{
@FXML LogController logController;
public void handleChatConsoleButton(ActionEvent event){
logController.handlelogTextAreaFX(event, "test");
}
}
LogStage.fxml:
<VBox prefWidth="500" prefHeight="300" id="logView" fx:controller="LogController" xmlns:fx="http://javafx.com/fxml" >
<TextArea text="-------- Log ---------" id="logTextArea" fx:id="logTextAreaFx"></TextArea>
</VBox>
MainStage.fxml:
<VBox minHeight="520" minWidth="1280" fx:controller="MainController" xmlns:fx="http://javafx.com/fxml">
<Button text="test Button" onAction="#handleChatConsoleButton"></Button>
</VBox>
主控制器。这些 Controller 应该设置所有 Controller 并实例化并启动GameView
.
public class ClientController {
private ClientView clientView;
@FXML private MainController mainController;
@FXML private LogController logController;
public ClientController(){
this.clientView = new ClientView();
}
public void showView(){
this.clientView.startView();
}
public void setText(String text){
logController.handlelogTextAreaFX(null, "hello world");
}
}
这些类启动整个应用程序。
public class GameMain {
static ClientController controller;
public static void main(String[] args){
controller = new ClientController();
controller.showView();
}
}
ClientView 设置所有框并启动 fxml。
public class ClientView extends Application{
VBox root = new VBox();
VBox mainStage;
public void startView(){
launch();
}
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(root);
loadMainStage();
loadLogStage();
primaryStage.setScene(scene);
primaryStage.show();
}
@FXML Pane MainStage;
public void loadMainStage(){
mainStage = new VBox();
try {
mainStage = FXMLLoader.load(getClass().getClassLoader().getResource("MainStage.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
root.getChildren().add(mainStage);
}
@FXML VBox LogStage;
public void loadLogStage(){
//ClientController controller = new ClientController();
FXMLLoader loader = new FXMLLoader();
Pane root = new Pane();
Scene chatScene = new Scene(root);
Stage stage = new Stage();
stage.setTitle("Log View");
stage.setScene(chatScene);
stage.setResizable(true);
try {
//loader.setController(controller);
LogStage = loader.load(getClass().getClassLoader().getResource("LogStage.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
root.getChildren().add(LogStage);
stage.show();
}
}
解决方案
推荐阅读
- express - 如何使用 express js 应用程序获取 bitbucket 私有存储库?
- javascript - 在 html 表单中禁用自动填充
- python - 类别中的不同列值
- laravel - Laravel sanctum $request->validate 验证失败时不返回 json
- google-apps-script - 在如何将复选框与宏链接方面需要您的帮助
- elasticsearch - 弹性搜索项目一个字段是某个字段的子字符串?
- python - 无需 ML 即可绘制用于实时对象检测的 bbox
- python - Matplotlib 条形图上的负值颜色不同
- office365 - 有没有办法将 Microsoft M365 配置文件卡用作其他 Web 应用程序中的小部件?
- arrays - 避免 C 中的副作用