通过循环添加JPanels [英] Adding JPanels through a loop

查看:128
本文介绍了通过循环添加JPanels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做到了!谢谢!
代码如下.我使用BoxLayout是因为我认为它是一个将问题堆叠在另一个之上的理想选择,但是现在我遇到了布局问题...当我堆叠多个问题时,问题面板开始重叠.有什么想法吗?

            panels1 = new MultipleChoice[5];
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    for(int i=0; i<4; i++){
        panels1[i]= new MultipleChoice();
        panels1[i].setAlignmentX(CENTER_ALIGNMENT);
        add(panels1[i]);


    }
    setVisible(true);


我正在设计一个在线测试小程序.
一项测试有多种选择和对/错问题.为了设置测试,我创建了两个JPanel类,一个用于选择题,一个用于对/错问题.因此,在创建测试时,我将根据非固定数量的问题将这些面板动态添加到JPanel容器中.

首先,如何动态地向面板添加新面板?我考虑过要声明JPanel类型的数组.我创建了一个面板类,然后使用for循环添加了对象:

MultitipleChoicePanel[] PanelArray;

for (...){
   PanelArray[i] =  new MultipleChoicePanel();
   containerpanel.add(PanelArray[i]);
   }

我不知道这在技术上是否可行.
这是我第一次使用Swing,我尝试这样做,但是显然没有用.
有没有人知道如何正确动态地添加这些面板?

第二,哪个布局管理器最适合于容器面板,以适合上一个面板下添加的每个新面板?
我考虑过动态设置一列的GridLayout并在添加面板时添加行.但是我一直在努力地动态修改挥杆动作.
有什么建议吗?

非常感谢您的帮助!

解决方案

JPanel默认布局为FlowLayout,默认情况下将每个组件添加到右侧,以适合您的问题.

您可能还对 swingx 感兴趣,他们拥有HorizontalLayout.

示例:

//in some place
 JPanel myBigPanel = new JPanel();
 myBigPanel.setLayout(new HorizontalLayout()); // swingx api

List<MultitipleChoicePanel> panelList = new ArrayList<>();
// panelList.add(new MultipleChoicePanel()).. .n times

for(MultipleChoicePanel mp : panelList){
 myBigPanel.add(mp);
}

myBigPanel.revalidate(); // revalidate should call repaint but who knows
myBigPanel.repaint();

如何使用各种布局管理器

Made it work! Thank you guys!
The code follows. I used BoxLayout since I thought it'd be ideal for stacking questions one on top of the other, but now I got issues with the layout... When I stack several questions the question panels start overlapping. Any thoughts?

            panels1 = new MultipleChoice[5];
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    for(int i=0; i<4; i++){
        panels1[i]= new MultipleChoice();
        panels1[i].setAlignmentX(CENTER_ALIGNMENT);
        add(panels1[i]);


    }
    setVisible(true);


I'm working on designing an Online Test applet.
A test has multiple choice and true/false questions. To set up a test I created two JPanel classes, one for the multiple choice question and one for the true/false questions. So when a test is created I'll just dynamically add these panels to a JPanel container according to the non-fixed number of questions.

First, how can I dynamically add new panels to a panel? I thought about declaring an array of the JPanel type. I created and then add objects of this panel class using a for loop:

MultitipleChoicePanel[] PanelArray;

for (...){
   PanelArray[i] =  new MultipleChoicePanel();
   containerpanel.add(PanelArray[i]);
   }

I don't know if this is technically possible.
This is my first time using Swing, and I tried doing this but obviously it didn't work.
Does anyone have an idea how correctly dynamically add these panels?

Second, which of the layout managers is best suited for the container panel in order to fit every new panel added right under the previous one?
I thought about dynamically setting up a GridLayout of one column and add rows as I add panels. But I've been really struggling modifying swings dynamically.
Any suggestions?

Thank you so much for your help!

解决方案

JPanel default layout is FlowLayout and add each component by default to the right so it would fit your problem.

You also may interested in swingx they have HorizontalLayout.

Example:

//in some place
 JPanel myBigPanel = new JPanel();
 myBigPanel.setLayout(new HorizontalLayout()); // swingx api

List<MultitipleChoicePanel> panelList = new ArrayList<>();
// panelList.add(new MultipleChoicePanel()).. .n times

for(MultipleChoicePanel mp : panelList){
 myBigPanel.add(mp);
}

myBigPanel.revalidate(); // revalidate should call repaint but who knows
myBigPanel.repaint();

How to use various Layout Managers

这篇关于通过循环添加JPanels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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