我编写了一个 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
最佳答案
我尝试了你的代码,但它总是显示相同的颜色。我认为你的Netbeans或jdk版本有问题。我正在使用 Netbeans 7.3 和 jdk 1.6。
关于java - 当我运行 JApplet 时,相同的代码有不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17567227/