简体   繁体   English

如何使用NetBeans GUI创建器将jPanel动态添加到已添加到JFrame的JScrollPane?

[英]How to dynamically add jPanels to a JScrollPane that has been added to the JFrame using NetBeans GUI creator?

This may be a silly question, and I'd rather not use the NetBeans inbuilt GUI editor, but I've been asked to. 这可能是一个愚蠢的问题,我宁愿不使用NetBeans内置的GUI编辑器,但我被要求。

The problem I'm having is I'm attempting to add a JPanel (itself containing a few labels) to an already existing JScrollPane that is on a JFrame. 我遇到的问题是我正在尝试将JPanel(本身包含一些标签)添加到JFrame上已有的JScrollPane。 The JScrollPane and JFrame have been created using the NetBeans GUI editor. 已使用NetBeans GUI编辑器创建了JScrollPane和JFrame。

I have an ArrayList (of undetermined size) of orders, and for each order in the ArrayList I'm attempting to create a JPanel and put it in the JScrollPane. 我有一个ArrayList(未定大小)的订单,对于ArrayList中的每个订单,我试图创建一个JPanel并将其放在JScrollPane中。

I'm having trouble with adding a JPanel to a JScrollPane and then adding labels to the JPanel. 我在向JScrollPane添加JPanel然后向JPanel添加标签时遇到问题。 I've looked at tutorials online but all of them need reference to the JFrame... but because I'm using the inbuilt GUI editor that comes with NetBeans I can't reference the JFrame in the code. 我已经在线查看了教程但是所有这些教程都需要引用JFrame ...但是因为我使用的是NetBeans附带的内置GUI编辑器,所以我无法在代码中引用JFrame。

The code I'm currently using: 我目前使用的代码:

    FlowLayout experimentLayout = new FlowLayout(FlowLayout.CENTER);
    JPanel panel = new JPanel();
    panel.setLayout(experimentLayout);
    JPanel panel2 = new JPanel();
    JLabel label2 = new JLabel("Hello");
    scrollPane.add(panel); //ScrollPane is already on form (put there by GUI editor)
    panel.add(panel2);
    panel2.add(label2);

Any help on how to add these JPanel to the JScrollPane would be very much appreciated! 任何有关如何将这些JPanel添加到JScrollPane的帮助将非常感谢!

With out the code, it's impossible to be 100% sure, but you would do it like you would had you built the UI yourself. 没有代码,就不可能百分百肯定,但你会像你自己构建UI一样。

jScrollPane1.setViewportView(yourPanel);

If you don't have direct access to the internals of the frame, then you need to provide some sort of access methods to allow you to do so. 如果您无法直接访问框架的内部,则需要提供某种访问方法以允许您这样做。

I've figured it out! 我已经明白了! I used the scrollPane.getViewport().add(panel); 我使用了scrollPane.getViewport()。add(panel); instead of just .add :) 而不只是.add :)

Thanks for your help :) 谢谢你的帮助 :)

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

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