java - 更改外观会更改 JTextPane 的颜色吗?

标签 java swing colors look-and-feel nimbus

我有一个 JTextPane 具有不同的背景和前景色。现在当 L&F 更改为 Nimbus L&F 时,我的 JTextPane 的颜色也改变了。怎么会这样?只有这个类有这样的问题。而其他的工作正常。问题是什么?

这就是我改变 L&F 的方式:

for (javax.swing.UIManager.LookAndFeelInfo info : 

javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

另一方面,在设置此 L&F 之前,颜色变化非常好。 或者这些语句运行良好:

     jtp.setBackground(Color.BLACK);
     jtp.setForeground(Color.WHITE);

知道哪里出了问题吗?

最佳答案

Swing 组件将它们的外观委托(delegate)给 ComponentUI 对象。作为 Swing 的一部分,为每个组件定义了接口(interface):ButtonUIJButton 委托(delegate)给 LabelUIJLabel , TextUI for JTextPane

每个 Swing 外观都包含每个接口(interface)的实现。例如。 MetalButtonUIMetalLabelUI 等绘制组件所需的外观。

当您调用 UIManager.setLookAndFeel 时,它会交换那组实现。

一切都非常聪明,但令人讨厌的是,每个外观都不必遵守您的任何前景/背景/边框等设置。

幸运的是,Nimbus 将其所有颜色定义为 UIManager 键。

因此,您可以执行此类操作来覆盖其默认颜色:

UIManager.put("nimbusBase", Color.BLACK);

完整列表请看这里:

http://www.ce.unipr.it/people/poggi/teaching/docs/javaSE7.0Tutorial/uiswing/lookandfeel/_nimbusDefaults.html

更新

尽管如此,看起来 Nimbus 的表现并不出色! 有些人幸运地用这个覆盖了 Nimbus 颜色:

Color bgColor = new Color("99999");
UIDefaults defaults = new UIDefaults();
defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
jeditorpane.putClientProperty("Nimbus.Overrides", defaults);
jeditorpane.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
jeditorpane.setBackground(bgColor);

关于java - 更改外观会更改 JTextPane 的颜色吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228336/

相关文章:

java - 在带有动态文本的 Jlabel(标签)框中设置背景图像

r - 按 ggplot2 geom_count 中的出现次数对点进行着色

javascript - 有没有办法用颜色填充按钮背景的一半?

java - 如何在java中识别一个zip文件?

java - 在 swing 中单击按钮时显示 JfreeChart

java - 我可以在 JFrame 中嵌入 AWT 框架吗?

java - 使用 SwingWorker 在 GUI 中添加进度条

javascript - 如何检查 CSS 背景色是否为白色?

java - 将数据库中的数据显示到tableView中

Java lang IllegalAccess 关于通过 HashBasedTable 累加器收集 Guava 不可变表