如何在javaFx中为一个场景创建一个菜单栏 [英] How to Acheive One Menubar for more then one scenes in javaFx

查看:120
本文介绍了如何在javaFx中为一个场景创建一个菜单栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是Java FX 2的新手。

I am basically new to Java FX 2.

场景:

我有3个场景和我想要一种方法来添加菜单栏,这样我就不想从前一个场景中明确删除菜单栏并将其添加到新场景中。像某些东西,父场景或某种方式菜单栏附加到舞台。我的意思是菜单栏只添加了一次,并且无论前面或下面的场景是什么都会出现。

I have 3 Scenes and I want a way to add menu-bar such that I don't i don't want to explicitly remove the menu bar from previous scene and add it to new one. Like Some thing a Parent Scene or some way menu-bar is attached to Stage. I mean menu-bar is added just one time and always be present whatever scene is in front or not.

如果这是可能的我该怎么做。

If This is Possible How Can I do this.

这是Oracle Docs提供的默认示例JavaFX http://docs.oracle.com/javafx/2/ui_controls/ MenuSample.java.html

Here is the Default Example Provided by Oracle Docs of JavaFX http://docs.oracle.com/javafx/2/ui_controls/MenuSample.java.html

public class Main extends Application {
 final ImageView pic = new ImageView();
 final Label name = new Label();
 final Label binName = new Label();
 final Label description = new Label();

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

 @Override
 public void start(Stage stage) {

   stage.setTitle("Menu Sample");
   Scene scene = new Scene(new VBox(), 400, 350);
   scene.setFill(Color.OLDLACE);

   MenuBar menuBar = new MenuBar();

   // --- Graphical elements
    final VBox vbox = new VBox();
    vbox.setAlignment(Pos.CENTER);
    vbox.setSpacing(10);        
    vbox.setPadding(new Insets(0, 10, 0, 10));
    makeContentsForVBox();// in this vBox area will be fill with name pic desrciption
    vbox.getChildren().addAll(name, binName, pic, description); // name is lable


   // --- Menu File
    Menu menuFile = new Menu("File");
    MenuItem add = new MenuItem("Shuffle",
        new ImageView(new Image(getClass().getResourceAsStream("new.png"))));
    add.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            shuffle();
            vbox.setVisible(true);
        }
    });

    MenuItem clear = new MenuItem("Clear");
    clear.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));
    clear.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            vbox.setVisible(false);
        }
    });

    MenuItem exit = new MenuItem("Exit");
    exit.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            System.exit(0);
        }
    });

    menuFile.getItems().addAll(add, clear, new SeparatorMenuItem(), exit);
    ((VBox) scene.getRoot()).getChildren().addAll(menuBar, vbox);

    stage.setScene(scene);
    stage.show();
 }
}

所以这里menuBar被添加到场景中。如果我交换场景并在前面带来另一个场景......我该怎么做。我想我从这个场景中删除menuBar并添加到其他或只是添加到新的。因此,每当我改变时,我都必须这样做。有没有办法避免这种情况?

So Here menuBar is added to a scene. if i swap the scene and bring an other scene in front ... What will i do. i think I remove menuBar from this scene and add to other or simply add to new one. so every time i have to do this when i change. Is there any way to avoid this??

推荐答案

我更喜欢的方法是使用场景 BorderPane 作为其根

The approach I would prefer is to use a Scene with BorderPane as its root

scene.setRoot(borderPane);

您可以将 MenuBar 添加到顶部 BorderPane 及其中心你可以放置 SplitPane

You can add the MenuBar to the top of the BorderPane and at its Center you can place SplitPane

BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(splitPane);

每当你需要切换到 WebView 时将其替换为 SplitPane

Whenever you need to switch to WebView just replace it with SplitPane :

borderPane.setCenter(webView);

按照这种方法,您的 MenuBar 将始终如一保持在 TOP ,您可以在 SplitPane WebView

Following this approach, your MenuBar will always remain on TOP and you can switch between SplitPane and WebView

这篇关于如何在javaFx中为一个场景创建一个菜单栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆