【问题标题】:JavaFX transition scene from Controller来自 Controller 的 JavaFX 过渡场景
【发布时间】:2014-12-04 19:26:30
【问题描述】:

我对 JavaFX(以及一般的 java)非常陌生,我一直在尝试在 FXML 控制器的场景之间进行转换。我尝试在网上查找多种解决方案,但似乎都没有。

我的主要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();
}

}

...和我的登录控制器:

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) {}

}

这是我得到的错误:

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

我改变场景的方式不起作用。我将如何处理这个问题? 提前致谢。

【问题讨论】:

  • 那么实际的问题是什么?
  • 如何从 loginController 更改场景。
  • 嗯,你基本上做对了。你能编辑你的问题来解释会发生什么(如果合适的话,它与你想要的有什么不同)。如果有错误,你需要说出它们是什么。
  • 对不起,我是新手。我正在使用 eclipse 并得到一个很长的错误日志。我应该发布整个内容吗?
  • 发布堆栈跟踪的相关部分(可能足以从最后一个“由”部分开始)。并确定它指向代码中的哪一行。

标签: java controller javafx fxml scene


【解决方案1】:

你的解决方案有什么问题

您的 getResource 参数错误 - 您不应使用文件路径(例如 F:/),而应使用 something related to your class path

您可能还有其他错误,我没有检查,只是想指出一个明显的错误。

如何解决

最简单的解决方案是将 Main.fxml 放在与 LoginController.java 相同的目录中,并检查编译程序时 Main.fxml 是否已被构建系统复制到与 LoginController.class 相同的目录中。

您只需使用FXMLLoader.load(getClass().getResource("Main.fxml")); 进行查找(与您的 Login.fxml 类似)。

示例代码

这是一个sample for switching FXML based scenes(如果您想替换整个场景内容而不是像示例那样替换部分场景,您的代码可能会更简单)。

【讨论】:

  • 谢谢,我尝试了你的想法。不幸的是,我仍然得到错误。我发布了更新的代码和错误日志。编辑:没有看到您的示例代码。会调查的,谢谢。
  • 您发布的错误是它找不到 MainController 类(您的问题中也没有提供)。无论如何,您都应该删除绝对文件引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多