点击javafx菜单上的活动

问题描述 投票:1回答:2

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

Simple image of the program

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

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

到目前为止我做了什么?添加一些按钮到HBox并将其分配到BorderPane的顶部。它确实有效,但看起来不像菜单。想让它看起来像菜单与CSS但没有工作。

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

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

user-interface javafx menubar mouseclick-event
2个回答
1
投票

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

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

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

0
投票

它已经超过一年,但这是一个简单的解决方案,使Menu可以点击:

<MenuBar>
        <Menu>
            <graphic>
                <Label fx:id="yourId" text="Your Text here" onMouseClicked="#mouseClickedEvent"/>
            </graphic>
        </Menu>
</MenuBar>
© www.soinside.com 2019 - 2024. All rights reserved.