一段时间以来,我一直在寻找解决此问题的方法,但一直未能找到。
我基本上是 JavaFX、Scene Builder、FXML、CSS 的新手。在此之前,我有三个月的 Java 经验,仅此而已。
我已经创建了一个元素; JavaFX。我在这个元素中创建了两个包; Controller 和 View 。在 Controller 包内有两个文件; Login.java 和 AdministratorHomescreen.java。在 View 包中有四个文件; QuizApp.fxml、QuizApp.css、Login.fxml 和 AdministratorHomescreen.fxml。
我一直在使用谷歌搜索并遵循不同的教程来学习如何做各种事情,到目前为止,我一直进展顺利。但现在我停住了。
当我的应用程序启动并且用户按下登录按钮时,我想做的是将它们转发到管理员主屏幕。
这是 Login.java 中的代码:
public class Login extends Application {
private BorderPane quizApp;
private Stage windowLogin;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage windowLogin) {
this.windowLogin = windowLogin;
this.windowLogin.setTitle("QuizApp - Please Login or Register");
windowLogin.resizableProperty().setValue(Boolean.FALSE);
applyQuizAppLayout();
applyLoginLayout();
}
public void applyQuizAppLayout() {
try {
FXMLLoader fxmlLoader = new FXMLLoader(Login.class.getClassLoader().getResource("view/QuizApp.fxml"));
quizApp = (BorderPane) fxmlLoader.load();
Scene scene = new Scene(quizApp);
windowLogin.setScene(scene);
windowLogin.show();
} catch (IOException exception) {
exception.printStackTrace();
}
}
public void applyLoginLayout() {
try {
FXMLLoader fxmlLoader = new FXMLLoader(Login.class.getClassLoader().getResource("view/Login.fxml"));
AnchorPane windowLogin = (AnchorPane) fxmlLoader.load();
quizApp.setCenter(windowLogin);
} catch (IOException exception) {
exception.printStackTrace();
}
}
public Stage getWindowLogin() {
return windowLogin;
}
Button loginButton = new Button();
@FXML
private void handleLoginButtonAction(ActionEvent event) {
System.out.println("Login details correct.");
// windowLogin.setScene(AdministratorHomescreen.start(windowAdministratorHomescreen));
}
}
}
我注释掉的最后一行代码是我得到错误的地方。错误显示为“无法解析为变量”。该按钮的工作方式与此不同,因此当单击它时,会向控制台显示一条消息。
这是 AdministratorHomepage.java 中的代码:
public class AdministratorHomescreen {
private BorderPane quizApp;
private Stage windowAdministratorHomescreen;
public void start(Stage windowAdministratorHomescreen) {
this.windowAdministratorHomescreen = windowAdministratorHomescreen;
this.windowAdministratorHomescreen.setTitle("QuizApp - Welcome Administrator");
windowAdministratorHomescreen.resizableProperty().setValue(Boolean.FALSE);
applyQuizAppLayout();
applyWindowAdministratorHomescreenLayout();
}
public void applyQuizAppLayout() {
try {
FXMLLoader fxmlLoader = new FXMLLoader(Login.class.getClassLoader().getResource("view/QuizApp.fxml"));
quizApp = (BorderPane) fxmlLoader.load();
Scene scene = new Scene(quizApp);
windowAdministratorHomescreen.setScene(scene);
windowAdministratorHomescreen.show();
} catch (IOException exception) {
exception.printStackTrace();
}
}
public void applyWindowAdministratorHomescreenLayout() {
try {
FXMLLoader fxmlLoader = new FXMLLoader(
Login.class.getClassLoader().getResource("view/WindowAdministratorHomescreen.fxml"));
AnchorPane windowAdministratorHomescreen = (AnchorPane) fxmlLoader.load();
quizApp.setCenter(windowAdministratorHomescreen);
} catch (IOException exception) {
exception.printStackTrace();
}
}
public Stage getWindowAdministratorHomescreen() {
return windowAdministratorHomescreen;
}
}
所以我知道编译器找不到我的变量,但是每当我更改 AdministratorHomescreen.java 中的方法时,它都会抛出一大堆新错误,所以我现在基本上只是在兜圈子。
有没有办法解决这个问题?我很可能把这一切都弄错了,但这是一天结束时的学习曲线。
非常感谢任何帮助。
附言当用户单击按钮时,我不只是想打开任何随机场景,我想打开一个已经应用了 QuizApp.fxml、QuizApp.css 和 AdministratorLogin.fxml 的场景,因此我的代码布局。
最佳答案
在您的登录按钮的监听器中,您需要将相关信息传递到下一阶段。
首先在 Logon 类中为您的登录按钮设置一个监听器
loginbutton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
try{
if("the login is successful"){
String temp = "Your relevant user information, like username or however you keep track of your data";
windowLogin.hide();
//You'll then make a new stage to pass the info to the administrator page
AdministratorHomeScreen nextStage = new AdministratorHomeScreen(windowLogin, temp);
}
else{
System.out.println("incorrect login);
}
}
catch(Exception e){
e.printStackTrace();
}
}
});
然后在您的 AdministratorHomeScreen 类中执行
public AdministratorHomeScreen(Stage stage, String takenOver){
windowLogIn = stage;
temp = takenOver;
start(new Stage());
}
我很确定这几乎涵盖了它。
编辑:对代码的顶部进行格式化,使其更易于阅读。
关于JavaFX Scene Builder 访问不同类中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36069312/