假设我在其中有一个适当大小的图像,Image()
我想将JScrollBar
组件的 Thumb 或 Knob 更改为该图像。
我知道我需要子类化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);
}
}
}
现在我没有看到任何 Thumb,当我尝试在 ScrollBar 周围单击时只有一个 Track。
我查看了这篇文章,看到有人推荐你阅读这篇文章,但他没有提到图片,所以这就是我想出的。
希望有人可以帮助我,谢谢!