简体   繁体   English

Controller的JavaFX过渡场景

[英]JavaFX transition scene from Controller

I am very new to JavaFX (and java in general) and I have been trying to transition between scenes from an FXML controller. 我对JavaFX(和一般Java)非常陌生,并且我一直在尝试从FXML控制器在场景之间进行转换。 I have tried to look up multiple solutions online however, none of them seem to work. 我试图在线查找多个解决方案,但是似乎都没有用。

My main java code: 我的主要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 App extends Application {

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

@Override
public void start(Stage primaryStage) throws Exception {


    Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));

    Scene scene1 = new Scene(root);

    primaryStage.setScene(scene1);
    primaryStage.setTitle("Login");
    primaryStage.show();
}

}

... and my LoginController: ...和我的LoginController:

package main;

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.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class LoginController implements Initializable 
{

@FXML
private Label loginLabel;

@FXML
private TextField fieldUsername;

@FXML 
private PasswordField fieldPassword;


@FXML
public void loginEvent(ActionEvent event) throws Exception{
    //This is where I try to change the scene
    if (fieldUsername.getText().equals("admin")  && fieldPassword.getText().equals("admin")){
    Parent parent = FXMLLoader.load(getClass().getResource("Main.fxml"));
    Stage primaryStage = new Stage();
    Scene scene = new Scene (parent);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Main Frame");
    primaryStage.show();
    }
    else {
        loginLabel.setText("Incorrect Username or Password");
    }
}

@Override
public void initialize(URL arg0, ResourceBundle arg1) {}

}

Here is the Error I get: 这是我得到的错误:

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 48 more
Caused by: javafx.fxml.LoadException: 
/F:/Programming/JAVA/Eclipse/Password%204/bin/main/Main.fxml:9

at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.access$700(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at main.LoginController.loginEvent(LoginController.java:34)
... 57 more
Caused by: java.lang.ClassNotFoundException: main.MainController
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 71 more

My way of changing the scene doesn't work. 我更改场景的方式无效。 How would I go about this issue? 我将如何处理这个问题? Thanks in advance. 提前致谢。

What is wrong with your solution 您的解决方案出了什么问题

Your getResource argument is wrong - you should not use a file path (eg F:/) instead you should use something related to your class path . 您的getResource参数是错误的-您不应使用文件路径(例如F:/),而应使用与类路径相关的内容

You may have other errors, I haven't checked, just wanted to note that obvious one. 您可能还有其他错误,我没有检查,只是想指出一个明显的错误。

How to fix it 如何修复

Easiest solution is to place the Main.fxml in the same directory as LoginController.java and check that, when you compile the program, Main.fxml has been copied by your build system to the same directory as LoginController.class. 最简单的解决方案是将Main.fxml放在与LoginController.java相同的目录中,并检查编译程序时Main.fxml是否已由构建系统复制到与LoginController.class相同的目录中。

For your lookup just use FXMLLoader.load(getClass().getResource("Main.fxml")); 对于您的查找,只需使用FXMLLoader.load(getClass().getResource("Main.fxml")); (similarly for your Login.fxml). (类似于您的Login.fxml)。

Sample code 样例代码

Here is a sample for switching FXML based scenes (your code could be simpler if you want to replace the scene content as a whole rather than parts of the scene like the sample does). 这是一个用于切换基于FXML的场景示例 (如果您想整体替换场景内容,而不是像该示例那样替换场景的一部分 ,您的代码可能会更简单)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM