I am creating a Blackjack game and need to refresh the JFrame
every time the user clicks a button. However, the frame is not updating! I've tried for hours trying to fix this in vain.
How do I properly reload all the elements in the frame based on the Stack of ImageIcon
objects I use to load images?
Here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
public class Blackjack extends JFrame implements ActionListener {
public void drawGUI(boolean firstTime) {
getContentPane().removeAll();
setLayout(new GridLayout(3, 9, 1, 1));
updateValues();
add(new JLabel(DEALER_TEXT, SwingConstants.CENTER));
add(new JLabel("Value: " + computerValue, SwingConstants.CENTER));
for(int i = 0; i < computerCards.size(); i++)
add(new JLabel(computerCards.get(i).getImagePath()));
leaveSpacing(false);
add(new JLabel(USER_TEXT, SwingConstants.CENTER));
add(new JLabel("Value: " + userValue, SwingConstants.CENTER));
for(int i = 0; i < userCards.size(); i++)
add(new JLabel(userCards.get(i).getImagePath()));
leaveSpacing(true);
if(firstTime) {
hitButton.addActionListener(this);
standButton.addActionListener(this);
}
leaveSpacing(3);
add(hitButton);
add(standButton);
leaveSpacing(1);
}
}