2017-10-19 101 views
-2

我使用Scene Builder來創建一個JavaFX GUI應用程序。我試圖用FXML實現這樣的事情:FXML通過按鈕打開一個新場景

reportButton = new Button("Report"); 
reportButton.setOnAction(e -> ReportPage.display()); 

但我無法弄清楚如何使用控制器頁面來做到這一點。有人可以告訴我,我該怎麼做? 謝謝

+0

請張貼更多的代碼和FXML文件(S)...這真的取決於你如何實施了不同的意見。他們有共同的家長觀點嗎? – deHaar

+0

只需在控制器中定義適當的方法即可。請參閱[文檔](https://docs.oracle.com/javase/9​​/docs/api/javafx/fxml/doc-files/introduction_to_fxml.html#controller_method_event_handlers)。 –

+0

你正在試圖打開一個新的舞臺?改變當前的場景?將視圖放在當前舞臺的容器中? –

回答

0

這裏是如何展示一個新的舞臺。在您添加此代碼對動作功能
(你可以添加使用場景生成器,函數代碼:在action屬性)

@FXML 
private void reportButtonHandler(ActionEvent event) { 
    FXMLLoader fxmlLoader = new 
     FXMLLoader(getClass().getResource("pathtofxml/ReportPage.fxml")); 
    Parent root1 = (Parent) fxmlLoader.load(); 
    Stage stage = new Stage(); 
    //set what you want on your stage 
    stage.initModality(Modality.APPLICATION_MODAL); 
    stage.setTitle("Report Page"); 
    stage.setScene(new Scene(root1)); 
    stage.setResizable(false); 
    stage.show(); 
} 
+0

非常感謝,它的工作原理 – ToniT