Java 图形用户界面不显示
因此,我必须非常快地构建这个JavaFX应用程序,我的代码可以编译,但GUI没有启动,我会遇到异常。一旦FileChooser代码实现,问题就开始了Java 图形用户界面不显示,java,Java,因此,我必须非常快地构建这个JavaFX应用程序,我的代码可以编译,但GUI没有启动,我会遇到异常。一旦FileChooser代码实现,问题就开始了 public class Main extends Application { @FXML // fx:id="openButton" private Button openButton; // Value injected by FXMLLoader @Override public void start(S
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);
}
}
public类主扩展应用程序{
@FXML//fx:id=“openButton”
私有按钮openButton;//FXMLLoader注入的值
@凌驾
public void start(Stage primaryStage)引发异常{
父根=fxmloader.load(getClass().getResource(“Plot.fxml”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent arg0){
FileChooser FileChooser=newfilechooser();
FileChooser.ExtensionFilter extFilter=newfilechooser.ExtensionFilter(“TXT文件(*.TXT)”,“*.TXT”);
fileChooser.getExtensionFilters().add(extFilter);
File File=fileChooser.showOpenDialog(primaryStage);
System.out.println(文件);
}
});
初级阶段。设置标题(“绘图仪”);
设置场景(新场景(根,1024768));
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(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完全是新手。任何提示都将不胜感激。另外,我正在使用胶子场景生成器
谢谢
例外情况:
应用程序启动方法中的异常线程“main”中的异常
java.lang.RuntimeException:
com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:917)
在
com.sun.javafx.application.launchempl.lambda$launchApplication$155(launchempl.java:182)
在java.lang.Thread.run(Thread.java:745)处,由以下原因引起:
sample.Main.start(Main.java:29)处的java.lang.NullPointerException
com.sun.javafx.application.launchempl.lambda$launchApplication1$162(launchempl.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(本机方法)
com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
在
com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95)
在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法)
com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 还有一个
进程已完成,退出代码为1
答案是将控制器和应用程序类分开,这样您就不会得到应用程序类的两个实例。否则,应用程序类的一个实例将不会初始化某些成员,并最终引发空指针异常
package plot;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;
import java.io.File;
public class Controller {
@FXML // fx:id="openButton"
private Button openButton; // Value injected by FXMLLoader
@FXML
public void open(ActionEvent e) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showOpenDialog(openButton.getScene().getWindow());
System.out.println(file);
}
}
请参阅相关内容(我建议研究第一个链接以了解此解决方案的工作原理)
package plot;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("Plot.fxml"));
Parent root = loader.load();
stage.setTitle("Plotter");
stage.setScene(new Scene(root, 1024 , 768));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<BorderPane prefHeight="400.0" prefWidth="723.0" xmlns="http://javafx.com/javafx/null"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="plot.Controller"
>
<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" onAction="#open"/>
<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>