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;
        }

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

4

1 回答 1

3

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

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

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

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

  • thumb(拇指的一般颜色)
  • thumbDarkShadow(拇指的剩余阴影部分)
  • thumbShadow(基本上是拇指的边框,用高亮一分为二)
  • thumbHighlight(说边框的后半部分)
  • track(背景)

所以一个例子是:

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

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

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

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

我在尝试改进我自己的 GUI 时遇到了这个问题,经过一些小的调整后,我想我会分享我的发现。

更多钥匙点我!

于 2016-12-17T13:09:21.337 回答