登录后Javafx无法显示主窗口fxml

时间:2016-08-11 01:58:55

标签: javafx login javafx-8

im devlop javaFx登录app来自youtube上的教程,用于学校作业。但点击登录按钮后。登录功能无法显示窗口主场景(AdminLayout.fxml)。只需登录窗口隐藏。谢谢

这是我的错误

Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 45 more
Caused by: java.lang.NullPointerException
    at mpn.manda.controller.LoginLayoutController.Login(LoginLayoutController.java:58)
    ... 55 more

LoginLayoutController.java第58行

Pane p = fxmlLoader.load(getClass().getResource("view/AdminLayout.fxml").openStream());

File LoginLayoutController.java

package mpn.manda.controller;

import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;

import javafx.fxml.Initializable;
import mpn.manda.model.LoginModel;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author Xaxxis
 */
public class LoginLayoutController implements Initializable {
    public LoginModel loginModel = new LoginModel();


    @FXML
    private Label isConnected;
    @FXML
    private TextField usernameField;
    @FXML
    private PasswordField passwordField;
    @Override
    public void initialize(URL location, ResourceBundle resource) {
        if (loginModel.isDbConnected()) {
            isConnected.setText("Connected");
        } else {
            isConnected.setText("Not Connected");
        }
    }

    public void Login (ActionEvent event) {

        try {
            if (loginModel.isLogin(usernameField.getText(), passwordField.getText())) {

                ((Node)event.getSource()).getScene().getWindow().hide();
                Stage primaryStage = new Stage();
                FXMLLoader fxmlLoader = new FXMLLoader();
                Pane p = fxmlLoader.load(getClass().getResource("view/AdminLayout.fxml").openStream());
                AdminLayoutController adminController = (AdminLayoutController)fxmlLoader.getController();
                adminController.GetUser(usernameField.getText());
                Scene scene = new Scene(p);
                scene.getStylesheets().add(getClass().getResource("css/application.css").toExternalForm());
                primaryStage.setScene(scene);
                primaryStage.show();      

            } else {
                isConnected.setText("Username and password is not correct");
            }
          } catch (SQLException e) {
              isConnected.setText("Username and password is not correct");
          }
             catch(IOException e) {
                e.printStackTrace();
            }
        }
    }

MainApp.java

package mpn.manda;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Xaxxis
 */
public class MainApp extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("view/LoginLayout.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("css/application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }    
}
</pre>

AdminLayoutController.java

<pre>
package mpn.manda.controller;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

/**
 * FXML Controller class
 *
 * @author Xaxxis
 */
public class AdminLayoutController implements Initializable {

    @FXML
    private Label userLabel;
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
    public void GetUser(String user) {
        // TODO
        userLabel.setText(user);
    }

    public void Logout(ActionEvent event) {

}
}

LoginLayout.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.net.URL?>
<?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?>
<?import javafx.scene.paint.RadialGradient?>
<?import javafx.scene.paint.Stop?>
<?import javafx.scene.text.Font?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mpn.manda.controller.LoginLayoutController">
    <stylesheets>
        <URL value="@/mpn/manda/css/application.css" />
    </stylesheets>
   <children>
      <Label fx:id="isConnected" layoutX="18.0" layoutY="94.0" prefHeight="28.0" prefWidth="485.0" text="Status" textFill="#f20202">
         <font>
            <Font size="15.0" />
         </font>
      </Label>
      <Label layoutX="94.0" layoutY="135.0" text="Username" />
      <Label layoutX="94.0" layoutY="183.0" text="Password" />
      <TextField fx:id="usernameField" layoutX="203.0" layoutY="130.0" promptText="Your username" />
      <PasswordField fx:id="passwordField" layoutX="203.0" layoutY="178.0" promptText="Your password" />
      <Button layoutX="203.0" layoutY="236.0" mnemonicParsing="false" onAction="#Login" prefHeight="27.0" prefWidth="78.0" text="Login..." />
      <Button layoutX="288.0" layoutY="236.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="78.0" text="Cancel" />
      <Label layoutX="47.0" layoutY="24.0" prefHeight="59.0" prefWidth="381.0" text="Welcom To The App">
         <font>
            <Font size="31.0" />
         </font>
         <textFill>
            <RadialGradient centerX="0.5" centerY="0.5" radius="0.5">
               <stops>
                  <Stop color="#3c361c" />
                  <Stop color="#9a8d8d" offset="0.9330855018587361" />
                  <Stop color="#9a8d8d" offset="1.0" />
               </stops>
            </RadialGradient>
         </textFill>
      </Label>
   </children>
</AnchorPane>

AdminLayout.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mpn.manda.controller.AdminLayoutController">
   <top>
      <Label fx:id="userLabel" prefHeight="17.0" prefWidth="154.0" text="Halo, " BorderPane.alignment="CENTER" />
   </top>
   <bottom>
      <Button mnemonicParsing="false" text="Logout.." BorderPane.alignment="CENTER" />
   </bottom>
</BorderPane>

1 个答案:

答案 0 :(得分:1)

路径错了。

<div id="hex1" class="hexagon-wrapper">
    <div id="color1" class="hexagon">yellow</div>
</div>

将解析相对于当前类的路径(因此它正在寻找getClass().getResource("view/AdminLayout.fxml") )。假设(因为资源在/mpn/manda/controller/view/AdminLayout.fxml中正常运行)MainApp包是view(即mpn.manda.viewAdminLayout.fxml在同一个包中),您可以做

LoginLayout.fxml

(注意相对于类路径解析的前导getClass().getResource("/mpn/manda/view/AdminLayout.fxml") )或

/

将解析相对于MainApp.class.getResource("view/AdminLayout.fxml") 类的路径。

不要试图使用MainApp来引用&#34;父包&#34;:当您从文件系统中读取资源和类时(例如在开发期间),这可能会有效,但会失败如果您的应用程序捆绑在一个jar文件中(它将在生产时)。

顺便说一句,您应该更喜欢通过指定FXML资源的URL而不是流来加载FXML。这是因为如果您没有指定..的URL,FXML文件本身中的任何resource resolutions都将失败。所以我建议

FXMLLoader

如果你喜欢FXML文件有一个包(FXMLLoader fxmlLoader = new FXMLLoader(MainApp.class.getResource("view/AdminLayout.fxml")); Pane p = fxmlLoader.load(); // ... )和控制器有一个(view)的结构(我不是,FWIW),另一种技术你可能是为了解析视图资源而定义一个空类:

controller

现在你可以从任何地方加载任何FXML

package mpn.manda.view ;

public abstract class View { }

FXMLLoader loader = new FXMLLoader(View.class.getResource("LoginLayout.fxml"));
Parent root = loader.load();

等。等