javafx表单没有出现

时间:2017-10-11 14:48:54

标签: java eclipse javafx scenebuilder

当我运行javafx程序时没有显示我创建的javafx windows窗体。控件显示如下图所示..

这是我的代码:

    package Employee;                                                              

import java.io.IOException;                                                    

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

public class main extends Application {                                        

    private Stage primaryStage;                                                
    private BorderPane mainLayout;                                             
    @Override                                                                  
    public void start(Stage primaryStage) throws IOException {                 
        this.primaryStage=primaryStage;                                        
        this.primaryStage.setTitle("My windows");                              
        showMainView();                                                        

    }                                                                          
    private void showMainView() throws IOException                             
    {                                                                          
        FXMLLoader loader=new FXMLLoader();                                    
        loader.setLocation(main.class.getResource("view/Mainview.fxml"));      
        mainLayout=loader.load();                                              
        Scene scene=new Scene(mainLayout);                                     
        primaryStage.setScene(scene);                                          

    }                                                                          


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

}

执行后,控制台显示如下 enter image description here

2 个答案:

答案 0 :(得分:1)

两件事:

  • 您的FXML中的mainLayout不是BorderPane
  • 您错过了对show的电话:

    primaryStage.show();

答案 1 :(得分:0)

view/Mainview.fxml BorderPane的根?如果不是,您将获得类似于您所看到的ClassCastException。即使它是,你可能需要将这一行写成一个明确的演员:mainLayout=(BorderPane)loader.load()此外,你需要在设置场景后调用primaryStage.show(),否则你的窗口赢了&# 39; t出现。