[英]How to extends JViewport correctly?
I am trying to design a class of endless coordinate board with a grid. 我正在尝试设计一类带有网格的无限坐标板。 I have extended a
JViewport
and it draws at initial position ok (inside JScrollPane
). 我扩展了
JViewport
,它在ok的初始位置绘制(在JScrollPane
内部)。 How to tell scroll pane that there are some space to scroll in any direction? 如何告诉滚动窗格在任何方向上都有一定的滚动空间?
The following does not help 以下无济于事
JCoordinateViewport coordinate = new JCoordinateViewport();
coordinate.setBackground(Color.WHITE);
//coordinate.setPreferredSize(new Dimension(10000, 10000));
JScrollPane scroll = new JScrollPane();
//scroll.setViewportView(coordinate);
scroll.setViewport(coordinate);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.getVerticalScrollBar().setMinimum(-10000);
scroll.getVerticalScrollBar().setMaximum(+10000);
scroll.getHorizontalScrollBar().setMinimum(-10000);
scroll.getHorizontalScrollBar().setMaximum(+10000);
UPDATE 更新
Does anybody knows how JScrollPane determines scroll ranges from it's viewport? 有人知道JScrollPane如何从其视口确定滚动范围吗?
UPDATE2 更新2
I found, that scrollbars appear t work if maximums and minimums are set after setVisible
called. 我发现,如果在
setVisible
调用后设置了最大值和最小值,滚动条将无法正常工作。
But unfortunately, paintConponent
does not called upon scroll. 但不幸的是,
paintConponent
并未在滚动时调用。
Why? 为什么?
UPDATE3 更新3
Although scroll bars work, they don't change viewport position. 尽管滚动条有效,但它们不会更改视口位置。
Why? 为什么?
CoordinateViewport coordinate = new JCoordinateViewport(); coordinate.setBackground(Color.WHITE); //coordinate.setPreferredSize(new Dimension(10000, 10000));
JScrollPane scroll = new JScrollPane(); //scroll.setViewportView(coordinate);
scroll.setViewport(coordinate);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.getVerticalScrollBar().setMinimum(-10000);
scroll.getVerticalScrollBar().setMaximum(+10000);
scroll.getHorizontalScrollBar().setMinimum(-10000);
scroll.getHorizontalScrollBar().setMaximum(+10000);
. 。
The following does not help
以下无济于事
then issue should be only in the class with name CoordinateViewport
那么问题应该只在名称为
CoordinateViewport
的类中
for better help sooner post an SSCCE , short, runnable, compilable, just about JFrame
, JScrollPane
and JViewport
为了更好地提供帮助,请尽快发布SSCCE ,它简短,可运行,可编译,大约与
JFrame
, JScrollPane
和JViewport
for reducing of flickering in the JViewport
is required to set 为了减少
JViewport
的闪烁,需要设置
own RepaintManager
自己的
RepaintManager
and by using/with built_in methods in JViewport
并通过/在
JViewport
使用Built_in方法
JViewport.setScrollMode(JViewport.BLIT_SCROLL_MODE); JViewport.setScrollMode(JViewport.BLIT_SCROLL_MODE); JViewport.setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
JViewport.setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE); JViewport.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
JViewport.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
please see Passing current Date and JTable how to change BackGround Color as potential source for SSCCE 请参阅传递当前日期和JTable如何将BackGround颜色更改为SSCCE的潜在来源
As shown here , painting on a JViewport
appears to "stick" to the viewport, while painting on the underlying scrollable component slides beneath. 如图所示这里 ,画上
JViewport
出现“粘”在视口中,而画上的底层滚动组件的幻灯片下面。 Sizes are integral multiples of TILE
: for demonstration purposes, the preferred size of the viewport is made smaller than the underlying panel; 大小是
TILE
整数倍:出于演示目的,使视口的首选大小小于基础面板的大小。 in practice, it's better to override getPreferredSize()
. 实际上,最好重写
getPreferredSize()
。 See also ScrollAction
, which auto-scrolls as the mouse hovers near any border. 另请参见
ScrollAction
,当鼠标悬停在任何边界附近时,它会自动滚动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.