【问题标题】:Cant launch application using JAVAFX FXML file无法使用 JAVAFX FXML 文件启动应用程序
【发布时间】:2016-03-02 03:23:59
【问题描述】:

我正在尝试从主类 (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)

【问题讨论】:

  • “无法识别的错误”是什么意思?
  • 编译器踢回“错误变量无法识别”
  • 编译器?我以为你说你正在运行这个,然后你得到了一个错误。哪一行给出了编译错误?
  • 抱歉错过了通信,运行时确实发生了错误。构建完全没有问题。
  • 那么你能在你的问题中发布堆栈跟踪吗?哪一行抛出异常?

标签: javafx controller main fxml launch


【解决方案1】:

注释用户名定义@FXML

@FXML
private TextField username;

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

【讨论】:

  • 哇,我觉得自己完全傻了。非常感谢您的帮助,我完全忘记添加它了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多