简体   繁体   English

JavaFX:OSX的“应用程序菜单”中缺少“关于”菜单项

[英]JavaFX : 'About' MenuItem is missing from the 'Application Menu' in OSX

I created a hello World Application in JavaFX and I will be using both QuitHandler and AboutHandler, I've noticed that 'About' and 'Preferences' menu Items are not shown in the menu : 我在JavaFX中创建了一个Hello World应用程序,并且我将同时使用QuitHandler和AboutHandler,我注意到“ About”和“ Preferences”菜单项未显示在菜单中:

在此处输入图片说明

How can I make it look like : 我如何使其看起来像:

在此处输入图片说明

Also , QuitHandler is not executing the check save behaviour , here is the code of the javaFX Hello World. 另外,QuitHandler也不执行检查保存行为,这是javaFX Hello World的代码。

package helloworld;

import com.apple.eawt.AppEvent;
import com.apple.eawt.QuitResponse;

import java.security.AccessControlException;
import java.util.Optional;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Sample application.
 * @author me
 */
public class HelloWorldApplication extends Application {
// constants --------------------------------------------------------------------------------
   private static final Logger LOG = LogManager.getLogger();

   /**
    * Running on Mac platform.
    */
   public static final boolean MAC;

   static {
      boolean mac = false;
      try {
         final String osName = System.getProperty("os.name");
         LOG.debug("OS: {}", osName);
         mac = osName != null && osName.toLowerCase().contains("mac");
      } catch (AccessControlException ex) {
         LOG.debug("Cannot determine OS");
      }
      MAC = mac;
   }

// member variables -------------------------------------------------------------------------
   private final Alert mAlert = new Alert(Alert.AlertType.CONFIRMATION);

// methods ----------------------------------------------------------------------------------
   @Override
   public void start(Stage pStage) {
      final BorderPane root = new BorderPane();

      final MenuBar menuBar = new MenuBar();

      final Menu fileMenu = new Menu("_File");
      menuBar.getMenus().add(fileMenu);

      // Exit
      boolean macQuitMenuItem = false;
      if (MAC) {
         final com.apple.eawt.Application application = com.apple.eawt.Application.getApplication();
         try {
            application.setQuitHandler(
               (AppEvent.QuitEvent pEvt, QuitResponse pQuitResponse) -> Platform.runLater(
                  () -> exit(pQuitResponse::cancelQuit)
               )
            );
            macQuitMenuItem = true;

            // occurs when running as untrusted applet
         } catch (AccessControlException ex) {
            LOG.debug("Cannot listen for application quit");
         }
      }
      if (!macQuitMenuItem) {
         fileMenu.getItems().add(new SeparatorMenuItem());

         final MenuItem exitMenuItem = new MenuItem("E_xit");

         exitMenuItem.setOnAction(pEvt -> exit(() -> {}));
         fileMenu.getItems().add(exitMenuItem);
      }


      root.setTop(menuBar);
      root.setCenter(new TextArea("Hello, world!"));

      final Scene scene = new Scene(root, 300, 250);

      pStage.setTitle("Hello World!");
      pStage.setScene(scene);

      mAlert.initOwner(pStage);
      mAlert.setTitle("Confirm Exit");
      mAlert.setHeaderText("Look, a Confirmation Dialog");
      mAlert.setContentText("Are you sure you want to exit?");

      pStage.show();
   }

   public static void main(String[] pArgs) {launch(pArgs);}

   public void exit() {exit(() -> {});}

   public void exit(Runnable pAbortExitRunnable) {
      LOG.info("Exit");
      if (checkUnsavedChanges()) {
         Platform.exit();
      } else {
         pAbortExitRunnable.run();
      }
   }

   private boolean checkUnsavedChanges() {
      final Optional<ButtonType> result = mAlert.showAndWait();
      return result.isPresent() && result.get() == ButtonType.OK;
   }
}

I want to know what is wrong with this code ? 我想知道这段代码有什么问题吗?

Edit 1 : pom.xml to compile and test this code. 编辑1:pom.xml来编译和测试此代码。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>app</groupId>
    <artifactId>helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.yuvimasory</groupId>
            <artifactId>orange-extensions</artifactId>
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>helloworld.HelloWorldApplication</mainClass>
                        </manifest>
                    </archive>

                </configuration>

            </plugin>
        </plugins>
    </build>
</project>

Getting the OSX menu to work with a JavaFX application is tricky business. 使OSX菜单与JavaFX应用程序一起使用是一件棘手的事情。 I can recommend you to have a look at https://github.com/codecentric/NSMenuFX 我可以建议您看看https://github.com/codecentric/NSMenuFX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM