当前位置: 首页 > 知识库问答 >
问题:

如何在handle方法中使用primary stage来查看JavaFX中的下一个fmxl?

广乐邦
2023-03-14

我试图在handle方法中创建一个主阶段,以便在使用第一个fxml(login.fxml)中的用户名和密码登录后查看下一个fmxl(即personnel.fxml),但每次登录时仍有错误。

main.java

package application;

import java.io.IOException;

import application.controller.LoginController;

import javafx.application.Application;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {

            Parent root = FXMLLoader.load(getClass().getResource("/application/controller/Login.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();

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

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

LoginController.java

package application.controller;

import java.io.IOException;

import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.layout.AnchorPane;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.BorderPane;

public class LoginController {

    @FXML
    private TextField user;

    @FXML
    private PasswordField pass;

    @FXML
    private Label label1;

    public void handleInput(ActionEvent actionEvent) throws IOException {

        if(user.getText().equals("abc") && pass.getText().equals("abc")) {

            label1.setText("Successful LogIn");

            Stage primaryStage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/application/controller/Personnel.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();

        } else {

            label1.setText("Unsuccess LogIn");
        }

    }

}
package application.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class PersonnelController {

    @FXML
    private Label label1;

    public void handleInput(ActionEvent actionEvent) {

        label1.setText("Hello World");
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<?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?>

<AnchorPane minHeight="400.0" minWidth="400.0" onDragDetected="#handleInput" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controller.LoginController">
   <children>
      <TextField fx:id="user" layoutX="117.0" layoutY="95.0" promptText="username" />
      <PasswordField fx:id="pass" layoutX="117.0" layoutY="135.0" promptText="password" />
      <Button layoutX="167.0" layoutY="200.0" mnemonicParsing="false" onAction="#handleInput" text="Login" />
      <Label fx:id="label1" layoutX="145.0" layoutY="174.0" text="Correct/Incorrect" />
   </children>
</AnchorPane>
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane minHeight="600.0" minWidth="600.0" onDragDetected="#handleInput" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="application.controller.PersonnelController">
   <children>
      <Label fx:id="label1" layoutX="185.0" layoutY="109.0" prefHeight="199.0" prefWidth="302.0" />
   </children>
</AnchorPane>
Nov 04, 2020 7:18:56 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by JavaFX runtime of version 8.0.261
Nov 04, 2020 7:19:06 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by JavaFX runtime of version 8.0.261
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    at java.lang.Thread.run(Unknown Source)
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)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 52 more
Caused by: java.lang.NullPointerException
    at application.controller.LoginController.handleInput(LoginController.java:42)
    ... 62 more

共有1个答案

哈和惬
2023-03-14

我发现了问题,唯一的问题是logincontroller类中缺少“application.css”位置。我需要做的只是把这个(“/application/application.css”)放入,而不是(“application.css”)

 类似资料:
  • 问题内容: 使用SceneBuilder。我有2个阶段,每个阶段都有一个控制器: , 。 Stage1Controller: Stage2Controller: 这是使用2种方法 (称为in 方法)将这两个fxml文件加载到Main.java类中的方式: 该方法在第一阶段中用作方法,它在两个阶段都转换视图。 如何输入方法?谢谢 问题答案: “快速又脏”的方法是给的引用: 现在在您的主应用程序中:

  • 我有一个带有start方法的类,用于启动JavaFX的primaryStage。 但是,我有另一个名为change_screen(int n)的函数,它将根据传递给它的数字创建一个新场景,并为该新场景执行primarystage.setscene()和.show()。 如果我的方法是错误的,那么在同一个窗口中改变几个场景的正确方法是什么?

  • 我正在努力在我的应用程序中获得一致的对齐,该应用程序是用GWT编写的,并通过基于WebKit的JavaFX 2.2 WebView(Java 7u40)进行查看。 我遇到的问题是,JavaFX中的CSS布局属性似乎存在问题/缺乏支持。文件(http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html)表示支持CSS 2

  • 问题内容: 我正在实现一个类似于工具箱的窗格,因此用户一次只能选择一个工具,因此我将其行为从切换到。 但是我发现它使用自己的皮肤带有点,但是我仍然希望它显示为法线。我是JavaFX和FXML的初学者,所以有人知道我该怎么做吗? 问题答案: 首先创建一个 单选按钮, 删除样式,然后添加如下样式 希望能解决您的问题

  • 我把这个要点用在我的文档中:https://gist.github.com/httpNick/708219f5002b6a9dc578 它对ES6 Babelfiying非常有效,但我想查看我的系统中的所有文件/public/js文件夹,并将生成版本输出到生成文件夹。有没有办法修改这个gulpfile来查看文件夹中的所有js文件? 从要点复制粘贴:

  • 对于Controller类,我没有太多内容: