2016-07-07 3 views
2

전에 코드를 표시하기 위해 노력 : 프로젝트의자바 FX : 대화 상자가 응용 프로그램

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); 
    } 
} 

내 계획하는 것은이 : 메인 애플리케이션 전에 텍스트 입력 대화 쇼를 위로하고 사용자의 이름을 반환합니다. 그러면 대화 상자가 닫히고 주 응용 프로그램이 열립니다.

오류 메시지 : 사전에

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$launchApplication$155(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$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) 
    ... 1 more 
Exception running application chat.ChatbotRunner 

감사합니다.

+0

당신은 프리 로더입니까? 프리 로더를 사용할 수 있으며, 사용자가 버튼을 클릭 할 때 체크를하고 응용 프로그램에로드가 완료되었음을 알립니다. 앱이 시작됩니다. – kamel2005

+0

Eclipse를 사용하고 있는데 이전에 프리 로더를 시도한 적이 없습니다. –

+0

응용 프로그램이 열리기 전에 이러한 대화 상자 중 하나를 사용하는 것에 대해 이야기하고 있습니다. http://code.makery.ch/blog/javafx-dialogs-official/ –

답변

4

기본 단계가 표시되지 않기 때문에 대화 상자에서이를 소유자로 사용할 수 없습니다. initOwner()에 대한 호출을 제거하면이 작업이 실행됩니다.

+0

대화 상자가 성공적으로 작동했습니다. 감사합니다! –

+2

@HarrisonThacker이 질문에 대한 답변이 있으면 적어도 받아 들인 대답으로 표시하십시오. –