无法使用JAVAFX FXML文件启动应用程序

时间:2016-03-02 03:23:59

标签: javafx controller main fxml launch

我正在尝试从主类(MAAWB)启动IntLogin窗口;但是,我似乎无法启动它。我的启动画面没有问题。我运行了一个没有问题的调试,直到它达到启动顺序,然后它才能恢复无法识别的错误。我在下面包含了main,fxml和controller文件。我在这里错过或做错了什么导致了我所有这些令人头疼的问题?如果有人有答案我怎么把我的启动画面放在计时器上?

package javafxswingapplication3;

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


public class MAAWB extends Application {

@Override
public void start(Stage primaryStage) {
    Parent root;
    try {
    root = FXMLLoader.load(getClass().getResource("Intlogin.fxml"));
    Stage stage = new Stage();
    stage.setTitle("initial login");
    stage.setScene(new Scene(root, 222, 484));
   stage.show();

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

public static void main(String[] args) {
    Preloader.launch(MAAWBPreloader.class, args);
    Application.launch(MAAWB.class, args);



}

}

FXML

   
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.effect.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="222.0" prefWidth="484.0"     xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"     fx:controller="javafxswingapplication3.IntloginController">
   <children>
  <TextField layoutX="173.0" layoutY="50.0" />
  <TextField fx:id="username" editable="false" layoutX="173.0" layoutY="90.0" />
  <ChoiceBox layoutX="173.0" layoutY="130.0" prefHeight="31.0" prefWidth="187.0" />
  <Label layoutX="91.0" layoutY="55.0" text="Installation" />
  <Label layoutX="94.0" layoutY="95.0" text="Username" />
  <Label layoutX="58.0" layoutY="135.0" text="Functional Area" />
  <Button cancelButton="true" layoutX="400.0" layoutY="182.0"     mnemonicParsing="false" text="Cancel" />
  <Button layoutX="327.0" layoutY="182.0" mnemonicParsing="false" text="Submit" />
</children>
</AnchorPane>

控制器

package javafxswingapplication3;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.Properties;
import static javafx.application.ConditionalFeature.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;

public class IntloginController implements Initializable {

private TextField username;

@Override
public void initialize(URL url, ResourceBundle rb) {
    final String UN = System.getProperty("user.name");

    username.setText(UN);
}    

}

错误

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at javafxswingapplication3.MAAWB.start(MAAWB.java:31)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at javafxswingapplication3.IntloginController.initialize(IntloginController.java:28)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)

1 个答案:

答案 0 :(得分:0)

注释用户名定义@FXML

@FXML
private TextField username;

这会将控制器中定义的TextField与通过fx:id="username"在FXML中定义的文本字段相关联。