简体   繁体   English

OS X + Java +启动画面+ fileDialog:对话框文件中没有文件打开

[英]OS X + Java + Splash image + fileDialog : no files in dialog file open

When I create and run a runnable Java application (JAR) with Splash screen, when activating the File Open dialog, no files are loaded in the dialog. 当我创建并运行带有Splash屏幕的可运行Java应用程序(JAR)时,激活“文件打开”对话框时,对话框中不会加载任何文件。 The search wheel keeps turning and nothing happens. 搜索轮不断转动,什么也没发生。 However when I omit the Splash images (from the Manifest.mf) file, the File Open dialog runs fine. 但是,当我省略Splash图像(来自Manifest.mf)文件时,“文件打开”对话框运行良好。 Also on Windows I have no problem. 同样在Windows上,我没有问题。 Only on OS XI have this problem. 仅在OS XI上存在此问题。 Tested it on both 10.8 and 10.9. 在10.8和10.9上都对其进行了测试。

BTW I deliberately use the older FileDialog class instead of the JFileChooser, because on OS X the fileDialog looks more native OS X. 顺便说一句,我故意使用较旧的FileDialog类而不是JFileChooser,因为在OS X上,fileDialog看起来更像是本机OSX。

BTW the same behaviour is there when I use the AppBundler to create a OS X App and specify the Splash screen as: 顺便说一句,当我使用AppBundler创建OS X App并将Splash屏幕指定为时,存在相同的行为:

I have created a very simple test program to illustrate. 我创建了一个非常简单的测试程序来说明。

What am I missing? 我想念什么? Who can help? 谁能帮忙? Is this a bug? 这是错误吗?

1) The sample Java code. 1)示例Java代码。

package fileDialog_Sample;

import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.plaf.metal.MetalIconFactory;

public class TestSwing extends JFrame {

    Frame frame = null;

    public TestSwing() {

        initUI();
        frame = this;
    }

    private void initUI() {

        JMenuBar menubar = new JMenuBar();
        ImageIcon icon = new ImageIcon("exit.png");

        JMenu file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);

        JMenuItem oMenuItem = new JMenuItem("Open", icon);
        oMenuItem.setMnemonic(KeyEvent.VK_O);
        oMenuItem.setToolTipText("Open file");
        oMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
            System.out.println("In File - Open");
            openFileMenu();
            }
        });
        file.add(oMenuItem);

        JMenuItem eMenuItem = new JMenuItem("Exit", icon);
        eMenuItem.setMnemonic(KeyEvent.VK_E);
        eMenuItem.setToolTipText("Exit application");
        eMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        file.add(eMenuItem);

        menubar.add(file);

        setJMenuBar(menubar);

        String lcOSName = System.getProperty("os.name").toLowerCase();
        Boolean IS_MAC = lcOSName.startsWith("mac os x");
        Boolean IS_WINDOWS = lcOSName.startsWith("windows");

        if (IS_MAC) {

            // take the menu bar off the jframe
            System.setProperty("apple.laf.useScreenMenuBar", "true");

            // set the name of the application menu item
            //  System.setProperty("com.apple.mrj.application.apple.menu.about.name", translations.getString("application.title"));
            System.setProperty("com.apple.mrj.application.apple.menu.about.name", "LightroomStatistics Viewer");
        }


        setTitle("Simple menu");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private static void openFileMenu() {
        Frame frame = new Frame();
          FileDialog fc;
        fc = new FileDialog(frame, "Choose a file", FileDialog.LOAD);
        fc.setDirectory(System.getProperty("user.home"));

        fc.setVisible(true);
        String fn = fc.getFile();
        if (fn == null)
          System.out.println("You cancelled the choice");
        else
          System.out.println("You chose " + fn);
    }

    public static void main(String[] args) {

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestSwing ex = new TestSwing();
                ex.setVisible(true);
            }
        });
    }
}

2) build.xml file without the splash screen setting for the Manifest.mf file 2)没有用于Manifest.mf文件的初始屏幕设置的build.xml文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project FileDialog_Sample with Jar-in-Jar Loader">
    <!--ANT 1.7 is required                                        -->
    <target name="create_run_jar">
        <jar destfile="D:/Temp/LRS_Viewer/FileDialog.jar">
            <manifest>
                <attribute name="Main-Class" value="fileDialog_Sample.TestSwing"/>
                <attribute name="Class-Path" value="."/>
                <attribute name="Rsrc-Class-Path" value="./"/>
            </manifest>
            <fileset dir="D:/Eclipse/FileDialog_Sample/bin"/>
        </jar>
    </target>
</project>

3) build_Splash.xml, same build file, yet now with the splash screen 3)build_Splash.xml,相同的构建文件,但现在具有启动屏幕

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project FileDialog_Sample with Jar-in-Jar Loader">
    <!--ANT 1.7 is required                                        -->
    <target name="create_run_jar">
        <jar destfile="D:/Temp/LRS_Viewer/FileDialog_Splash.jar">
            <manifest>
                <attribute name="Main-Class" value="fileDialog_Sample.TestSwing"/>
                <attribute name="Class-Path" value="."/>
                <attribute name="Rsrc-Class-Path" value="./"/>
                <attribute name="SplashScreen-Image" value="splash.png"/>
            </manifest>
            <fileset dir="D:/Eclipse/FileDialog_Sample/bin"/>
        </jar>
    </target>
</project>

Finally, the splash.png image is in the resources directory in the Java project. 最后,splash.png图像位于Java项目的resources目录中。 I build it with Eclipse. 我用Eclipse构建它。

Yes, it's a bug 是的,这是一个错误

https://bugs.openjdk.java.net/browse/JDK-8009203 https://bugs.openjdk.java.net/browse/JDK-8009203

Bizarrely, they've deferred the fix to JDK9 because 奇怪的是,他们将修复程序推迟到JDK9,因为

It is unlikely that the client would notice the problem as it's quite unusual to open the FileChooser immediately after the application loads. 客户端不太可能注意到该问题,因为在应用程序加载后立即打开FileChooser是很不寻常的。

However you hit this bug w/o opening the FileDialog immediately after load. 但是,您在加载后立即打开FileDialog却遇到了此错误。 You can wait long after the splash is down before creating a FileDialog, and it will still be broken. 您可以在启动结束后等待很长时间才能创建FileDialog,但该对话框仍将被破坏。

Would very much like a work-around to this, as there's no working redistributable jvm on OSX that can have a splash and a FileDialog in the same app. 这很可能是一种解决方法,因为OSX上没有可再发行的jvm,该同一个应用程序中可以具有启动画面和FileDialog。

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

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