2017-10-11 151 views
0

其實我正在創建基於菜單的程序(菜單欄,菜單,menuitem)。從每個菜單項調用新窗口就像那樣。處理當前窗口並在主窗口中加載新的fxml窗口

@FXML 
private Pane rootP; 
public static Pane root; 


@Override 
    public void initialize(URL url, ResourceBundle rb) { 
     root = rootP; 
    } 

@FXML 
void loadWindow2Action(ActionEvent event) throws IOException { 
    root.getChildren().clear(); 
    Pane newLoadedPane = (Pane) FXMLLoader.load(getClass().getResource("window2.fxml")); 
    root.getChildren().add(newLoadedPane); 
} 

現在我有一個Window2 fxml屏幕1個tableview,裏面的表我在每一行放置一個按鈕。
我的問題是如何加載新的fxml頁面和處理當前的fxml頁面,當我點擊該按鈕。

在swing中,我們簡單地配置當前的Internalframe並調用新的Internalframe。就像是 。

StdInternalFrame f = new StdInternalFrame(desktop); 
this.dispose(); 
desktop.add(f); 

enter image description here

這是做這

 

    btnPay.setOnAction(e -> {  
    try {  
     MemberWindowController.root.getChildren().clear();  
     Pane newLoadedPane = (Pane)FXMLLoader.load(getClass().getResource("Window3.fxml"));  
     MemberWindowController.root.getChildren().add(newLoadedPane);  
     } catch (IOException ex) {  
     Logger.getLogger(Window3Controller.class.getName()).log(Level.SEVERE, null, ex);  
     }  
    }); 


回答

0

這其實是非常簡單的,你只需要執行下面的代碼的任何其他更好的辦法每當你想關閉一個階段:

Node node = (Node)event.getSource(); 
Stage stage = (Stage) node.getScene().getWindow(); 
stage.close(); 

詞 「事件」 是指你的動作事件,如:

public void something(ActionEvent event){ 

} 

試試吧,將很好地工作。

+0

您的代碼關閉我的應用程序而不是當前窗口。 – kar