简体   繁体   English

如何正确扩展JViewport?

[英]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 ,它简短,可运行,可编译,大约与JFrameJScrollPaneJViewport

  • for reducing of flickering in the JViewport is required to set 为了减少JViewport的闪烁,需要设置

    1. own RepaintManager 自己的RepaintManager

    2. 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.

相关问题 如何更新JViewPort - How to update JViewPort 如果类A扩展了类B扩展了Activity,如何正确使用findViewById - How to correctly use findViewById if a class A extends class B extends Activity 如何正确返回从类扩展并实现接口的内容? - How to Correctly return something that extends from a class and implements an interface? 如何正确使用Java泛型? 扩展集 <Integer> - How to use Java generic correctly where ? extends Set<Integer> JViewport中有多个视图? - Multiple views in a JViewport? JViewport没有定位它的视图? - JViewport is not positioning its view? Java Swing - 了解JViewport - Java Swing - Understanding the JViewport 我如何在语法上正确地实现具有类型的接口<K extends Comparable<K> ,V&gt;? - How do I syntaxcially correctly implement an interface with type<K extends Comparable<K>, V>? 如何从扩展Fragment而不是Activity的类中正确检索声明为片段布局的ImageView? - How can I correctly retrieve an ImageView declared into a fragment layout from a class that extends Fragment and not Activity? 列表<? super List<? super Integer> &gt; 和列表<? extends List<? super Integer> &gt; 以及如何正确使用它? - List<? super List<? super Integer>> and List<? extends List<? super Integer>> and how to use it correctly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM