Java Swing:JScrollPane无法正常工作 [英] Java Swing: JScrollPane not working

查看:170
本文介绍了Java Swing:JScrollPane无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些字段的JPanel。 JPanel的高度是有限的所以我必须在它周围放置一个JScrollPane,以便人们可以向下滚动。

I have a JPanel which contains some fields. The height of the JPanel is limited so I have to put a JScrollPane around it so people can scroll down.

如下所示,它显示完美。但是你不能向下滚动(或向上)。

As you can see below, it displays perfectly. But you can't scroll down (or up).

DetailPanel detail = new DetailPanel();
JScrollPane jsp = new JScrollPane(detail);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setBounds(745, 10, 235, 225);
add(jsp);

详情面板:

private void init(){
            setLayout(null);
            setSize(140, 400);
            int x = 5, y = 0;
            for(int i = 0; i < lbls.length; i++) {
                JLabel lbl = new JLabel(lbls[i]);
                lbl.setBounds(x, y, 200, 25);
                add(lbl);
                fields[i] = new JTextField();
                fields[i].setBounds(x, y+26, 200, 25);
                add(fields[i]);
                y+=50;
            }
        }

推荐答案

您的DetailPanel没有布局管理器与之关联,这意味着当您向其添加子项时它不会展开,这意味着JScrollPane没有可以滚动的任何地方。在DetailPanel上调用 setLayout()或覆盖 getPreferredSize()以添加其子级的高度并返回有意义的价值。

Your DetailPanel has no layout manager associated with it, which means it doesn't expand when you add children to it, which means the JScrollPane doesn't have anywhere to scroll. Either call setLayout() on your DetailPanel or override getPreferredSize() to add up the heights of its children and return a meaningful value.

这篇关于Java Swing:JScrollPane无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆