JavaFX Button onAction不会打开选定的FXML视图

时间:2017-05-02 22:33:01

标签: javafx controller

我暂时停留在'菜单屏幕'。

当我点击ACTIVATED CUSTOMER PANEL时,它不会打开CustomerDisplay.fxml

Main Screen

以下是我的代码;

public class MainApp extends Application {

protected Parent content;
private Stage primaryStage;
private Stage secondStage;
private CustomerController custCtrl;
private MaintainerController mntnCtrl;
private MachineryController machCtrl;
private String fxml="";
public static MainApp instance;

public MainApp () {
    instance=this;
}

/**
 * @param args
 */
public static void main(String[] args) {    
    launch(args);
}

public static MainApp getInstance() {
    return instance;
}

@Override
public void start(Stage primaryStage) throws Exception {
    initializePanel();
    Scene scene = new Scene(content);

    primaryStage.setResizable(false);
    primaryStage.initStyle(StageStyle.UTILITY);

    primaryStage.setScene(scene);
    primaryStage.show();
}

private void initializePanel() throws IOException{
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("fxml/SimulatorDisplay.fxml"));
    content = loader.load();        
}

public void openCustomerPanel() throws IOException{
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("fxml/CustomerDisplay.fxml"));
    content = loader.load(); 
}

public void openMaintainerPanel() throws IOException{
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("fxml/MaintainerDisplay.fxml"));
    content = loader.load();        
}

public void openMachineryPanel() throws IOException{
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("fxml/MachineryDisplay.fxml"));
    content = loader.load();        
}   

}

public class SimulatorController implements Initializable{{
.
.
.
    @FXML
    public void clickCustomer (ActionEvent event) throws IOException{
        log.info("Starting Customer Panel");
        MainApp.getInstance().openCustomerPanel()**;
    }
}

它打印log.info(“启动客户面板”),但我看不到任何新窗口。 我想知道如何点击主屏幕上的按钮,并显示新窗口。除非我们关闭它,否则主屏幕仍然打开。我们需要定义新舞台吗?

1 个答案:

答案 0 :(得分:0)

基于James_D的评论; 这是工作代码:

        public class MainApp extends Application {

        public static MainApp instance;
        private Stage secondaryStage;
    .
    .
    .

        public void openCustomerPanel() throws IOException{
            FXMLLoader loader = new FXMLLoader();
            secondaryStage= new Stage();
            loader.setLocation(getClass().getResource("fxml/CustomerDisplay.fxml"));
            content = loader.load(); 
            Scene scene = new Scene(content);
            secondaryStage.setScene(scene);
            secondaryStage.show();
        }
    }