JScrollPane中的绝对布局面板 [英] Absolute Layout Panel within JScrollPane

查看:375
本文介绍了JScrollPane中的绝对布局面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有绝对布局的面板(不要问为什么),我需要以编程方式在其上添加元素.我做了那部分,但是现在我想用JScrollPane包围面板,以便当用户添加更多项目时,滚动条可以完成它的工作.但是用滚动条包围面板不起作用.我在这里可以做什么.

I'am using panel with absolute layout (don't ask why) and I need to add elements on it programmatically. I done that part, but now I want to surround panel with JScrollPane so that when user add more items, scroll bar does its job. But surrounding panel with scroll bar doesn't work. What can I do here.

JFrame frame = new JFrame();
    frame.setSize(582, 451);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(10, 11, 546, 391);
    frame.getContentPane().add(scrollPane);

    JPanel panel = new JPanel();
    scrollPane.setViewportView(panel);
    panel.setLayout(null);




for(int i=0;i<labelList.size();i++){
        if(a==4){
            a=0;
            b++;
        }
        labelList.get(i).setBounds(10+120*a+10*a, 10+130*b+10*b, 120, 130);
        labelList.get(i).setOpaque(true);
        labelList.get(i).setBackground(Color.WHITE);
        labelList.get(i).setText(Integer.toString(i));
        panel.add(labelList.get(i));
        a++;
    }

推荐答案

您不会喜欢我的答案,但我认为我们应该被迫为您提供最佳答案,并非总是您想听到的.就是这样:使用布局管理器来进行组件布局.是的,虽然在新手看来空布局是最容易使用的,但是随着您获得更多的经验,您会发现完全相反.空布局的使用使GUI非常不灵活,尽管它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上却看起来很糟糕,并且很难更新和维护.相反,您将需要学习和学习布局管理器,然后嵌套JPanels,每个JPanels都使用其自己的布局管理器来创建令人愉悦且复杂的GUI,在所有OS上看起来都很好.

You're not going to like my answer, but I feel that we should be compelled to give you the best answer, which is not always what you want to hear. And that is this: use layout managers to do your component layouts. Yes while it might seem to newbies that null layouts are the easiest to use, as you gain more experience you will find that exactly the opposite is true. Null layout use makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.

为便于使用,我邀请您使用空布局创建一个复杂的GUI,然后尝试在此GUI的中间添加一个组件.这将要求您手动重新计算添加到新组件左侧或下方的组件的所有位置和大小,这容易产生许多错误.布局管理器都将自动为您执行此操作.

As for ease of use, I invite you to create a complex GUI with your null layout, and then later try to add a component in the middle of this GUI. This will require you to manually re-calculate all the positions and sizes of components added to the left or below the new component, which is prone to many errors. The layout managers will all do this automatically for you.

具体使用有效的布局管理器将更新JPanel的preferredSize,因为添加了更多组件并且对JPanel进行了重新验证(告知布局重新布局其组件).您的空JPanel将不会执行此操作,因此JScrollPane将无法工作.是的,您可以通过一种替代方法来手动计算和设置JPanel的preferredSize,但这很危险,因此出于上述相同的原因也不建议这样做.

Specifically use of valid layout managers will update your JPanel's preferredSize automatically increase as more components are added and as the JPanel is revalidated (the layouts are told to re-layout their components). Your null JPanel will not do this, and so the JScrollPane will not work. Yes, a work around is for you to manually calculate and set your JPanel's preferredSize, but this is dangerous and not recommended for the same reasons noted above.

这篇关于JScrollPane中的绝对布局面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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