Tre*_*kaz 7 java macos javafx javafx-8
带有屏幕菜单栏的简单玩具应用程序可以像这样在Java FX 8中编写:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(final String[] args) throws Exception {
launch(Main.class, args);
}
@Override
public void start(Stage stage) throws Exception {
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu("File");
MenuItem newNotebookMenuItem = new MenuItem("New Notebook...");
newNotebookMenuItem.setAccelerator(KeyCombination.keyCombination("Meta+N"));
newNotebookMenuItem.setOnAction(event -> { System.out.println("Action fired"); });
fileMenu.getItems().add(newNotebookMenuItem);
menuBar.getMenus().add(fileMenu);
menuBar.setUseSystemMenuBar(true);
VBox root = new VBox();
root.getChildren().add(menuBar);
Scene scene = new Scene(root, 400, 350);
stage.setScene(scene);
stage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
这可能适用于制作计算器应用程序,如只有一个窗口的应用程序,并且当最后一个窗口关闭时应用程序关闭,但我的应用程序是基于文档的.当基于文档的应用程序没有打开文档时,没有打开窗口,但菜单应保持可见,以便用户可以打开新文档.
通常的方法是setDefaultMenuBar在com.apple.eawtAPI中,但这不是很好,因为:
有没有正确的方法设置这个我还没有找到?我希望在Application上找到这样一个方法(一些邮件列表帖子暗示它可能会在那里结束)但看起来它仍然缺失.
| 归档时间: |
|
| 查看次数: |
4521 次 |
| 最近记录: |