如何知道JScrollBar是否已到达JScrollPane的底部? [英] How to know if a JScrollBar has reached the bottom of the JScrollPane?
问题描述
我想知道是否有办法知道JScrollBar
(在我的情况下为垂直)何时到达其包含JScrollPane
的底部.
I'd like to know if there is a way to know when a JScrollBar
(vertical in my case) has reached the bottom of his containing JScrollPane
.
起初,我虽然在滚动条上使用了AdjustmentListener
,但是我不知道如何解释JScrollBar
的value
属性.另外,我不确定是否正确理解maximum
的含义,以及是否可以使用该值来获取所需的信息.
At first i have though of using an AdjustmentListener
on the scroll bar but i don't know how to interpret the value
attribute of the JScrollBar
. Also i'm not sure to properly understand what the maximum
represents and if i can use with the value to get the information i need.
修改:
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent ae) {
System.out.println("Value: " + scrollPane.getVerticalScrollBar().getValue() + " Max: " + scrollPane.getVerticalScrollBar().getMaximum());
}
}
推荐答案
您必须在计算中添加滚动条的范围.我在下面的示例中将代码添加到您的代码中.
You have to add the extent of the scrollbar to your calculation. I added the code into your code in the example below.
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent ae) {
int extent = scrollPane.getVerticalScrollBar().getModel().getExtent();
System.out.println("Value: " + (scrollPane.getVerticalScrollBar().getValue()+extent) + " Max: " + scrollPane.getVerticalScrollBar().getMaximum());
}
});
两种替代实现(部分对Kleopatra做出反应)
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent event) {
JScrollBar scrollBar = (JScrollBar) event.getAdjustable();
int extent = scrollBar.getModel().getExtent();
System.out.println("1. Value: " + (scrollBar.getValue() + extent) + " Max: " + scrollBar.getMaximum());
}
});
或通过模型
scrollPane.getVerticalScrollBar().getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent event) {
BoundedRangeModel model = (BoundedRangeModel) event.getSource();
int extent = model.getExtent();
int maximum = model.getMaximum();
int value = model.getValue();
System.out.println("2. Value: " + (value + extent) + " Max: " + maximum);
}
});
这篇关于如何知道JScrollBar是否已到达JScrollPane的底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!