【发布时间】:2016-06-10 09:51:01
【问题描述】:
所以我必须非常快速地构建这个 JavaFX 应用程序,我的代码编译但 GUI 没有启动并且我得到异常。实现 FileChooser 代码后问题就开始了。
public class Main extends Application {
@FXML // fx:id="openButton"
private Button openButton; // Value injected by FXMLLoader
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Plot.fxml"));
openButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent arg0) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showOpenDialog(primaryStage);
System.out.println(file);
}
});
primaryStage.setTitle("Plotter");
primaryStage.setScene(new Scene(root, 1024 , 768));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXML 文件是这样的:
<BorderPane prefHeight="400.0" prefWidth="723.0" xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1">
<top>
<HBox alignment="TOP_CENTER" prefHeight="50.0" spacing="10.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="openButton" mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Open Dataset" />
<Button mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Button" />
<Button mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Button" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<BorderPane.margin>
<Insets />
</BorderPane.margin>
</HBox>
</top>
</BorderPane>
我对 JavaFX 完全是新手。任何提示表示赞赏。附言我正在使用 Gluon 场景构建器。
谢谢。
例外情况:
应用程序启动方法中的异常线程“main”中的异常 java.lang.RuntimeException:应用程序启动方法中的异常 com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 在 com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) 在 java.lang.Thread.run(Thread.java:745) 引起: java.lang.NullPointerException at sample.Main.start(Main.java:29) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 在 com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 在 com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 在 java.security.AccessController.doPrivileged(Native Method) 在 com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 在 com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 在 com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 在 com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) ... 1 更多
进程以退出代码 1 结束
【问题讨论】:
-
您能否提供您看到的具体错误消息?
-
@ChiefTwoPencils 添加。
-
@jewelsea 如果除了标记重复和指向 NullPointers 之外,您实际上有正确的答案,请写一个,否则,请原谅!当人们没有正确的答案并指出不相关的东西时,我讨厌这种情况!原谅这个问题,去别的地方吧!
-
@jewelsea 非常感谢!非常感谢!
标签: java