2014-09-25 2 views
1

다른 스레드를 닫지 않고 창을 닫을 때 JavaFX 응용 프로그램을 실행하는 스레드를 종료하려고합니다. 나는이 프로그램을 시작할 때 테스트 목적으로JavaFX : 창 닫기 후 응용 프로그램 스레드가 종료되지 않습니다.

package testIt; 

import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.stage.WindowEvent; 

import java.io.IOException; 

public class MemoryVisualizerApp extends Application{ 


public static void main(String[] args) { 
    launch(args); 
} 

//Setup the scene and launch with given properties 
@Override 
public void start(Stage primaryStage) throws IOException{ 
    Parent root = FXMLLoader.load(getClass().getResource("/MemoryVisualizer.fxml")); 
    Scene scene = new Scene(root, 650, 340); 
    //Set whether the screen should be re-sizable (possibly best size = default) 
    primaryStage.setResizable(true); 
    primaryStage.setMinHeight(300); 
    primaryStage.setMinWidth(550); 
    primaryStage.setTitle("MINT Performance"); 
    primaryStage.setScene(scene); 
    scene.getStylesheets().add("testIt/MemoryVisualizer.css"); 
    primaryStage.show(); 
    primaryStage.show(); 
    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() 
      { 
       public void handle(WindowEvent e){ 
        System.out.println("test"); 
        try { 
         stop(); 
        } catch (Exception e1) { 
         e1.printStackTrace(); 
        } 
       } 
      }); 
} 

} 

는이 앱이 유일하게 실행, 그리고 내가 창을 닫으 때, 전체 프로그램을 종료해야합니다이 내 응용 프로그램 클래스입니다. 하지만 난 여전히 프로그램을 종료 할 수있는 옵션이 있습니다 (나는 이클립스를 사용하고 있으며 빨간색 사각형은 여전히 ​​클릭 가능합니다), 스레드가 여전히 활성화되어 있음을 의미합니다.

GUI 창을 닫은 후에이 스레드가 종료되도록하려면 어떻게해야합니까?

+0

'setOnCloseRequest()'를 사용하지 않고 종료해야합니다. 이 메서드를 제거하고 사용한 두 stage.show() 중 하나를 주석 처리 한 후에 어떤 일이 발생하는지 알려 주실 수 있습니까? – ItachiUchiha

답변

0

당신은 Platform.exit() 대신 사용할 수 있습니다 stop()

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() 
    { 
     public void handle(WindowEvent e){ 
      System.out.println("test"); 
      try { 
       Platform.exit(); 
      } 
      catch (Exception e1) { 
       e1.printStackTrace(); 
      } 
     } 
    }); 

Take a look at the JavaFX Application life cycle

+0

그게 전부지만, 내가 그랬어 그리고 그것은 아직 종료되지 않습니다. 읽은 사각형은 여전히 ​​앱 종료를 허용합니다. –

+0

primaryStage.show()를 두 번 호출합니다! – Indrajith

+0

잘 잡으십시오, 나는 두번째 것을 삭제했습니다, 그러나 그것은 문제를 해결하지 못했습니다. –

0

정지를 무시하십시오() 메소드 :이 응용 프로그램이 실행 중지 될 것입니다

@Override 
public void stop(){ 
    System.exit(0); 
} 

IDE에서.