我想从 JScrollPane 的滚动条中删除滚动条箭头按钮。我该怎么做?
问问题
5701 次
4 回答
6
class NoArrowScrollBarUI extends BasicScrollBarUI {
protected JButton createZeroButton() {
JButton button = new JButton("zero button");
Dimension zeroDim = new Dimension(0,0);
button.setPreferredSize(zeroDim);
button.setMinimumSize(zeroDim);
button.setMaximumSize(zeroDim);
return button;
}
@Override
protected JButton createDecreaseButton(int orientation) {
return createZeroButton();
}
@Override
protected JButton createIncreaseButton(int orientation) {
return createZeroButton();
}
@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
//own painting if needed
}
@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
//own painting if needed
}
}
删除按钮为那时腾出空间。我发现让按钮归零是最简单的方法。
于 2010-11-12T07:08:40.360 回答
2
如果您使用的是 JScrollBar 的基本版本,那么它可能正在使用 BasicScrollBarUI 进行渲染。我建议您扩展 BasicScrollBarUI 以创建自定义 UI 类(如 MyBasicScrollBarUI)。按钮是超类中的受保护变量。因此,您需要覆盖子类中的 installComponents() 方法并确保不添加按钮。请参阅下面的代码片段并按照那里的建议隐藏行。
protected void installComponents(){
switch (scrollbar.getOrientation()) {
case JScrollBar.VERTICAL:
incrButton = createIncreaseButton(SOUTH);
decrButton = createDecreaseButton(NORTH);
break;
case JScrollBar.HORIZONTAL:
if (scrollbar.getComponentOrientation().isLeftToRight()) {
incrButton = createIncreaseButton(EAST);
decrButton = createDecreaseButton(WEST);
} else {
incrButton = createIncreaseButton(WEST);
decrButton = createDecreaseButton(EAST);
}
break;
}
scrollbar.add(incrButton); // Comment out this line to hide arrow
scrollbar.add(decrButton); // Comment out this line to hide arrow
// Force the children's enabled state to be updated.
scrollbar.setEnabled(scrollbar.isEnabled());
}
然后,在初始化 JScrollBar 后的代码中,您可以调用 setUI() 并传入 MyBasicScrollBarUI 类的实例。
注意:我自己没有尝试过,但从代码看来它可以工作。
于 2009-11-24T00:23:30.657 回答
0
这不是最优雅的方式......但对我有用
JScrollBar jsb = getHorizontalScrollBar();
for(Component c : jsb.getComponents()) {
jsb.remove(c);
}
于 2017-10-20T06:16:27.200 回答
0
这就是我去的方式。
- 将要隐藏的滚动条的滚动条策略设置为
never
- 模仿这种行为
MouseWheelListener
这种方法:
- 用很少的代码行就可以快速实现。
- 保留了
L&F
. - 将删除按钮和栏。
下面是删除verticall scroll bar
.
JScrollPane myScrollPane = new JScrollPane();
//remove the scroll bar you don't want
myScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
JTextPane myJTextArea = new JTextPane();
//myScrollPane.setViewportView(myJTextArea);
myScrollPane.addMouseWheelListener(new MouseWheelListener() {
//this will mimick the behavior of scrolling
public void mouseWheelMoved(MouseWheelEvent e) {
JScrollBar scrollBar = myScrollPane.getVerticalScrollBar();
//capturing previous value
int previousValue = scrollBar.getValue();
int addAmount;
//decide where the wheel scrolled
//depending on how fast you want to scroll
//you can chane the addAmount to something greater or lesser
if(e.getWheelRotation()>0) {
addAmount = 2;
}else {
addAmount = -2;
}
//set the new value
scrollBar.setValue(previousValue + addAmount);
}
});
于 2020-05-15T10:32:49.123 回答