【发布时间】:2014-03-03 18:26:20
【问题描述】:
当我创建并运行带有启动画面的可运行 Java 应用程序 (JAR) 时,当激活“文件打开”对话框时,对话框中没有加载任何文件。搜索轮不停地转动,什么也没有发生。但是,当我省略 Splash 图像(来自 Manifest.mf)文件时,“文件打开”对话框运行良好。 在 Windows 上我也没有问题。只有在 OS X 上我有这个问题。在 10.8 和 10.9 上都进行了测试。
顺便说一句,我故意使用旧的 FileDialog 类而不是 JFileChooser,因为在 OS X 上,fileDialog 看起来更像原生 OS X。
顺便说一句,当我使用 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) build.xml 文件没有 Manifest.mf 文件的启动画面设置
<?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 构建它。
【问题讨论】:
标签: java macos swing user-interface dialog