I have created a JButton, on which if i click, a card is displayed. Each time i click this button, next card of same suit is supposed to be appeared, and this process should continue to go on until all the 13 cards of same suit have been displayed. I am displaying cards in form of pictures on an ImageIcon in JLabel. The problem is that if i want to display all the cards at the same spot, the first card is not removed from its place, hiding rest of the 12 cards under itself. It means that every new card, which is supposed to be displayed comes under its precedent card. I have tried to remove the precedent card from JLabel by using frame.remove(mylabel); method. But it didnt help. Moreover, If i change the co-ordinates in setBound() method, card gets displayed. But i want to display the card at very same spot where the first card was displayed. I have used counter to switch towards the next card. Each time, a card is displayed, it makes an increment in counter variable, eventually switching to the next card. Please guide me how i can remove the prior card in order to display the next card. I have used all my card displaying logic inside ActionListener method. JFrame and JButton both are declared outside of this ActinListener method. My code is given bellow:
my_btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ImageIcon pic = new ImageIcon();
JLabel lbl = new JLabel();
if(counter == 0) {
pic = new ImageIcon(c1);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 1) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(c2);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 2) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(c3);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 3) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(c4);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 4) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(c5);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 5) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(c6);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 6) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(c7);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 7) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(c8);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 8) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(c9);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 9) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(c10);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 10) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(cj);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 11) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(cq);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else if(counter == 12) {
frame.remove(lbl);
lbl.setVisible(false);
pic = new ImageIcon(ck);
lbl = new JLabel(pic);
frame.add(lbl);
lbl.setBounds(200, 100, 100, 100);
lbl.setVisible(true);
counter++;
}
else {
frame.remove(lbl);
lbl.setVisible(false);
JOptionPane.showMessageDialog(frame, "Card deck ends.");
}
}
});