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

7

我在样式化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;
        }

这将把拇指的颜色变成绿色。

1个回答

3

如果还有人在查找帮助,以下内容可能有所帮助:

下面这行代码可以用来改变滚动条的样式。用"_"括起来的字段可以更改(包括key和COLOR)。

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

改变您的工具栏颜色最相关的键是:
  • thumb(工具栏的一般颜色)
  • thumbDarkShadow(剩余阴影部分的工具栏)
  • thumbShadow(基本上是工具栏的边框,用高亮分成两半)
  • thumbHighlight(所述边框的第二半部分 有点
  • track(背景)

因此,一个例子是:
UIManager.put("ScrollBar.thumb", new ColorUIResource(Color.black));

这将使大拇指的主要部分变黑。
如果您想使用全色大拇指,则使用除轨道和设置键之外的所有键,并将它们设置为相同的颜色。
如果您想要一个轮廓,请将前两个设置为color1,后两个设置为color2。
我在尝试改善自己的GUI时发现了这一点,在进行了一些微小的调整后,我想分享我的发现。
要查看更多的键,请点击此处

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接