【问题标题】:How to set menu bar with child window in javafx?如何在 javafx 中设置带有子窗口的菜单栏?
【发布时间】:2017-10-11 05:35:06
【问题描述】:

       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(); 
    }

这是我添加带有边框窗格的菜单栏的代码部分,但它挂起我的应用程序,因为我无法登录或做任何事情,我还必须添加子窗口以供参考我附加图像

【问题讨论】:

  • 图片链接失效!您是否尝试过使用 fxml 方法来设置您的 gui 样式?
  • 不,我不知道 fxml
  • 我先询问菜单栏,然后​​再询问子窗口
  • 您可以创建一个BorderPane 并将MenuBar 设置为其top,在下面(如center)您可以放置​​一个始终位于MenuBar 下方的视图然后切换到另一个视图...使用 fxml 进行布局非常有用,请尝试使用 SceneBuilder (gluonhq.com/products/scene-builder)。
  • 没有fxml有什么办法吗?

标签: java javafx menubar childwindow


【解决方案1】:

这是在BorderPaneMenuItems 的中心处理视图的一个非常基本且最小的示例:

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);
    }
}

希望对你有帮助……

编辑 我刚看到你最新的编辑……如果你真的需要不同的窗口(场景或舞台),方法会变得更复杂。读者必须获得有关不同窗口的更多信息(例如您如何创建它们、处理它们的内容等等)。

【讨论】:

  • 它是正确的,但很难与舞台上不同项目的完整动作相结合,因为它是相互关联的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多