如何在javafx中设置带子窗口的菜单栏?

时间:2017-10-11 05:35:07

标签: java javafx menubar childwindow

as the menu bar is coming in the middle

       vbox2.setPadding(new Insets(3));
        vbox2.setSpacing(3);
         vbox2.getChildren().addAll( browser1,browser);

        HBox.setHgrow(vbox2, Priority.ALWAYS);


        hbox.setPadding(new Insets(20));
//        StackPane.setMargin(hbox, new Insets(20));
        hbox.getChildren().addAll(vbox, vbox2);
        root.getChildren().add(hbox);
        Scene scene = new Scene(root, 500, 300); // the stack pane is the root node
        //scene.setCursor(Cursor.CROSSHAIR);
           MenuBar menuBar = new MenuBar();
             Menu menu = new Menu("Window");
        menu.getItems().add(new MenuItem("browser"));
        menu.getItems().add(new MenuItem("img"));
        menuBar.getMenus().add(menu); 
         menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
    BorderPane borderPane = new BorderPane();

        borderPane.prefHeightProperty().bind(scene.heightProperty());
        borderPane.prefWidthProperty().bind(scene.widthProperty());

        borderPane.setTop(menuBar);
           root.getChildren().add(borderPane);
        primaryStage.setScene(scene);
        primaryStage.show(); 
    }

这里是代码部分,我正在添加菜单栏和边框窗格,但它挂起我的应用程序,因为我无法登录或做任何事情,我不得不添加子窗口也参考我附加图像

image after adding borderpane with menu bar

1 个答案:

答案 0 :(得分:0)

这是BorderPane MenuItem的{​​{1}}中心处理视图的一个非常基本且极少的示例:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            // create the menu bar with a menu and its items
            MenuBar menuBar = new MenuBar();
            Menu mainMenu = new Menu("Window");
            MenuItem browserItem = new MenuItem("browser");
            MenuItem imageItem = new MenuItem("image");
            MenuItem closeItem = new MenuItem("exit");
            // create some different contents for the center of the border pane
            Label imagePlaceHolder = new Label("IMAGE TO BE SHOWN");
            WebView browser = new WebView();
            WebEngine browserEngine = browser.getEngine();

            // set the actions for the different items
            closeItem.setOnAction(action -> {
                System.exit(0);
            });

            imageItem.setOnAction(action -> {
                root.setCenter(imagePlaceHolder);
            });

            browserItem.setOnAction(action -> {
                root.setCenter(browser);
                browserEngine.load("http://www.google.com");
            });

            // add items to the menu, then the menu to the menu bar
            mainMenu.getItems().addAll(closeItem, browserItem, imageItem);
            menuBar.getMenus().add(mainMenu);

            // set the scene
            Scene scene = new Scene(root,400,400);
            root.setTop(menuBar);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

我希望它有所帮助...

修改 我刚看到你最新的编辑...如果你真的需要不同的窗口(场景或阶段),这种方法将变得更加复杂。读者必须获得有关不同窗口的更多信息(例如,如何创建它们,处理它们的内容等等)。