我编写了一个 JApplet,并在初始化中为 Nimbus L&F 设置了颜色。
当我通过 Netbeans 或通过 Google Chrome 运行小程序时,有 9/10 次发生按钮背景设置为深色,但有时 Nimbus 无法设置颜色。
这是一个SSCCE:
import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import javax.swing.UIManager;
public class NimbusColors extends javax.swing.JApplet {
// colors and look and feel
Color buttonBackgroundColor;
Color buttonTextColor;
Color textAreaBackgroundColor;
Color textAreaTextColor;
Color skinColor;
Color defaultButtonBackgroundColor = Color.decode("#4a4a4a");
Color defaultButtonTextColor = Color.decode("#cecece");
Color defaultTextAreaBackgroundColor = Color.decode("#414141");
Color defaultTextAreaTextColor = Color.decode("#cecece");
Color defaultSkinColor = Color.decode("#353535");
Color progressColor = Color.decode("#00FFFF");
@Override
public void init() {
getContentPane().setBackground(defaultSkinColor);
UIManager.put("TextArea.background", defaultTextAreaBackgroundColor);
UIManager.put("TextArea.foreground", defaultTextAreaTextColor);
UIManager.put("control", defaultTextAreaBackgroundColor);
UIManager.put("nimbusLightBackground", defaultSkinColor);
UIManager.put("background", defaultSkinColor);
UIManager.put("text", defaultButtonTextColor);
UIManager.put("ComboBox.background", defaultSkinColor.darker().darker());
UIManager.put("Button.background", defaultSkinColor);
UIManager.put("info", defaultSkinColor.brighter().brighter());
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NimbusColors.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the applet */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
initComponents();
}
});
} catch (InterruptedException | InvocationTargetException ex) {
System.exit(11);
}
}
private void initComponents() {
jButtonBrowseFS = new javax.swing.JButton();
jButtonBrowseFS.setText("Browse");
jButtonBrowseFS.setToolTipText("Browse your filesystem to select files to upload");
jButtonBrowseFS.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jButtonBrowseFS.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButtonBrowseFS)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButtonBrowseFS)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}
private javax.swing.JButton jButtonBrowseFS;
}
我已经用 Netbeans 7.3.1 尝试了这段代码,创建了一个新的 Java 项目并添加了一个新的 JApplet 文件。如果我从 Netbeans 运行文件十几次,有时按钮背景是暗的,有时不是。
任何人都可以复制这种奇怪的行为吗?到底是怎么回事?
更新 1:我正在运行 Windows 8 Pro