【问题标题】:Click event on menu in javafxjavafx中菜单上的单击事件
【发布时间】:2017-06-20 22:10:46
【问题描述】:

我想制作一个程序,如果用户点击菜单项,我们可以改变场景。

Simple image of the program

例如,如果您单击菜单栏中的设置,将显示同一窗口中的另一个场景,您可以更改程序的设置。

注意:我的菜单没有任何菜单项。只有菜单栏。

到目前为止我尝试了什么? 向 HBox 添加一些按钮并将其分配到 BorderPane 的顶部。它确实可以工作,但看起来不像菜单。试图使它看起来像带有 CSS 的菜单但没有工作。

有什么问题? 问题是主菜单上的单击处理程序不起作用。 如果我将点击事件处理程序分配给开始按钮,它确实有效,但不在“设置”菜单上。

想知道实现这个想法的最佳方法是什么?

【问题讨论】:

  • 如果菜单为空,则菜单不会生成事件(很遗憾)。可能您最好的选择是在HBox(或ToolBar)中添加一些按钮(或者只是标签?)并将它们设置为菜单,如您所描述的那样。我建议您尝试一下并发布一个具体问题,如果您无法按照您想要的方式进行操作。

标签: user-interface javafx menubar mouseclick-event


【解决方案1】:

已经一年多了,但这里有一个简单的解决方案,Menu 可以点击:

<MenuBar>
        <Menu>
            <graphic>
                <Label fx:id="yourId" text="Your Text here" onMouseClicked="#mouseClickedEvent"/>
            </graphic>
        </Menu>
</MenuBar>

【讨论】:

    【解决方案2】:

    以下是我之前项目的部分内容。 MenuItem 在不同的类中,我在 main 中调用一个方法来切换场景。

    我有两个页面选择和信息,都有自己的容器和场景和样式表。选择页面是开始时显示的初始页面,我切换信息页面。

    settingsMenuItem.setOnAction(e -> {
        e.consume();
        Launcher.selectionPage();
    });
    

    我的主要课程:

    public class Launcher extends Application {
    
        private static FlowPane selectionPane;
        private static BorderPane infoPane;
        private static Scene selectionScene, infoScene;
        private static Stage theStage;
        private static String selectionCSS;
        private static String informationCSS;
    
        public static void main(String args[]) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) throws Exception {
    
            //Global reference needed to switch scenes in another method.
            this.theStage = primaryStage;
    
            //Declares resources, in this case stylesheet. Declared here to be applied in another method 
            selectionCSS = this.getClass().getResource("/views/SelectionStyle.css").toExternalForm();
            informationCSS = this.getClass().getResource("/views/InformationStyle.css").toExternalForm();
    
            //Initial page setup
            selectionPane = new SelectionPage();
            selectionScene = new Scene(selectionPane, 500, 500);
            selectionScene.getStylesheets().add(selectionCSS);
    
            //Stage setup
            primaryStage.setScene(selectionScene);
            primaryStage.show();
        }
    
    
        //Changes page
        public static void informationPage(String starSign) {
    
            infoPane = new InformationPage();
            infoScene = new Scene(infoPane, 500, 270);
            infoScene.getStylesheets().add(informationCSS);
            theStage.setScene(infoScene);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 2017-07-16
      相关资源
      最近更新 更多