I am having an array of JLabels with custom icons and 3 buttons.First time, a single button is displayed(Start) when click the panel repaints and the labels are displayed: https://i.stack.imgur.com/BHqa2.jpg . When i click the 2nd one(Urmatorul) i want the content of the pane to be erased and then replace the labels, but this is how it looks : https://i.stack.imgur.com/Uc5GH.jpg .The last button is an exit button. Also, i think is about the GridBagConstraints..
static JLabel[] label = new JLabel[words[random].length()]; // in the main class
//in the JPanel class
for(int i = 0; i < label.length; i++)
label[i] = new JLabel(box);
add(sButton);
//This is the first button ( that clears the jpanel for the first time and adds the 2 buttons + jlabels
sButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
remove(sButton);
c.gridx = 3;
add(text,c);
c.gridx = 4;
add(field, c);
c.gridx = 0;
for(int i = 0; i < label.length; i++){
c.gridy = 1;
c.gridx++;
c.insets = new Insets(400,0,0,0);
add(label[i],c);
}
c.insets = new Insets(0,0,0,0);
c.gridy ++;
c.gridx = 0;
c.gridwidth = 4;
c.weighty = 0.09;
c.anchor = GridBagConstraints.SOUTH;
add(eButton, c);
c.gridx = 3;
add(nButton, c);
revalidate();
repaint();
}
});
eButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
// The second button, which i am having trouble with ( i just copied the code from the first button listener)
nButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
label = new JLabel[words[random].length()];
for(int i = 0; i < label.length; i++)
label[i] = new JLabel(box);
removeAll();
revalidate();
repaint();
c.gridx = 3;
add(text,c);
c.gridx = 4;
add(field, c);
c.gridx = 0;
for(int i = 0; i < label.length; i++){
c.gridy = 1;
c.gridx++;
c.insets = new Insets(400,0,0,0);
add(label[i],c);
}
c.insets = new Insets(0,0,0,0);
c.gridy ++;
c.gridx = 0;
c.gridwidth = 4;
c.weighty = 0.09;
c.anchor = GridBagConstraints.SOUTH;
add(eButton, c);
c.gridx = 3;
add(nButton, c);
revalidate();
repaint();
}
});
}