【问题标题】:Custom JScrollbar Problem (change the knob/thumb)自定义 JScrollbar 问题(更改旋钮/拇指)
【发布时间】:2010-05-19 11:42:45
【问题描述】:

我想改变 JScrollBar 的外观。
我通过覆盖/扩展 ScrollBarUI 来做到这一点。
通过覆盖createIncreaseButtoncreateDecreaseButton 来改变箭头按钮的外观是没有问题的。 我通过覆盖paintThumb 和paintTrack 方法来改变轨道的宽度。

它现在看起来像 <----o---->(非常细的轨迹线和椭圆形拇指/旋钮)。
问题:
旋钮到最后都不能动:
它看起来像什么:  <---o------>
它应该是什么样子:<---------o>

我知道这是因为我使椭圆没有拉伸(原始矩形随宽度拉伸)。
我完全一无所知,因为要更改拇指移动的计算,以便它可以移动到最后。

非常感谢您的帮助。

代码如下:

public class TestScrollBarMain extends JFrame {

public TestScrollBarMain() {
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(500, 500));
    JScrollPane s = new JScrollPane(p);
    MyScrollBar b = new MyScrollBar();
    s.setVerticalScrollBar(b);
    getContentPane().add(s);
    setSize(100, 100);
    setVisible(true);
}

public static void main(String[] args) {
    new TestScrollBarMain();
}

public class MyScrollBarUI extends BasicScrollBarUI {

    @Override
    protected void paintThumb(final Graphics g, final JComponent c, final Rectangle thumbBounds) {
        if (thumbBounds.isEmpty() || !this.scrollbar.isEnabled()) {
            return;
        }
        g.translate(thumbBounds.x, thumbBounds.y);
        g.setColor(this.thumbDarkShadowColor);
        g.drawOval(2, 0, 14, 14);
        g.setColor(this.thumbColor);
        g.fillOval(2, 0, 14, 14);
        g.setColor(this.thumbHighlightColor);
        g.setColor(this.thumbLightShadowColor);
        g.translate(-thumbBounds.x, -thumbBounds.y);
    }

    @Override
    protected void paintTrack(final Graphics g, final JComponent c, final Rectangle trackBounds) {
        g.setColor(Color.black);
        g.fillRect(trackBounds.width / 2, trackBounds.y, 3, trackBounds.height);
        if (this.trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT) {
            this.paintDecreaseHighlight(g);
        } else if (this.trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT) {
            this.paintIncreaseHighlight(g);
        }
    }
}

public class MyScrollBar extends JScrollBar {

    MyScrollBar() {
        super();
        setUI(new MyScrollBarUI());
    }
}

}

【问题讨论】:

  • 好看的问题也很重要...
  • 请提供paintThumb和paintTrack的代码

标签: java


【解决方案1】:

在您的 MyScrollBarUI 代码中包含此内容:

  protected void setThumbBounds(int x, int y,int width,int height)
  {
      super.setThumbBounds(x, y, 14, 14);
  }
  protected Rectangle getThumbBounds()
  {
      return new Rectangle(super.getThumbBounds().x,super.getThumbBounds().y,14,14);
  }
  protected Dimension getMinimumThumbSize()
  {
      return new Dimension(14,14);
  }
  protected Dimension getMaximumThumbSize()
  {
      return new Dimension(14,14);
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    相关资源
    最近更新 更多