繁体   English   中英

更改JScrollPane的拇指颜色和背景颜色?

[英]Changing the thumb color and background color of a JScrollPane?

我在设计JScrollPane时遇到了麻烦。 我只是希望能够改变拇指和背景的颜色(还可以删除增加/减少按钮)。 到目前为止,我尝试了以下内容:

    this.setBackground(new Color(0,0,0));
    this.viewport.setBackground(new Color(0,0,0));
    this.getViewport().setBackground(Color.BLACK);
    this.setOpaque(false);
    this.getVerticalScrollBar().setUI(new BasicScrollBarUI()
    {   
        @Override
        protected JButton createDecreaseButton(int orientation) {
            return createZeroButton();
        }

        @Override    
        protected JButton createIncreaseButton(int orientation) {
              return createZeroButton();
        }
        @Override 
        protected void configureScrollBarColors(){

        }

    });

    this.getHorizontalScrollBar().setUI(new BasicScrollBarUI()
    {   
        @Override
        protected JButton createDecreaseButton(int orientation) {
            return createZeroButton();
        }

        @Override    
        protected JButton createIncreaseButton(int orientation) {
              return createZeroButton();
        }


    });
}
private JButton createZeroButton() {
    JButton jbutton = new JButton();
    jbutton.setPreferredSize(new Dimension(0, 0));
    jbutton.setMinimumSize(new Dimension(0, 0));
    jbutton.setMaximumSize(new Dimension(0, 0));
    return jbutton;
}

   UIManager.put("ScrollBar.trackHighlightForeground", (new Color(57,57,57))); 
    UIManager.put("scrollbar", (new Color(57,57,57))); 
    UIManager.put("ScrollBar.thumb", new ColorUIResource(new Color(57,57,57))); 
    UIManager.put("ScrollBar.thumbHeight", 2); 
    UIManager.put("ScrollBar.background", (new Color(57,57,57)));
    UIManager.put("ScrollBar.thumbDarkShadow", new ColorUIResource(new Color(57,57,57)));
    UIManager.put("ScrollBar.thumbShadow", new ColorUIResource(new Color(57,57,57)));
    UIManager.put("ScrollBar.thumbHighlight", new ColorUIResource(new Color(57,57,57)));
    UIManager.put("ScrollBar.trackForeground", new ColorUIResource(new Color(57,57,57)));
    UIManager.put("ScrollBar.trackHighlight", new ColorUIResource(new Color(57,57,57)));
    UIManager.put("ScrollBar.foreground", new ColorUIResource(new Color(57,57,57)));
    UIManager.put("ScrollBar.shadow", new ColorUIResource(new Color(57,57,57)));
    UIManager.put("ScrollBar.highlight", new ColorUIResource(new Color(57,57,57)));

通过以上所有代码,我得到一个带有白色背景的黑色拇指。 有趣的是,如果我删除setUI函数,我得到一个带有黑暗背景的默认拇指..

有任何想法吗?

谢谢


已解决 ** * ** *


上面的configureScrollBarColors函数可以通过以下方式使用:

 @Override 
        protected void configureScrollBarColors(){
            this.thumbColor = Color.GREEN;
        }

这会将拇指的颜色更改为绿色。

以防万一有人在这个线程上寻找帮助:

以下行可用于更改滚动条的外观。 可以更改由“_”字符包围的字段(键和颜色)。

UIManager.put("ScrollBar._key_", new ColorUIResource(_COLOR_));

用于更改条形颜色的最相关键是:

  • thumbthumb一般颜色)
  • thumbDarkShadow (拇指的剩余阴影部分)
  • thumbShadow (基本上是拇指的边框,用高光分割成两半)
  • thumbHighlight (边界类的下半部分)
  • track (背景)

所以一个例子是:

UIManager.put("ScrollBar.thumb", new ColorUIResource(Color.black));

这会使拇指的主要部分变黑。

如果您要使用完整的单色拇指,则使用除轨道以外的所有键并将它们设置为相同的颜色。

如果你想要一个轮廓,前两个设置为color1,后两个设置为color2。

在尝试改进我自己的GUI时,我试图改变这一点,并经过一些小的调整后认为我会分享我的发现。

如需更多按键,请点击我!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM