當我創建並運行閃屏可運行的Java應用程序(JAR),激活文件打開對話框時,沒有文件被加載在對話框中。搜索輪不停地轉動,沒有任何反應。但是,當我省略Splash圖像(來自Manifest.mf)文件時,文件打開對話框運行良好。 在Windows上我也沒問題。只有在OS X我有這個問題。測試它在10.8和10.9。OS X +的Java +開機畫面+ FileDialog的:在對話框的文件沒有文件打開
BTW我故意使用舊的FileDialog類,而不是JFileChooser中,因爲在OS X上的FileDialog看起來更加本地OS X.
BTW相同的行爲是存在的,當我使用AppBundler創建OS X應用程序並指定作爲閃屏:
我創建了一個非常簡單的測試程序來說明。
我錯過了什麼? 誰可以幫忙? 這是一個錯誤?
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項目中的資源目錄。 我使用Eclipse構建它。