gpt4 book ai didi

JavaFX 节点未初始化

转载 作者:行者123 更新时间:2023-12-02 00:58:24 24 4
gpt4 key购买 nike

我试图更改应用程序中的场景,并且加载场景后需要直接更新其中一个节点,但问题是该节点尚未初始化,所以我总是得到 NPE。如何强制初始化节点或等到所有节点都已初始化后再调用该 ndoe 的方法

我从未成功尝试调用节点,但是如果我在事件中调用节点(例如按下按钮),它似乎确实有效。

package za.holmes.PAT;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;

public class EventHandler implements Initializable {

@FXML
private AnchorPane changePane;
@FXML
private TextField IDField;
@FXML
private PasswordField passField;
@FXML
private Label errorField;
@FXML
private Label nameText;

private User currentUser;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}

// @FXML
// private void initialize() {
// }

@FXML
private void start(ActionEvent event) {
((Button) event.getSource()).setDisable(true);;
System.out.println("Program started");
try {
System.out.println("Connecting to database");
Class.forName("za.holmes.PAT.DataHandler");
} catch (Exception e) {
e.printStackTrace();
}
updateScreen("Login.fxml");
}

private void updateScreen(String fileName) {
try {
FXMLLoader loader = new FXMLLoader();
AnchorPane pane = (AnchorPane)loader.load(Oldage.class.getResourceAsStream("/scenes/" + fileName));
changePane.getChildren().setAll(pane);

} catch (Exception e) {
e.printStackTrace();
}
}

//Init complete
@FXML
private void login(ActionEvent event) {
String userIDText = "", passText = "";
int user = 0;
try {
errorField.setVisible(false);
userIDText = IDField.getText();
passText = passField.getText();
// This is a call to another method that gets the type of user
// attempting to log in(it will be 1)
user = DataHandler.checkUser(userIDText, passText);
switch (user) {
case 0:
errorField.setVisible(true);
break;
case 1:
updateScreen("Old.fxml");
currentUser = DataHandler.getInfoElder(userIDText);
nameText.setText("Hello");
break;
case 2:
break;
case 3:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

最佳答案

是的,只是不要使用单个 Controller 。使用多个 Controller ,而不是通过改变元素的可见性来改变屏幕,而不是让每个“场景”成为一个全新的 FXML 文件

关于JavaFX 节点未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57789624/

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