2013-04-23 63 views
0

我有工作不正常此JavaFX的菜单:调整大小菜单进入层

public class CreatingMenus { 

    // Generate Menu 
    public void initMenu(Stage primaryStage, Group root, Scene scene) { 

     // Call Menu Actions from Java Method MenuActions 
     final MenuActions ma = new MenuActions(); 

     MenuBar menuBar = new MenuBar(); 

     // File menu - new, save, close, exit 
     Menu menu = new Menu("File"); 
     menu.getItems().add(new MenuItem("New")); 
     menu.getItems().add(new MenuItem("Save")); 
     menu.getItems().add(new MenuItem("Close")); 
     menu.getItems().add(new SeparatorMenuItem()); 

     MenuItem menuItem = new MenuItem("Exit"); 

     // Exit from the application 
     menuItem.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent e) { 
       // This Java Method is called from the Java Class MenuActions 
       ma.programExit(); // Exit program 
      } 
     }); 

     menu.getItems().add(menuItem); 

     menuBar.getMenus().add(menu); 

     // Options menu - Preferences 
     Menu options = new Menu("Options"); 
     options.getItems().add(new MenuItem("Preferences")); 

     menuBar.getMenus().add(options); 

     // Help menu - About 
     Menu help = new Menu("Help"); 

     MenuItem helpItem = new MenuItem("About"); 

     // Exit from the application 
     helpItem.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent e) { 
       aboutDialog(); 
      } 
     }); 

     help.getItems().add(helpItem); 

     menuBar.getMenus().add(help); 

     menuBar.prefWidthProperty().bind(primaryStage.widthProperty()); 

     menuBar.setLayoutX(0); 
     menuBar.setLayoutY(0); 

     root.getChildren().add(menuBar); 

    } 

    public void aboutDialog() { 

     final int xSize = 640; 
     final int ySize = 480; 
     final String logoImage = "/logo.png"; 
     final Color backgroundColor = Color.WHITE; 
     final String text = "SQL Browser"; 
     final String version = "Product Version: 1.0"; 
     final String license = "License Information"; 

     final Stage aboutDialog = new Stage(); 
     aboutDialog.initModality(Modality.WINDOW_MODAL); 

     GridPane grid = new GridPane(); 
     grid.setAlignment(Pos.CENTER); 
     grid.setHgap(10); 
     grid.setVgap(10); 
     grid.setPadding(new Insets(20, 20, 20, 20)); 

     // Logo 
     Image image = new Image(getClass().getResourceAsStream(logoImage)); 
     ImageView logo = new ImageView(image); 

     grid.add(logo, 1, 0); 

     // Product name 
     Text productName = new Text(text); 
     productName.setFont(Font.font("Verdana", 12)); 
     grid.add(productName, 0, 2); 

     // Product version 
     Text productVersion = new Text(version); 
     productVersion.setFont(Font.font("Verdana", 12)); 
     grid.add(productVersion, 0, 3); 

     // Product License 
     Text productLicense = new Text(license); 
     productLicense.setFont(Font.font("Verdana", 12)); 
     grid.add(productLicense, 0, 4); 

     // Close Button 
     Button closeButton = new Button("Close"); 

     closeButton.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent arg0) { 
       // Close the dialog when "Close" button is pressed 
       aboutDialog.close(); 
      } 
     }); 
     grid.add(closeButton, 5, 18); 

     // Configure dialog size and background color 
     Scene aboutDialogScene = new Scene(grid, xSize, ySize, backgroundColor); 
     aboutDialog.setScene(aboutDialogScene); 
     aboutDialog.show(); 

    } 
} 

当我加入这个菜单它覆盖其他组件到应用程序和他们没有责任,因为我想他们是这背后层。你能告诉我如何在单独的图层中隔离这个菜单,因为我不想放置其他组件。

编辑

当我使用菜单中的代码,我得到这样的结果:

enter image description here

当我删除的菜单代码,我得到这样的结果,应用程序运行正常:

菜单代码的某处存在问题。

+0

请添加[SSCCE](http://sscce.org/)。 – Kai 2013-04-23 08:09:28

+0

我更新了帖子。 – 2013-04-23 08:38:34

+0

也许您已将菜单添加到错误的父级。你的代码不会显示你要添加它的“Group”以及之前和之后发生的事情。 – Kai 2013-04-23 09:03:46

回答

1

适用于我,没有任何变化。你可能应该检查你的Java/JavaFx版本。我在Windows上使用1.7_17,64位。 enter image description here

我添加了一些图片(我没有你的图片),以确保你得到的行为不是因为他们。

+0

我不使用Netbeans,抱歉。您也可以使用任何其他Java安装从命令行运行您的项目。 – Kai 2013-04-23 09:51:38

+0

也许这是javafx在centos上的一个问题,是的。为了在这个背后尝试运行其中一个[示例](http://docs.oracle.com/javafx/2/ui_controls/menu_controls.htm)。您的代码在Windows 7上的机器上工作。 – Kai 2013-04-23 10:01:09

+0

我找到了导致问题的原因!我删除这行:'menuBar.prefWidthProperty()。bind(primaryStage.widthProperty());'这个菜单不是自动调整大小的。 – 2013-04-23 12:45:57

2

场景宽度绑定

舞台宽度包含窗口边界和标题栏 - 这是不相关的内部布局。

要使用结合充满东西的场景宽度,事物的优选尺寸绑定到场景大小,而不是载物台尺寸:

menuBar.prefWidthProperty().bind(menuBar.getScene().widthProperty()); 

或者(通常优选的),可以使用替代的布局窗格和自动执行大小调整的布局约束。对于您的特定布局,BorderPane可能会有用。

您的执行过程中可能会有其他问题导致问题(我没有检查过)。

的布局使用组

你的引擎收录代码使用Group主场景布置根源。对于像您的示例那样布局灵活大小的屏幕,组通常是一个糟糕的选择。一个团体的规模并不大。相反,建议使用上面提到的可调整大小的layout pane

工具支持

  1. ScenicView是在调试现有布局的问题非常宝贵的。
  2. SceneBuilder适用于各种布局的快速原型设计。

我强烈建议您使用这些工具来帮助理解布局如何工作并为您提供调试布局问题所需的工具和知识。