gpt4 book ai didi

java - 无法访问其他 Controller 的字段

转载 作者:行者123 更新时间:2023-12-02 12:54:44 29 4
gpt4 key购买 nike

我正在尝试用 JavaFX 编写 GUI 应用程序。基本思想是有一个主 GridPane View ,其内部是另一个应在按下按钮时更改的 Pane 。由于此 Pane 必须读取用户的一些输入,因此我希望 Controller 之间存在流,以便我可以在应用程序的不同部分之间传递数据。我的想法是将所有 Controller 放在一个地方,并从任何地方引用它们的字段。它似乎不起作用 - 似乎我无法从 Pane 外部修改 Pane 的内容,即使我可以访问它的字段。它看起来或多或少像这样:

main_view.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.MainController">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<GridPane prefHeight="300.0" prefWidth="400.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="30.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" spacing="20.0" GridPane.rowSpan="2">
<children>
<TextField fx:id="userInputTextField">
<VBox.margin>
<Insets left="5.0" right="5.0" />
</VBox.margin>
</TextField>
<Button mnemonicParsing="false" onAction="#appendToFirstArea" text="Append to area 1" />
<Button mnemonicParsing="false" onAction="#appendToSecondArea" text="Append to area 2" />
<Button mnemonicParsing="false" onAction="#moveToFirstArea" text="Move to area 1" />
<Button mnemonicParsing="false" onAction="#moveToSecondArea" text="Move to area 2" />
</children>
<GridPane.margin>
<Insets />
</GridPane.margin>
</VBox>
<Pane fx:id="content" prefHeight="300.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowSpan="2" />
</children>
</GridPane>
</children>
</GridPane>

MainController.java

package main;

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;

import java.io.IOException;

public class MainController {
public TextField userInputTextField;
public Pane content;

private FirstAreaController getFirstAreaController() {
return Main.firstAreaController;
}

private SecondAreaController getSecondAreaController() {
return Main.secondAreaController;
}

public void appendToFirstArea(ActionEvent actionEvent) {
String text = userInputTextField.getText();
getFirstAreaController().firstTextArea.appendText(text + "\n");
}

public void appendToSecondArea(ActionEvent actionEvent) {
String text = userInputTextField.getText();
getSecondAreaController().secondTextArea.appendText(text + "\n");
}

public void moveToFirstArea(ActionEvent actionEvent) {
try {
GridPane firstArea = FXMLLoader.load(getClass().getResource("first_area.fxml"));
content.getChildren().clear();
content.getChildren().add(firstArea);
} catch (IOException e) {
e.printStackTrace();
}
}

public void moveToSecondArea(ActionEvent actionEvent) {
try {
GridPane secondArea = FXMLLoader.load(getClass().getResource("second_area.fxml"));
content.getChildren().clear();
content.getChildren().add(secondArea);
} catch (IOException e) {
e.printStackTrace();
}
}
}

first_area.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane onKeyPressed="#appendTest" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity"
prefHeight="300.0" prefWidth="280.0"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111"
fx:controller="main.FirstAreaController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
</rowConstraints>
<children>
<TextArea fx:id="firstTextArea" prefHeight="200.0" prefWidth="200.0">
<GridPane.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</GridPane.margin>
</TextArea>
</children>
</GridPane>

FirstAreaController.java

package main;

import javafx.scene.control.TextArea;

public class FirstAreaController {
public TextArea firstTextArea;
}

second_area.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity"
prefHeight="300.0" prefWidth="280.0"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111"
fx:controller="main.SecondAreaController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
</rowConstraints>
<children>
<TextArea fx:id="secondTextArea" prefHeight="200.0" prefWidth="200.0">
<GridPane.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</GridPane.margin>
</TextArea>
</children>
</GridPane>

SecondAreaController.java

package main;

import javafx.scene.control.TextArea;

public class SecondAreaController {
public TextArea secondTextArea;
}

Main.java

package main;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

public static MainController mainController;
public static FirstAreaController firstAreaController;
public static SecondAreaController secondAreaController;

@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("main_view.fxml"));
Parent root = loader.load();
primaryStage.setTitle("Test");
primaryStage.setScene(new Scene(root, 400, 300));
mainController = loader.getController();

FXMLLoader firstLoader = new FXMLLoader();
firstLoader.setLocation(getClass().getResource("first_area.fxml"));
firstLoader.load();
firstAreaController = firstLoader.getController();

FXMLLoader secondLoader = new FXMLLoader();
secondLoader.setLocation(getClass().getResource("second_area.fxml"));
secondLoader.load();
secondAreaController = secondLoader.getController();

primaryStage.setResizable(false);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

按“附加到 1 个区域”按钮后,没有任何反应 - 文本没有显示在 TextArea 中。不过,当我添加仅在 FirstAreaController 内部调用的方法时,文本会按预期出现在该区域中。我该如何修复它?

最佳答案

您正在加载second_area.fxml两次: MainController 一次,您在哪里加载 second_area.fxml并显示它,并在 Main 中显示一次。您正在访问的 Controller 是与您加载 second_area.fxml 时加载的 UI 关联的 Controller 。在Main ,但实际显示的 UI 是您在 MainController 中加载的 UI 。因此,当您调用 Controller 上的方法时,您正在更新从未显示过的 UI。

无论如何,公共(public)字段遍布各处的“全局数据”方法并不是一个特别好的主意,并且(由于许多有据可查的原因)从长远来看不太可能非常易于维护。

因为您仅从 MainController 访问这些内容,只需输入 FirstAreaControllerSecondAreaController MainController 中的字段类,并在加载(和显示)相应的 FXML 文件时设置它们的值:

public class MainController {
public TextField userInputTextField;
public Pane content;

private FirstAreaController firstAreaController ;
private SecondAreaController secondAreaController ;



public void appendToFirstArea(ActionEvent actionEvent) {
String text = userInputTextField.getText();
firstAreaController.firstTextArea.appendText(text + "\n");
}

public void appendToSecondArea(ActionEvent actionEvent) {
String text = userInputTextField.getText();
secondAreaController.secondTextArea.appendText(text + "\n");
}

public void moveToFirstArea(ActionEvent actionEvent) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("first_area.fxml"));
GridPane firstArea = loader.load();
firstAreaController = loader.getController();
content.getChildren().clear();
content.getChildren().add(firstArea);
} catch (IOException e) {
e.printStackTrace();
}
}

public void moveToSecondArea(ActionEvent actionEvent) {
try {
FXMLLoader loader = getClass().getResource("second_area.fxml");
GridPane secondArea = loader.load();
secondAreaController = loader.getController();
content.getChildren().clear();
content.getChildren().add(secondArea);
} catch (IOException e) {
e.printStackTrace();
}
}
}

如果您需要从“更远的”对象更新 UI,请考虑使用数据模型和 MVC 方法。参见,例如Applying MVC With JavaFx .

关于java - 无法访问其他 Controller 的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44478498/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com