JScrollPane 获取与设置位置正确:
jScrollPane.getViewport().getViewPosition()
别的都不对,
JScrollPane 监听
JScrollBar bar = srrollPane.getVerticalScrollBar();// 返回控制视口垂直视图位置的垂直滚动条 bar.getPreferredSize(); bar.addAdjustmentListener(new AdjustmentListener() { @Override public void adjustmentValueChanged(AdjustmentEvent e) { // srrollPane= (JScrollPane) evt.getSource(); Point p = srrollPane.getViewport().getViewPosition(); System.out.println("position2:"+p.getY()); } });// 监听滚动条模型发生更改
srrollPane.getViewport().getHeight()
当var设置为Never时,
jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
屏幕可见高度:
vbar.getVisibleAmount()srrollPane.getHeight(),这个不太准确
可以滚动高度为:totalHeight,滚动条子控件的总高度-屏幕可见高度,就是可以滚动总高度
下面是滚动设置方法:
JScrollBar vbar = jScrollPane.getVerticalScrollBar();// 返回控制视口垂直视图位置的垂直滚动条 String aa = jScrollPane.getHeight() + " " + vbar.getVisibleAmount() + " " + jScrollPane.getVisibleRect().getHeight() + " "; double totalHeight = backgroundPanel.getPreferredSize().getHeight() - vbar.getVisibleAmount();//-vbar.getHeight()-jScrollPane.getVerticalScrollBarPolicy()*0.5; ylocation = ylocation * totalHeight; if (ylocation < totalHeight) { Point target = jScrollPane.getViewport().getLocation(); target.setLocation(target.getX(), ylocation); jScrollPane.getViewport().setViewPosition(target); }