JavaFX:试图让对话框出现在应用程序之前
JavaFX: Trying to make dialog appear before application
代码:
import java.io.IOException;
import java.util.Optional;
/**
* Version 0.8
* @author htha9587
* 7-7-16
*/
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TextInputDialog;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ChatbotRunner extends Application
{
private Stage primaryStage;
private BorderPane rootLayout;
/**
* Sets stage with the scene.
*/
@Override
public void start(Stage primaryStage)
{
//Sets up dialog before main application.
TextInputDialog dialog = new TextInputDialog("Harrison");
dialog.setTitle("ChatbotFX");
dialog.setHeaderText("This message brought to you by ChatbotFX.");
dialog.setContentText("What's your name?");
dialog.initOwner(primaryStage);
//Sets Icon.
dialog.setGraphic(new ImageView(this.getClass().getResource("HAL.png").toString()));
//Retrieves response value.
Optional<String> result = dialog.showAndWait();
if (result.isPresent())
{
dialog.setResult("Hello " + result.get());
}
//Sets main stage and scene.
this.primaryStage = primaryStage;
this.primaryStage.setTitle("ChatbotFX");
//Sets Application Icon.
this.primaryStage.getIcons().add(new Image("file:resources/images/HAL.png"));
initRootLayout();
}
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader(ChatbotRunner.class.getResource("view/ChatbotView.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns main stage.
*/
public Stage getPrimaryStage()
{
return primaryStage;
}
/**
* Runs the program.
* @param args
*/
public static void main(String[] args)
{
launch(args);
}
}
我的项目计划是这样的:在主应用程序和 return 用户名之前显示一个文本输入对话框。然后对话框将关闭,然后主应用程序将打开。
错误信息:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication5(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at javafx.scene.control.HeavyweightDialog.updateStageBindings(HeavyweightDialog.java:329)
at javafx.scene.control.HeavyweightDialog.initOwner(HeavyweightDialog.java:123)
at javafx.scene.control.Dialog.initOwner(Dialog.java:479)
at chat.ChatbotRunner.start(ChatbotRunner.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(WinApplication.java:191)
... 1 more
Exception running application chat.ChatbotRunner
提前致谢。
由于未显示主舞台,对话框无法将其用作所有者。如果您删除对 initOwner() 的调用,这将 运行。
代码:
import java.io.IOException;
import java.util.Optional;
/**
* Version 0.8
* @author htha9587
* 7-7-16
*/
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TextInputDialog;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ChatbotRunner extends Application
{
private Stage primaryStage;
private BorderPane rootLayout;
/**
* Sets stage with the scene.
*/
@Override
public void start(Stage primaryStage)
{
//Sets up dialog before main application.
TextInputDialog dialog = new TextInputDialog("Harrison");
dialog.setTitle("ChatbotFX");
dialog.setHeaderText("This message brought to you by ChatbotFX.");
dialog.setContentText("What's your name?");
dialog.initOwner(primaryStage);
//Sets Icon.
dialog.setGraphic(new ImageView(this.getClass().getResource("HAL.png").toString()));
//Retrieves response value.
Optional<String> result = dialog.showAndWait();
if (result.isPresent())
{
dialog.setResult("Hello " + result.get());
}
//Sets main stage and scene.
this.primaryStage = primaryStage;
this.primaryStage.setTitle("ChatbotFX");
//Sets Application Icon.
this.primaryStage.getIcons().add(new Image("file:resources/images/HAL.png"));
initRootLayout();
}
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader(ChatbotRunner.class.getResource("view/ChatbotView.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns main stage.
*/
public Stage getPrimaryStage()
{
return primaryStage;
}
/**
* Runs the program.
* @param args
*/
public static void main(String[] args)
{
launch(args);
}
}
我的项目计划是这样的:在主应用程序和 return 用户名之前显示一个文本输入对话框。然后对话框将关闭,然后主应用程序将打开。
错误信息:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication5(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at javafx.scene.control.HeavyweightDialog.updateStageBindings(HeavyweightDialog.java:329)
at javafx.scene.control.HeavyweightDialog.initOwner(HeavyweightDialog.java:123)
at javafx.scene.control.Dialog.initOwner(Dialog.java:479)
at chat.ChatbotRunner.start(ChatbotRunner.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(WinApplication.java:191)
... 1 more
Exception running application chat.ChatbotRunner
提前致谢。
由于未显示主舞台,对话框无法将其用作所有者。如果您删除对 initOwner() 的调用,这将 运行。