简体   繁体   English

更改JScrollPane中的JScrollBars值的问题:90显然是幻数

[英]Issue with changing JScrollBars value in a JScrollPane: 90 is the magic number apparently

OK, so my basic premise is that I have the following setup ( http://imgur.com/qgwaYEn.png ): 好的,所以我的基本前提是我具有以下设置( http://imgur.com/qgwaYEn.png ):

主界面

And my problem is only in relation to the main image window. 我的问题仅与主图像窗口有关。 All the little black boxes surround POI picked out by a Harris Detector and I want the user to select a subset of the squares to send as the foundation of a training set. 哈里斯探测器(Harris Detector)挑选出的所有小黑框都围绕着POI,我希望用户选择正方形的一个子集作为训练集的基础发送。 As can be seen (bottom right), I can turn the boxes red when they're clicked, and I do this by redrawing onto the image and then changing the top component of the JSplit pane to a new JScrollPane that's a JLabel that encapsulates an image icon. 如图所示(右下),我可以在单击框时将它们变成红色,方法是重新绘制到图像上,然后将JSplit窗格的顶部组件更改为新的JScrollPane,该JScrollPane是封装了图像图标。

This works great, but being a new ScrollPane, it jumps back to the top left, which is inconvenient at best. 这很好用,但是作为新的ScrollPane,它会跳回到左上角,这是最不方便的。 So now, before changing the JScrollPane, I'm storing the current value of the both the Horizontal and Vertical bars and then setting the new ScrollPanes bars to the respective values. 因此,现在,在更改JScrollPane之前,我要存储水平和垂直条的当前值,然后将新的ScrollPanes条设置为相应的值。

BUT, this only works for one of them. 但是,这仅适用于其中之一。 It's so odd. 真奇怪 The first one always takes just fine, the second however defaults to 90, regardless of how it's set. 第一个总是很好用,而第二个则默认为90,无论设置如何。

The relevant code segment is: 相关代码段为:

int scrollHoriz = view_scroll.getHorizontalScrollBar().getValue();
int scrollVerti = view_scroll.getVerticalScrollBar().getValue();
System.out.println("OLD Horizontal Position: " + view_scroll.getHorizontalScrollBar().getValue());
System.out.println("OLD Vertical Position: " + view_scroll.getVerticalScrollBar().getValue());
System.out.println("Horiz: " + scrollHoriz);
System.out.println("Verti: " + scrollVerti + "\n");

view_scroll = new JScrollPane();
view_scroll.getViewport().add(new JLabel(new ImageIcon(theImage)));
view_scroll.getHorizontalScrollBar().setUnitIncrement(12);
view_scroll.getVerticalScrollBar().setUnitIncrement(12);
view_scroll.getViewport().getView().addMouseListener(new TrainingMousePress(this));

view_scroll.getVerticalScrollBar().setValue(scrollVerti);
view_scroll.getHorizontalScrollBar().setValue(scrollHoriz);

System.out.println("NEW Horizontal Position: " + view_scroll.getHorizontalScrollBar().getValue());
System.out.println("NEW Vertical Position: " + view_scroll.getVerticalScrollBar().getValue());
System.out.println("Horiz: " + scrollHoriz);
System.out.println("Verti: " + scrollVerti);

view.remove(view.getTopComponent());
view.setTopComponent(view_scroll);
view.setDividerLocation(600);

return;

Which will print out (on click of any box): 将打印出来(单击任何框):

  • OLD Horizontal Position: 216 OLD水平位置:216
  • OLD Vertical Position: 360 老垂直位置:360
  • Horiz: 216 霍里兹:216
  • Verti: 360 垂直:360
  • NEW Horizontal Position: 90 新的水平位置:90
  • NEW Vertical Position: 360 新的垂直位置:360
  • Horiz: 216 霍里兹:216
  • Verti: 360 垂直:360

And if you invert the two setters so: 如果您将两个设置器反转,则:

view_scroll.getHorizontalScrollBar().setValue(scrollHoriz);
view_scroll.getVerticalScrollBar().setValue(scrollVerti);

You get: 你得到:

  • OLD Horizontal Position: 569 OLD水平位置:569
  • OLD Vertical Position: 510 老垂直位置:510
  • Horiz: 569 荷里兹:569
  • Verti: 510 垂直:510
  • NEW Horizontal Position: 444 新的水平位置:444
  • NEW Vertical Position: 90 新的垂直位置:90
  • Horiz: 444 霍里兹:444
  • Verti: 474 垂直:474

The opposite behaviour, but 90 still gets assigned to the second bar regardless. 相反的行为,但是无论如何仍然将90分配给第二个小节。

Any suggestions would be more than welcome. 任何建议都将受到欢迎。 :) :)

Instead of saving the individual values of the scrollbars, save the position of the viewport: 保存视口的位置,而不是保存滚动条的单个值:

Point saved = scrollPane.getViewport().getViewPosition();

Then you can restore the position using: 然后,您可以使用以下方法恢复职位:

scrollPane.getViewport().setViewPosition( saved );

Also, make sure all the code is executed on the Event Dispatch Thread when you update the GUI. 另外,在更新GUI时,请确保在事件调度线程上执行了所有代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM