strange refreshing behavior
Hey guys, been working on my hangman project. I created a method to set the text of two sets of JLabels. The first one I set as an array of JLabels, each label representing one letter the chosen word for the game, and the second one is simply a map<String, JLabel> of the whole alphabet. I added each of these labels to a JPanel with FlowLayout, and then added these to a JPanel with GridLayout which I placed in the southern region of the frame. I added everything to the frame first before adding the JLabels to them because I wanted the whole frame to appear before I prompted for the game word.
However there is a strange problem. Even though I am using repaint on both of the JPanels AND on the frame, the letters do not appear unless I resize the window. I thought repaint was supposed to fix this problem but apparently it doesn't. Why is it that the letters appear after I resize it but not immediately?
Here is my code so far:
Code:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*; //for map
public class Hangman {
public static void main(String[] args) {
Hangman hangman = new Hangman();
}
JFrame frame = new JFrame();
JLabel winLabel;
JButton bNew;
JPanel south1;
JPanel south2;
JPanel south3;
JPanel south4;
JPanel south;
JLabel[] wordArray;
//JLabel[] abcArray;
Map<String, JLabel> abcMap;
private int tries = 0;
private int wrong = 0;
public Hangman() {
HangmanPanel hPanel = new HangmanPanel();
hPanel.setPreferredSize(new Dimension(400, 300));
abcMap = new HashMap<String, JLabel>();
winLabel = new JLabel("");
bNew = new JButton("New");
south1 = new JPanel(new FlowLayout());
south1.setForeground(Color.WHITE);
south1.setBackground(Color.WHITE);
south2 = new JPanel(new FlowLayout());
south2.setBackground(Color.WHITE);
south2.add(winLabel);
south3 = new JPanel(new FlowLayout());
south3.setBackground(Color.WHITE);
south4 = new JPanel(new FlowLayout());
south4.setBackground(Color.GRAY);
south4.add(bNew);
south = new JPanel(new GridLayout(4, 1));
south.add(south1);
south.add(south2);
south.add(south3);
south.add(south4);
frame.setLayout(new BorderLayout());
frame.setTitle("Hangman");
frame.setSize(new Dimension(400,400));
frame.setLocationRelativeTo(null);
frame.add(hPanel, BorderLayout.CENTER);
frame.add(south, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
String gameWord = getWord();
int length = gameWord.length();
wordArray = new JLabel[length];
createBoard(gameWord, length);
}
private void createBoard(String gameWord, int length) {
for(int x = 0; x < length; x++) {
wordArray[x] = new JLabel("" + gameWord.charAt(x));
}
for(int x = 0; x < 26; x++) {
//abcArray[x] = new JLabel("" + (char)(x + 97));
abcMap.put("" + (char)(x + 97), new JLabel("" + (char)(x + 97)));
}
for(int x = 0; x < length; x++) {
south1.add(wordArray[x]);
}
for(int x = 0; x < 26; x++) {
//south3.add(abcArray[x]);
south3.add(abcMap.get("" + (char)(x + 97)));
}
south1.repaint();
south3.repaint();
south.repaint();
frame.repaint();
}
private String getWord() {
return JOptionPane.showInputDialog(frame, "Please enter word.");
}
}
And here is the other class:
import javax.swing.*;
import java.awt.*;
public class HangmanPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
}
}