簡體   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