简体   繁体   English

如何将垂直和水平滚动条置于JScrollPane中心?

[英]How do I center the vertical and horizontal scrollbars in a JScrollPane?

I have a JPanel with a JLabel in it, added to a JScrollPane. 我有一个带有JLabel的JPanel,添加到JScrollPane中。 I have an actionListener that calls JLabel.setIcon("file.jpg"); 我有一个调用JLabel.setIcon("file.jpg"); . The image is displayed in the JScrollPane correctly and is full size. 图像正确显示在JScrollPane中,并且是完整大小。 The scrollbars appear perfectly. 滚动条显示完美。 I am trying to position the vertical and horizontal scrollbars in the center by default so you are looking at the center of the image by default. 我正在尝试将垂直和水平滚动条默认定位在中心,因此默认情况下您正在查看图像的中心。

Is there a JScrollPane method that will position the viewport on the center of the image? 是否有一个JScrollPane方法将视口定位在图像的中心? Or could I manually set the position of each scrollbar to max size divided by 2? 或者我可以手动将每个滚动条的位置设置为最大尺寸除以2吗?

I have tried 我努力了

JScrollPane.getVerticalScrollBar().setValue(JScrollPane.getVerticalScrollBar().getMaximum() / 2);

While it compiles it does not center the scrollbar. 虽然它编译它不会使滚动条居中。 I have also tried setting the layout manager of my JPanel to GridBagLayout but that doesn't work either. 我也尝试将我的JPanel的布局管理器设置为GridBagLayout,但这也不起作用。

Basically, you need to know the size of the viewport's viewable area. 基本上,您需要知道视口的可视区域的大小。

Rectangle bounds = scrollPane.getViewport().getViewRect();

Then you need the size of the component, but once it's added to the scroll pane, you can get this from the view port... 然后你需要组件的大小,但是一旦它被添加到滚动窗格,你可以从视图端口获取...

Dimension size = scrollPane.getViewport().getViewSize();

Now you need to calculate the centre position... 现在你需要计算中心位置......

int x = (size.width - bounds.width) / 2;
int y = (size.height - bounds.height) / 2;

Then you need to simply adjust the view port position... 然后你需要简单地调整视口位置......

scrollPane.getViewport().setViewPosition(new Point(x, y));

Now, remember, this is only going to work once the scroll pane has being realised on the screen (or at least it has being laid out within it's parent container) 现在,请记住,只有在屏幕上实现滚动窗格(或者至少它已在其父容器中布局)之后,这才会起作用

I'm guessing that the image has not been read at the time you try to execute your code so try something like the following: 我猜测在您尝试执行代码时尚未读取图像,请尝试以下操作:

label.setIcon( new ImageIcon("...") );

SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        Rectangle bounds = scrollPane.getViewport().getViewRect();

        JScrollBar horizontal = scrollPane.getHorizontalScrollBar();
        horizontal.setValue( (horizontal.getMaximum() - bounds.width) / 2 );

        JScrollBar vertical = scrollPane.getVerticalScrollBar();
        vertical.setValue( (vertical.getMaximum() - bounds.height) / 2 );
    }
});

This will add code to the end of the Event Dispatch Thread so hopefully it executes after the image has been read in completely and the scrollbar values have all been updated. 这将在Event Dispatch Thread的末尾添加代码,因此希望它在完全读入图像并且滚动条值全部更新后执行。

Add an AdjustmentListener and see if that helps. 添加AdjustmentListener ,看看是否有帮助。 It will let you know if the value of the component changes. 如果组件的值发生变化,它会通知您。 That way, when the image is added, the scroll bar's properties will change and you will be notified. 这样,添加图像后,滚动条的属性将发生变化,您将收到通知。 You can then try to set the caret position to the mid. 然后,您可以尝试将插入位置设置为中间位置。

Tutorial: http://examples.javacodegeeks.com/desktop-java/awt/event/adjustmentlistener-example/ 教程: http//examples.javacodegeeks.com/desktop-java/awt/event/adjustmentlistener-example/

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

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