我想改变JScrollBar的外观
我这样做是为了覆盖/扩展ScrollBarUI
通过覆盖createIncreaseButton
和createDecreaseButton
来更改箭头按钮的前景没有问题。
我通过覆盖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());
}
}
}
答案 0 :(得分: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);
}