[英]JavaFX: how to change a scene included in the center of a BorderPane
[英]How to change CenterPane from LeftPane in javaFx borderPane?
我試圖在javafx中創建一個面板,我習慣於將邊框作為主要場景。 中央面板有4個窗口(main1,main2,main3,main4),左側面板中有一個導航菜單。
borderPane.setCenter(mainMenu1.getCenterMain1UI());
//borderPane.setCenter(mainMenu2.getCenterMain2UI());
//borderPane.setCenter(mainMenu3.getCenterMain3UI());
//borderPane.setCenter(mainMenu4.getCenterMain4UI());
public BorderPane getAppWindow(){
if (borderPane == null){
borderPane = new BorderPane();
borderPane.setTop(topPanel.getTopPanelUI());
borderPane.setBottom(bottomPanel.getBottomPanelUI());
borderPane.setLeft(leftPanel.getLeftPanelUI());
borderPane.setCenter(mainMenu.getCenterMainUI());
borderPane.setAlignment(borderPane.getCenter(), Pos.TOP_LEFT);
}
return borderPane;
}
在左側面板控制器中
public class LeftPanelController {
public VBox leftPanelPane;
public Button btnLeftPanelMainmenu;
public Button btnLeftPanelDb;
public Button btnLeftPanelOfficeInfo;
public Button btnLeftPanelConfiguration;
public void btnLeftPanelMainmenuOnClickAction(ActionEvent e){
change border pane center to main
}
public void btnLeftPanelDbOnClickAction(ActionEvent e){
change border pane center to DB
}
public void btnLeftPanelOfficeInfoOnClickAction(ActionEvent e){
change border pane center to DB
}
public void btnLeftPanelConfigurationOnClickAction(ActionEvent e){
change border pane center to configuration
}
}
您需要為每個菜單按鈕設置on action事件,以便它更改BorderPane
中心顯示的內容。
這是一個例子:
import java.util.LinkedHashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class BorderPaneExample extends Application {
private Map<String, Node> menuOptions;
private Node defaultCenterNode = new Label("Example node 1");
@Override
public void start(Stage primaryStage) throws Exception {
menuOptions = new LinkedHashMap<>();
menuOptions.put("Main Menu", defaultCenterNode);
menuOptions.put("History", new Label("Example node 2"));
menuOptions.put("Office Info", new Label("Example node 3"));
menuOptions.put("Configuration", new Label("Example node 4"));
BorderPane root = new BorderPane();
root.setCenter(defaultCenterNode);
VBox menuLayout = new VBox();
for (String key : menuOptions.keySet()) {
Button button = new Button(key);
button.setMaxWidth(Double.MAX_VALUE);
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
root.setCenter(menuOptions.get(key));
}
});
menuLayout.getChildren().add(button);
}
root.setLeft(menuLayout);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.setTitle("BorderPane Example");
primaryStage.setResizable(false);
primaryStage.sizeToScene();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我像這樣在左側面板中更改了按鈕單擊方法;
public void btnLeftPanelMainmenuOnClickAction(ActionEvent e) {
AppWindow.borderPane.setCenter(AppWindow.mainMenu.getCenterMainUI());
}
public void btnLeftPanelDbOnClickAction(ActionEvent e) {
AppWindow.borderPane.setCenter(AppWindow.dbMenu.getCenterDbUI());
}
public void btnLeftPanelConfigurationOnClickAction(ActionEvent e) {
AppWindow.borderPane.setCenter(AppWindow.configMenu.getCenterConfigurationUI());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.