更新 JscrollBar 而不重置位置

发布于 2024-09-29 18:36:23 字数 178 浏览 8 评论 0原文

我有一个扩展 JScrollPane 的类,它的视口是另一个扩展 JComponent 并实现 Scrollable 的类。当组件的大小发生变化时,除非我调用 revalidate(),否则 JscrollBars 不会更新,但这会将滚动条的位置重置为左上角。有没有办法在保持滚动条当前位置的同时更新滚动条的大小?

谢谢,罗布

I have a class which extends JScrollPane, its viewport is another class which extends JComponent and implements Scrollable. When the size of the component changes the JscrollBars do not update unless I call revalidate() however this resets the position of the scroll bars to the top left. Is there a way of updating the size of the scroll bars while maintaining their current position?

Thanks, Rob

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住你手 2024-10-06 18:36:23

问题是您的自定义滚动窗格还是您的自定义组件?我们无法开始猜测您可能做出了什么样的改变。发布您的 SSCCE 来演示该问题。

它对我来说效果很好。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScrollSSCCE extends JPanel
{
    public ScrollSSCCE()
    {
        setLayout( new BorderLayout() );

        final JPanel panel = new JPanel();
        panel.setPreferredSize( new Dimension(200, 200) );
        JScrollPane scrollPane = new JScrollPane( panel );
        add( scrollPane );

        JButton button = new JButton("Adjust");
        add(button, BorderLayout.SOUTH);
        button.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                Dimension d = panel.getPreferredSize();
                d.width +=50;
                d.height +=50;
                panel.setPreferredSize(d);
                panel.revalidate();
            }
        });
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("ScrollSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new ScrollSSCCE() );
        frame.setSize(150, 150);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

Is the problem your custom scroll pane or your custom component? We can't begin to guess what kind of changes you may have made. Post your SSCCE that demonstrates the problem.

It works fine for me.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScrollSSCCE extends JPanel
{
    public ScrollSSCCE()
    {
        setLayout( new BorderLayout() );

        final JPanel panel = new JPanel();
        panel.setPreferredSize( new Dimension(200, 200) );
        JScrollPane scrollPane = new JScrollPane( panel );
        add( scrollPane );

        JButton button = new JButton("Adjust");
        add(button, BorderLayout.SOUTH);
        button.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                Dimension d = panel.getPreferredSize();
                d.width +=50;
                d.height +=50;
                panel.setPreferredSize(d);
                panel.revalidate();
            }
        });
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("ScrollSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new ScrollSSCCE() );
        frame.setSize(150, 150);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文