加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 改变外观和感觉改变了JTextPane的颜色?

发布时间:2020-12-15 04:08:00 所属栏目:Java 来源:网络整理
导读:我有一个JTextPane有一个不同的背景和前景色.现在L F改为Nimbus L F,我的JTextPane的颜色改变了.怎么样?只有这个类有这样的问题.而其他人运作良好.问题是什么? 这就是我改变L F的方式: for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UI
我有一个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,ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        }

另一方面,在设置L& F之前,颜色变化很好.
或者这些陈述运作良好:

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

知道什么是错的吗?

解决方法

Swing组件将其外观委托给ComponentUI对象.作为Swing的一部分,为每个组件定义了接口:JButton委托给的ButtonUI,JLabel的LabelUI,JTextPane的TextUI等.

每个Swing外观都包含每个接口的实现.例如. MetalButtonUI,MetalLabelUI等绘制该组件但外观和感觉.

当你调用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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读