当我运行JApplet时,为相同的代码获得了不同的颜色

时间:2013-07-10 09:36:06

标签: java swing applet look-and-feel nimbus

我已经编写了一个JApplet,并在初始化时设置了Nimbus L& F的颜色。

当我通过Netbeans或谷歌浏览器运行applet时,按钮背景设置为黑暗会发生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

3 个答案:

答案 0 :(得分:2)

我尝试了你的代码,但它始终显示相同的颜色。我认为您的Netbeans或jdk版本存在问题。我使用的是Netbeans 7.3和jdk 1.6。 enter image description here

答案 1 :(得分:2)

    JButton的
  • 使用Painter, Background is ignored by default

  • Java6 / 7

  • 之间没有变化
  • 并非所有Keys都按照我们的预期工作,Nimbus有很多支持(在基于Nimbus的两三个定制L& F中解决)

  • 其中一种方法,在所有情况下都适用于我,例如使用Swing Timer故意延迟

enter image description here

Color.decode("#353535");返回

enter image description here

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class NimbusTest3 {

    private static final long serialVersionUID = 1L;
    private javax.swing.JButton button;
    private JFrame frame = new JFrame();
    private Timer t;

    public NimbusTest3() {
        button = new javax.swing.JButton();
        button.setText("Text");
        frame.add(button);
        frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        t = new Timer(1000, new ActionListener() {
            private Random r = new Random();

            @Override
            public void actionPerformed(ActionEvent e) {
                Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
                try {
                    LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance();
                    UIDefaults uiDefaults = lnf.getDefaults();
                    uiDefaults.put("nimbusBase", c);
                    UIManager.getLookAndFeel().uninitialize();
                    UIManager.setLookAndFeel(lnf);
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }
                UIDefaults defaults = UIManager.getDefaults();
                defaults.put("Button.background", c);
                SwingUtilities.updateComponentTreeUI(button);
                t.stop();
            }
        });
        t.start();
    }

    public static void main(String args[]) {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            return;
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                NimbusTest3 nimbusTest3 = new NimbusTest3();
            }
        });
    }
}

答案 2 :(得分:0)

我终于找到了解决方法。

在netbeans中,我将按钮的背景属性设置为某个值(与我想要的值不同,但也与默认值240,240,240不同)。

当我运行applet时,现在我总能得到我所期望的,那就是使用Nimbus在代码中设置的颜色。