我正在尝试从主类(MAAWB)启动IntLogin窗口;但是,我似乎无法启动它。我的初始屏幕启动没有问题。我运行了一个没有问题的调试,直到它到达启动序列,然后它回放了一个无法识别的错误。我在下面包含了主文件、fxml 和控制器文件。我在这里错过或做错了什么,让我头疼?另外,如果有人有答案,如何将我的初始屏幕放在计时器上?
主要
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)
注释
用户名定义@FXML。
@FXML
private TextField username;
这会通过 fx:id="username"
将控制器中定义的文本字段与 FXML 中定义的文本字段相关联。