我现在可以看到滚动优先级给予垂直滚动。我想改变这一点。我该怎么做?
答案 0 :(得分:2)
感谢第一个答案 - 非常有帮助。但是我发现上面的响应中的iNewValue需要乘以evt.getWheelRotation()值,这是由滚轮鼠标实际旋转的不同滚轮鼠标段的数量。
此外滚动时间的条件也需要考虑到这一点 - 条件必须是evt.getWheelRotation()< = -1或evt.getWheelRotation()> = 1
这是一个适合我的更新示例。
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelEvent;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
class MyJScrollPane extends JScrollPane
{
public MyJScrollPane(Component component)
{
super(component);
final JScrollBar horizontalScrollBar = getHorizontalScrollBar();
final JScrollBar verticalScrollBar = getVerticalScrollBar();
setWheelScrollingEnabled(false);
addMouseWheelListener(new MouseAdapter()
{
public void mouseWheelMoved(MouseWheelEvent evt)
{
if (evt.getWheelRotation() >= 1)//mouse wheel was rotated down/ towards the user
{
int iScrollAmount = evt.getScrollAmount();
int iNewValue = horizontalScrollBar.getValue() + horizontalScrollBar.getBlockIncrement() * iScrollAmount * Math.abs(evt.getWheelRotation());
if (iNewValue <= horizontalScrollBar.getMaximum())
{
horizontalScrollBar.setValue(iNewValue);
}
}
else if (evt.getWheelRotation() <= -1)//mouse wheel was rotated up/away from the user
{
int iScrollAmount = evt.getScrollAmount();
int iNewValue = horizontalScrollBar.getValue() - horizontalScrollBar.getBlockIncrement() * iScrollAmount * Math.abs(evt.getWheelRotation());
if (iNewValue >= 0)
{
horizontalScrollBar.setValue(iNewValue);
}
}
}
});
}
}
答案 1 :(得分:0)
要更改JScrollPane
的默认滚动优先级,您需要创建自己的JScrollPane
版本,并在移动鼠标滚轮时强制执行水平ScrollBar
以实现而不是垂直ScrollBar
。
可以像这样创建被覆盖的JScrollPane版本:
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
class MyJScrollPane extends JScrollPane
{
public MyJScrollPane(Component component)
{
super(component);
final JScrollBar horizontalScrollBar = getHorizontalScrollBar();
final JScrollBar verticalScrollBar = getVerticalScrollBar();
setWheelScrollingEnabled(false);
addMouseWheelListener(new MouseAdapter()
{
public void mouseWheelMoved(MouseWheelEvent evt)
{
if (evt.getWheelRotation() == 1)//mouse wheel was rotated down/ towards the user
{
int iScrollAmount = evt.getScrollAmount();
int iNewValue = horizontalScrollBar.getValue() + horizontalScrollBar.getBlockIncrement() * iScrollAmount;
if (iNewValue <= horizontalScrollBar.getMaximum())
{
horizontalScrollBar.setValue(iNewValue);
}
}
else if (evt.getWheelRotation() == -1)//mouse wheel was rotated up/away from the user
{
int iScrollAmount = evt.getScrollAmount();
int iNewValue = horizontalScrollBar.getValue() - horizontalScrollBar.getBlockIncrement() * iScrollAmount;
if (iNewValue >= 0)
{
horizontalScrollBar.setValue(iNewValue);
}
}
}
});
}
}
我希望这可以解决滚动优先级为JScrollPane
的问题。