【问题标题】:When I run a JApplet got different colors for the same code当我运行 JApplet 时,相同的代码有不同的颜色
【发布时间】:2013-07-10 09:36:06
【问题描述】:

我编写了一个 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

【问题讨论】:

  • @hellzone 我正在使用 jdk1.7.0_25。请尝试调整窗口大小,单击按钮等,然后重试。我不认为这取决于netbeas版本,因为我在谷歌浏览器中运行完整小程序时遇到了类似的问题
  • 我尝试了所有方法,但没有任何改变。安装 jdk 1.6 并尝试使用它。 jdk 1.7 可能是问题所在。
  • @hellzone 在实际的小程序中,我使用 Java 7 的特性,所以如果它是 Java 7 的错误,我必须接受它......
  • @PaoloM : Stackoverflow 上 99.9 % 的人可能甚至不会尝试测试您的案例,原因很简单,他们可能会犹豫构建一个 html 文件来测试以下代码你的。为了让别人以更简单的方式测试您的代码,在发布与JApplet/Applet 相关的代码时,必须在.java file 中也插入这一小行,这样就不必编写html 文件。 /**<applet code="NimbusColors.java" width=500 height=500></applet>*/ 现在可以简单地写javac NimbusColors.java 编译然后appletviewer NimbusColors.java 运行它:-)
  • @PaoloM : 同样在这里,只是一次,在多次尝试中,我得到了这个白色按钮,虽然大多数时候它是灰色的,可能是Nimbus 中的一个错误,它有一些问题总是:(

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


【解决方案1】:

我试过你的代码,但它总是显示相同的颜色。我认为您的 Netbeans 或 jdk 版本有问题。我正在使用 Netbeans 7.3 和 jdk 1.6。

【讨论】:

  • @mKorbel 代码为我工作。我不知道它与 netbeans 或 JDK 相关,但代码运行良好:)
  • @hellzone 代码不适合你...工作的一个按钮背景颜色设置为深色。
【解决方案2】:
  • 对于 JButton 有使用 Painter, Background is ignored by default

  • Java6/7 之间没有变化

  • 并非所有的 Keys 都像我们预期的那样工作,Nimbus 有很多优点(在基于 Nimbus 的 2-3 自定义 L&F 中解决)

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

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

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();
            }
        });
    }
}

【讨论】:

  • “默认忽略背景”是什么意思?如果我评论设置 Button.background 的行,我总是有一个发光按钮,否则它有时会发生。
  • Painter 负责应用,绘制渐变背景
  • 对不起,我不明白。
  • 每次当 OP 从不阅读 Oracle tutorial 并且从不检查 Nimbus defaults 时都会发生这种情况 :-) .. ,对于 Buttons Components 是否使用了 Painter,以防您更改Nimbus overrides(并不是所有的键都真正对应覆盖),那么Button.background可以被Java编译器接受
  • 好的,谢谢。出于好奇...您是否尝试过重现我的问题中的奇怪行为?我不明白为什么相同的代码(即使没有重新编译)在不同的运行中表现不同......
【解决方案3】:

我终于找到了解决方法。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    相关资源
    最近更新 更多