2017-08-12 52 views
0

我應該爲GUI創建單獨線程還是自動創建它?如果我應該如何做到這一點? 我不明白我如何運行GUI。用於GUI的單獨線程

package sample; 

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

public class MyMain extends Application implements Runnable 
{ 
@Override 
public void run() 
{ 

} 

@Override 
public void start(Stage primaryStage) throws Exception 
{ 
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 
    primaryStage.setTitle("Hello World"); 
    primaryStage.setScene(new Scene(root, 200, 300)); 
    primaryStage.setMinWidth(220); 
    primaryStage.setMinHeight(340); 
    primaryStage.show(); 
} 

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

回答

1

您不必創建新線程。只需使用此代碼:

public class MyMain extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 
     primaryStage.setTitle("Hello World"); 
     primaryStage.setScene(new Scene(root, 200, 300)); 
     primaryStage.setMinWidth(220); 
     primaryStage.setMinHeight(340); 
     primaryStage.show(); 
    } 

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

Application類本身負責線程的。