The ideal is to create lists and place these components so you can easily access them later. And for the layout issue I recommend using box encapsulation (Box) as it can create two vertical and one horizontal to
keep the layout in an organized way. (Of course you will need to make adjustments to make the program "pretty" .) But, just do some accounts and position the elements and you will not get an error.
importjavax.swing.Box;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JTextField;importjava.util.ArrayList;importjava.util.List;publicclassGuiApp1{publicstaticvoidmain(String[]args){newGuiApp1();}publicGuiApp1(){JFrameguiFrame=newJFrame();guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);guiFrame.setTitle("Example GUI");
guiFrame.setSize(300,250);
guiFrame.setLocationRelativeTo(null);
Box caixaVertical = Box.createVerticalBox();
List<JLabel> labelLista = new ArrayList<JLabel>();
List<JTextField> inputLista = new ArrayList<JTextField>();
for(int i=0;i<10;i++){
Box caixaHorizontal = Box.createHorizontalBox();
JLabel label = new JLabel("Label" + i + " ");
JTextField input = new JTextField(10);
labelLista.add(label);
inputLista.add(input);
caixaHorizontal.add(label);
caixaHorizontal.add(input);
caixaVertical.add(caixaHorizontal);
}
guiFrame.add(caixaVertical);
guiFrame.setVisible(true);
}
}
Note 1: I did not bother with the aesthetics of this program
Note 2: I particularly prefer programming a lot of the Interface always in the hand.
(but I use an image editing program to view the desired placement i.e photoshop / paintshop etc)