更改JScrollPane的拇指颜色和背景颜色? [英] Changing the thumb color and background color of a JScrollPane?

查看:448
本文介绍了更改JScrollPane的拇指颜色和背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设计JScrollPane时遇到麻烦.我只希望能够更改拇指和背景的颜色(并删除增加/减少按钮).到目前为止,我已经尝试了以下方法:

I am having trouble styling a JScrollPane. I just want to be able to change the color of both the thumb and the background (and also remove the increase/decrease buttons). So far I have tried the following:

    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函数,我会得到一个默认的带有深色背景的拇指.

With all of the above code I get a darkened thumb with a white background. Funnily enough if I remove the setUI functions, I get a default thumb with a darkened background..

有什么想法吗?

谢谢

已解决 ** * ** *

SOLVED******

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

the configureScrollBarColors function above can be used in the following way:

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

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

That changes the color of the thumb to green.

推荐答案

以防万一仍然有人在此线程上寻求帮助:

Just in case someone out there is still falling on this thread looking for help:

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

The following line can be used to change to look of your scrollbar. The fields surrounded by "_" characters can be changed (key and COLOR).

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

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

The most relevant keys for changing the color of your bar are:

  • thumb(拇指的一般颜色)
  • thumbDarkShadow(剩余的阴影部分)
  • thumbShadow(基本上是拇指的边框,该边框用高光一分为二)
  • thumbHighlight(所述边界种类的后半部分)
  • track(背景)
  • thumb (General color of thumb)
  • thumbDarkShadow (Remaining shadowed part of thumb)
  • thumbShadow (Essentially a border for the thumb which is split in half with highlight)
  • thumbHighlight (Second half of said border kind of)
  • track (background)

所以一个例子是:

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

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

This would make the main part of the thumb black.

如果要使用全彩色拇指,请使用除跟踪以外的所有键,并将它们设置为相同的颜色.

If you are going for a full single color thumb then use all keys except track and set them to the same color.

如果要绘制轮廓,请将前两个设置为color1,将后两个设置为color2.

If you are going for an outline set the first two as color1 and the second two as color2.

在尝试改善自己的GUI时我遇到了这个问题,经过一些细微的调整后,我认为我会分享自己的发现.

有关更多键的信息,请单击我!

这篇关于更改JScrollPane的拇指颜色和背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆