繁体   English   中英

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

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

当我创建并运行带有Splash屏幕的可运行Java应用程序(JAR)时,激活“文件打开”对话框时,对话框中不会加载任何文件。 搜索轮不断转动,什么也没发生。 但是,当我省略Splash图像(来自Manifest.mf)文件时,“文件打开”对话框运行良好。 同样在Windows上,我没有问题。 仅在OS XI上存在此问题。 在10.8和10.9上都对其进行了测试。

顺便说一句,我故意使用较旧的FileDialog类而不是JFileChooser,因为在OS X上,fileDialog看起来更像是本机OSX。

顺便说一句,当我使用AppBundler创建OS X App并将Splash屏幕指定为时,存在相同的行为:

我创建了一个非常简单的测试程序来说明。

我想念什么? 谁能帮忙? 这是错误吗?

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)没有用于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,相同的构建文件,但现在具有启动屏幕

<?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>

最后,splash.png图像位于Java项目的resources目录中。 我用Eclipse构建它。

是的,这是一个错误

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

奇怪的是,他们将修复程序推迟到JDK9,因为

客户端不太可能注意到该问题,因为在应用程序加载后立即打开FileChooser是很不寻常的。

但是,您在加载后立即打开FileDialog却遇到了此错误。 您可以在启动结束后等待很长时间才能创建FileDialog,但该对话框仍将被破坏。

这很可能是一种解决方法,因为OSX上没有可再发行的jvm,该同一个应用程序中可以具有启动画面和FileDialog。

暂无
暂无

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

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