2016-04-23 96 views
0

我的代码编译,但我有空的窗口。我认为FXML加载不是很好。感谢帮助!空窗口JavaFX

package application; 


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

public class Main extends Application { 

public static void main(String[] args) { 
     launch(args); 
    } 
@Override 
public void start(Stage primaryStage) { 

    try{ 
    StackPane stackPane = new StackPane(); 
    FXMLLoader loader = new FXMLLoader(); 
    loader.setLocation(Main.class.getResource("/StackPaneWindow.fxml")); 
    Scene scene = new Scene(stackPane); 

    primaryStage.setTitle("Hello World!"); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

}

StackPaneWindows.fxml只有按钮。我不能粘贴。

回答

1

您需要加载FXML文件:

FXMLLoader loader = new FXMLLoader(); 
loader.setLocation(Main.class.getResource("/StackPaneWindow.fxml")); 
Parent node = loader.load() 
StackPane stackPane = new StackPane(node); 

而且你可以把StackPane为根节点在FXML文件,那么你就可以加载该文件,如:

FXMLLoader loader = new FXMLLoader(); 
loader.setLocation(Main.class.getResource("/StackPaneWindow.fxml")); 
Scene scene = new Scene(loader.load); 
+0

兄弟!你真棒! – Brade