Java ScrollPane 重叠内容

发布于 2024-12-06 15:55:03 字数 760 浏览 0 评论 0原文

我确信我的 JScrollPanes 遇到了一个初学者问题。问题在于垂直滚动条与封闭面板(右侧)内的组件重叠。当滚动条与 JComboBox 的下拉位重叠时,这会变得有点痛苦。

我已将问题归结为这个小片段 - 我希望它能说明问题。

public class ScrollTest extends JFrame
{
    public ScrollTest()
    {
        super("Overlap issues!");
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(100,0));

        for(int b=0;b<100;++b)
        {
            panel.add(new JButton("Small overlap here ->"));
        }

        JScrollPane scrollpane = new JScrollPane(panel);
        add(scrollpane);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) 
    {
        new ScrollTest();
    }
}

我先看了一下,但不知道是否有其他人已经解决了这个问题。抱歉,如果它是重复的,非常感谢任何人都可以提供像我这样的 java-newb 的帮助!

I'm having what I am sure is very much a beginners problem with my JScrollPanes. The problem is that the vertical scrollbar overlaps the components within the enclosed panel (on the right hand side). It becomes a bit of a pain when the scrollbar overlaps the drop-down bit of JComboBoxes.

I have boiled the problem down to this little snippet - I hope it illustrates the issue.

public class ScrollTest extends JFrame
{
    public ScrollTest()
    {
        super("Overlap issues!");
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(100,0));

        for(int b=0;b<100;++b)
        {
            panel.add(new JButton("Small overlap here ->"));
        }

        JScrollPane scrollpane = new JScrollPane(panel);
        add(scrollpane);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) 
    {
        new ScrollTest();
    }
}

I had a look first but couldn't see if anyone else had already addressed this problem. Sorry if it's a duplicate and many thanks for any help anyone can offer a java-newb like me!

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

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

发布评论

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

评论(1

梦言归人 2024-12-13 15:55:03

问题在于,JScrollPane 的默认设置是使用默认值 JScrollPane.VERTICAL_SCROLLBAR_​​AS_NEEDED 来布局组件,这反过来会添加滚动条,而无需再次布局组件。

在您的示例中,您知道您将需要一个滚动条,因此将其更改为始终显示滚动条

public class ScrollTest extends JFrame
{
    public ScrollTest()
    {
        super("Overlap issues!");
        JPanel panel = new JPanel();
        //Insets insets = panel.getInsets();
        //insets.set(5, 5, 5, 25);
        //insets.set(top, left, bottom, right);
        panel.setLayout(new GridLayout(100,0));

        for(int b=0;b<100;++b)
        {
            panel.add(new JButton("Small overlap here ->"));
        }

        JScrollPane scrollpane = new JScrollPane(panel);
        scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollpane);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) 
    {
        new ScrollTest();
    }
}

The problem is that the default for a JScrollPane is to layout the components with a default of JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED which in turn adds the scrollbar without laying out the components again.

In your example you know you will need a scrollbar so change it to always display the scrollbar

public class ScrollTest extends JFrame
{
    public ScrollTest()
    {
        super("Overlap issues!");
        JPanel panel = new JPanel();
        //Insets insets = panel.getInsets();
        //insets.set(5, 5, 5, 25);
        //insets.set(top, left, bottom, right);
        panel.setLayout(new GridLayout(100,0));

        for(int b=0;b<100;++b)
        {
            panel.add(new JButton("Small overlap here ->"));
        }

        JScrollPane scrollpane = new JScrollPane(panel);
        scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollpane);
        pack();
        setVisible(true);
    }

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