有人知道问题可能是什么吗?
编辑:似乎苹果已经解决了这个问题:http://lists.apple.com/archives/java-dev/2013/Jun/msg00055.html
更新2013-06-21:本答案包含一些有用的解决方法和替代方法,但是@sidney-markowitz-biomatters' answer 包含了正确的代码修复 - LAF 需要在事件线程中设置!
最近出现的问题似乎与更新破坏了Aqua外观(LAF)有关,这是Mac OS X上Swing应用程序的默认外观。
如果您需要Aqua LAF,则没有太多选择。您可能需要等待苹果的下一个Java更新(我假设他们会优先修复此问题,因为这是他们自己的LAF)。您还可以尝试使用Java Application Bundler(即捆绑Oracle JRE并避免使用系统的JRE)。
如果您可以使用不同的LAF,则您的应用程序应该按照正常方式工作。至少对于PaperCut是这样的(003更新导致了一些窗口焦点问题,004更新导致了混乱)。
一些选项:
Using the Java version-specific cross platform LAF from Java code (e.g. Nimbus or Metal):
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName())
Setting a specific LAF from Java code:
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel")
Overriding the default LAF from terminal:
java -Dswing.defaultlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel MyApp
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
,并且希望有一个不需要改变代码(即热修复)的解决方法,因此我们需要按如下方式覆盖默认的系统 LAF。
Overriding the system LAF from terminal:
java -Dswing.systemlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel ...
Overriding the system LAF from an Info.plist
file (if you have bundled as a Mac application, also works for the other VM options) (e.g. at My.app/Contents/Info.plist
).
You want to add -Dswing.systemlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel
to the end of the <string>
value for the VMOptions
<key>
. The options are space separated, just like from the terminal. E.g. if you already have a useScreenMenuBar
option:
<key>VMOptions</key>
<string>-Dcom.apple.macos.useScreenMenuBar=true -Dswing.systemlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel</string>
编辑:@trashgod要求提供可复现的示例。我不确定004更新的问题范围有多大,但这里有一个简单的重现:
更新2013-06-21 - 错误的方式,重现错误:
public class AquaLafTest {
public static void main(String[] args) throws Exception {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
javax.swing.JOptionPane.showMessageDialog(null, "msg");
}
}
使用附带004更新的苹果JRE运行(例如在 /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
)。观察到消息不可见,对话框图标不可见,按钮也不可见。
使用旧版苹果JRE或其他JRE运行。观察到对话框按预期显示。
2013-06-21更新 - 在事件线程上正确地执行,可以正常工作:
public class AquaLafTest {
public static void main(String[] args) throws Exception {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
try {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
javax.swing.JOptionPane.showMessageDialog(null, "msg");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
线程安全
。由于调用时不涉及显示任何内容并且一直有效,因此我敢打赌许多人认为这是在实际执行任何GUI之前设置某些配置设置的方法,并仅调用它一次然后就忘记它。 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
try {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
});