问题
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!
回答1:
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
回答2:
how correctly dynamically add these panels?
After adding components to a visible GUI you need to do:
panel.add(...);
panel.revalidate();
panel.repaint();
回答3:
"and then add objects of this panel class using a for loop:.....I dont know if its technically possible"
As far as an array of panels, I would do it with an arraylist and do it similarly to how you did it.
import java.util.ArrayList;
ArrayList<MultipleChoicePanel> array = new ArrayList<MultipleChoicePanel>();
for(...){
array.add(new MultipleChoicePanel());
containerPanel.add(array.get(i));
}
回答4:
import java.awt.*;
import javax.swing.*;
public class JavaSwing extends JApplet {
public void init(){
Container content = getContentPane();
JScrollPane pane= new JScrollPane();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
panel.add(addPanel("title121"));
panel.add(addPanel("title112"));
panel.add(addPanel("title12"));
panel.add(addPanel("title11"));
pane.getViewport().add(panel);
content.add(pane);
}
public static JPanel addPanel(String title){
JPanel panel = new JPanel();
panel.add(new JButton(title));
return panel;
}
}
来源:https://stackoverflow.com/questions/17621185/adding-jpanels-through-a-loop