2017-06-20 445 views
1

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

Simple image of the program

例如如果u点击菜单栏的设置,在同一个窗口中的另一个场景将出现,并且您可以更改程序的设置。

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

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

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

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

+0

菜单不产生事件,如果它们是空的(不幸)。大概你最好的选择是向HBox(或“ToolBar”)添加一些按钮(或者可能只是标签?),并将它们设置为菜单形式,就像你描述的那样。如果你不能按照你想要的方式工作,我建议尝试这个方法,并发布一个具体的问题,试图完成这项工作。 –

回答

1

下面是我的以前的项目的部分。 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); 
    } 
} 
相关问题