Java 如何将JScrollbar的拇指更改为自定义图像

Java 如何将JScrollbar的拇指更改为自定义图像,java,swing,scrollbar,custom-component,jscrollbar,Java,Swing,Scrollbar,Custom Component,Jscrollbar,假设我在image()中有一个大小合适的图像 我想将JScrollBar组件的拇指或旋钮更改为此图像 我知道我需要对ScrollBarUI 这就是我现在的处境 public class aScrollBar extends JScrollBar { public aScrollBar(Image img) { super(); this.setUI(new ScrollBarCustomUI(img)); } public class

假设我在
image()中有一个大小合适的图像
我想将
JScrollBar
组件的拇指或旋钮更改为此图像

我知道我需要对
ScrollBarUI

这就是我现在的处境

public class aScrollBar extends JScrollBar {

    public aScrollBar(Image img) {
        super();
        this.setUI(new ScrollBarCustomUI(img));
    }

    public class ScrollBarCustomUI extends BasicScrollBarUI {

        private final Image image;

        public ScrollBarCustomUI(Image img) {
            this.image = img;
        }

        @Override
        protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
            Graphics2D g2g = (Graphics2D) g;
            g2g.dispose();
            g2g.drawImage(image, 0, 0, null);
            super.paintThumb(g2g, c, thumbBounds);
        }

        @Override
        protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
            super.paintTrack(g, c, trackBounds);
        }


        @Override
        protected void setThumbBounds(int x, int y, int width, int height) {
            super.setThumbBounds(0, 0, 0, 0);
        }


        @Override
        protected Dimension getMinimumThumbSize() {
            return new Dimension(0, 0);
        }

        @Override
        protected Dimension getMaximumThumbSize() {
            return new Dimension(0, 0);
        }
    }
}
现在我看不到任何拇指,只有一个轨道,当我试图点击周围的滚动条

我查看了这篇文章,看到有人推荐你们阅读,但他并没有提到图片,所以这就是我想到的

希望有人能帮助我,谢谢

问题是:

g2g.drawImage(image, 0, 0, null);
必须使用当前拇指位置作为绘制起点。我想一定是thumbRect.x和thumbRect.y,所以:

g2g.drawImage(image, thumbRect.x, thumbRect.y, null); should work.
此外,我不确定您是否在paintThumb中调用了super方法。那条线不会覆盖你定制的东西吗

和:应该忽略dispose调用。

问题是:

g2g.drawImage(image, 0, 0, null);
必须使用当前拇指位置作为绘制起点。我想一定是thumbRect.x和thumbRect.y,所以:

g2g.drawImage(image, thumbRect.x, thumbRect.y, null); should work.
此外,我不确定您是否在paintThumb中调用了super方法。那条线不会覆盖你定制的东西吗


并且:应该忽略dispose调用。

为什么要调用
g2g.dispose()
?它会破坏图形对象,因此无法绘制拇指。尝试在
paintThumb
方法中删除此调用。以下是绘制自定义拇指的示例:

@Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
        if (thumbBounds.isEmpty() || !scrollbar.isEnabled()) {
            return;
        }
        g.translate(thumbBounds.x, thumbBounds.y);
        g.drawRect(0, 0, thumbBounds.width - 2, thumbBounds.height - 1);
        AffineTransform transform = AffineTransform.getScaleInstance((double) thumbBounds.width
                / thumbImg.getWidth(null), (double) thumbBounds.height / thumbImg.getHeight(null));
        ((Graphics2D) g).drawImage(thumbImg, transform, null);
        g.translate(-thumbBounds.x, -thumbBounds.y);
    }

为什么要调用
g2g.dispose()
?它会破坏图形对象,因此无法绘制拇指。尝试在
paintThumb
方法中删除此调用。以下是绘制自定义拇指的示例:

@Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
        if (thumbBounds.isEmpty() || !scrollbar.isEnabled()) {
            return;
        }
        g.translate(thumbBounds.x, thumbBounds.y);
        g.drawRect(0, 0, thumbBounds.width - 2, thumbBounds.height - 1);
        AffineTransform transform = AffineTransform.getScaleInstance((double) thumbBounds.width
                / thumbImg.getWidth(null), (double) thumbBounds.height / thumbImg.getHeight(null));
        ((Graphics2D) g).drawImage(thumbImg, transform, null);
        g.translate(-thumbBounds.x, -thumbBounds.y);
    }

指定非空边界和大小时会发生什么情况?您是否尝试过MetalScrollBarUI
?请编辑您的问题,使其包含一个@Epicmaster+1作为问题,不知道……请学习java命名约定并坚持这些约定当您指定非空边界和大小时会发生什么?您是否尝试过MetalScrollBarUI?请编辑您的问题,以包含一个@Epicmaster+1作为问题,不知道…请学习java命名约定并坚持它们