问题描述
如何在舞台上添加 MenuBar
?我曾尝试将其包含在我的 GridPane
中(已将其所有其他元素添加到其中),但它看起来好像从未附加到窗口的顶部.
How do I add a MenuBar
to a stage? I have tried including it in my GridPane
(which I have added all my other elements to) but it never looks as if it is attached to the top of the window.
总有办法解决吗?
我使用此代码将其放置在布局中.
I used this code to place it in the layout.
layout.add(menuBar,0,1,10,1);
其中 layout
是 GridPane
,而 menuBar
是 MenuBar
,其中添加了菜单项.
Where layout
is the GridPane
, and menuBar
is the MenuBar
with the menu things added.
推荐答案
如果在代码中执行此操作,请尝试使用更好的节点作为 root
.当我有一个 MenuBar
时,我喜欢使用 VBox
.您可以在 MenuBar
之后添加 GridPane
.
If you are doing this in code, try using a better node as the root
. When I have a MenuBar
, I love to use VBox
. You can add your GridPane
after the MenuBar
.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication249 extends Application
{
@Override
public void start(Stage primaryStage)
{
Menu miFile = new Menu("File");
MenuBar menuBar = new MenuBar();
menuBar.getMenus().add(miFile);
VBox root = new VBox(menuBar);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
这篇关于将菜单栏添加到Gridpane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!