O meu código é este, estou só a fazer uma coisa simples para ver como funciona pois é a primeira vez que mexo em JPanel:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class teste2 {
public static void put(GridBagConstraints c, JPanel pane1, int cc) throws InterruptedException{
final int co = cc;
JLabel questions = new JLabel(""+co+co+co+co+co+co+co+co+co+co);
c.gridx = 0;
c.gridy = 0;
pane1.add(questions, c);
c.insets = new Insets(10,10,10,10);
JButton button1 = new JButton("Button " + (co + 1));
c.gridx = 0;
c.gridy = 1;
pane1.add(button1, c);
JButton button2 = new JButton("Button " + (co + 2));
c.gridx = 0;
c.gridy = 2;
pane1.add(button2, c);
JButton button3 = new JButton("Button " + (co + 3));
c.gridx = 0;
c.gridy = 3;
pane1.add(button3, c);
JButton button4 = new JButton("Button " + (co + 4));
c.gridx = 0;
c.gridy = 4;
pane1.add(button4, c);
button1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "Está a funcionar " + (co + 1));
}
}
);
button2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "Está a funcionar " + (co + 2));
}
}
);
button3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "Está a funcionar " + (co + 3));
}
}
);
button4.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "Está a funcionar " + (co + 4));
}
}
);
//Thread.sleep(1000);
//co++;
}
public static void main(String[] args) throws InterruptedException{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane1 = new JPanel(new GridBagLayout());
JPanel pane2 = new JPanel(new GridBagLayout());
frame.getContentPane().add(pane1, BorderLayout.NORTH);
frame.getContentPane().add(pane2, BorderLayout.EAST);
GridBagConstraints c = new GridBagConstraints();
int co = 0;
while(co < 20){
if(co == 0){
put(c, pane1, co);
co = co + 4;
pane1.revalidate();
pane1.repaint();
}
if(co%4 == 0){
put(c, pane1, co);
co = co + 4;
pane1.revalidate();
pane1.repaint();
}
}
}
}
Obrigado
