I have a Computer Science project due Monday. We have to make a game and use GUI in order to ensure ourselves a good score. I decided to make Hang Man. The class reads in a txt file of common SAT words (I'm a sophomore in high school, so I need to start studying these words).
Some background on my progress: I've had to make several new files for my classes because my old ones got all corrupt and messed up. Like whenever I wanted to add changes after running a successful program, the class wouldn't read any of my edits at all. Like if I had set my JFrame size from 600 x 600 to 300 x 300, the size would remain 600 x 600, even if my parameters for setSize() would be (300,300). So I had to do lots of copy-pasting here and there on new files, and then there were no problems. They began to act normally.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class HangMan extends JFrame implements ActionListener{
private ArrayList <Vocab> satWords = new ArrayList <Vocab>();
private boolean gameOver;
private boolean empty;
private Label error = new Label ("",1);
private Label info = new Label ("", 1);
private JButton slots [] = new JButton [0]; //initial state has 0 slots.
private JButton a = new JButton("A");
private JButton b = new JButton("B");
private JButton c = new JButton("C");
private JButton d = new JButton("D");
private JButton e = new JButton("E");
private JButton f = new JButton("F");
private JButton g = new JButton("G");
private JButton h = new JButton("H");
private JButton i = new JButton("I");
private JButton j = new JButton("J");
private JButton k = new JButton("K");
private JButton l = new JButton("L");
private JButton m = new JButton("M");
private JButton n = new JButton("N");
private JButton o = new JButton("O");
private JButton p = new JButton("P");
private JButton q = new JButton("Q");
private JButton r = new JButton("R");
private JButton s = new JButton("S");
private JButton t = new JButton("T");
private JButton u = new JButton("U");
private JButton v = new JButton("V");
private JButton w = new JButton("W");
private JButton x = new JButton("X");
private JButton y = new JButton("Y");
private JButton z = new JButton("Z");
private JPanel buttonPanel = new JPanel();
private JPanel slotPanel = new JPanel();
private JPanel cluePanel = new JPanel();
private String winner;
private String clueDef;
private int numLetters;
public HangMan(){
setLayout(new BorderLayout());
setSize(600,600);
add(buttonPanel, BorderLayout.SOUTH);
a.addActionListener(this);
buttonPanel.add(a);
b.addActionListener(this);
buttonPanel.add(b);
c.addActionListener(this);
buttonPanel.add(c);
d.addActionListener(this);
buttonPanel.add(d);
e.addActionListener(this);
buttonPanel.add(e);
f.addActionListener(this);
buttonPanel.add(f);
g.addActionListener(this);
buttonPanel.add(g);
h.addActionListener(this);
buttonPanel.add(h);
i.addActionListener(this);
buttonPanel.add(i);
j.addActionListener(this);
buttonPanel.add(j);
k.addActionListener(this);
buttonPanel.add(k);
l.addActionListener(this);
buttonPanel.add(l);
m.addActionListener(this);
buttonPanel.add(m);
n.addActionListener(this);
buttonPanel.add(n);
o.addActionListener(this);
buttonPanel.add(o);
p.addActionListener(this);
buttonPanel.add(p);
q.addActionListener(this);
buttonPanel.add(q);
r.addActionListener(this);
buttonPanel.add(r);
s.addActionListener(this);
buttonPanel.add(s);
t.addActionListener(this);
buttonPanel.add(t);
u.addActionListener(this);
buttonPanel.add(u);
v.addActionListener(this);
buttonPanel.add(v);
w.addActionListener(this);
buttonPanel.add(w);
x.addActionListener(this);
buttonPanel.add(x);
y.addActionListener(this);
buttonPanel.add(y);
z.addActionListener(this);
buttonPanel.add(z);
try{
Scanner inFile = new Scanner(new File("SAT.txt"));
int numWords = inFile.nextInt();
inFile.nextLine(); //needed to flush EOL
while(inFile.hasNext()){
String randomWord = inFile.nextLine();
String def = inFile.nextLine();
satWords.add(new Vocab(randomWord, def));
}
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
Random chooser = new Random();
int pick = chooser.nextInt(satWords.size());
winner = satWords.get(pick).getWord();
//THE WINNING WORD!
//must be kept hidden!
clueDef = satWords.get(pick).getDefinition();
//Definition used as a clue.
numLetters = satWords.get(pick).getNumLetters();
getSlots();
add(error, BorderLayout.SOUTH);
}
private void getSlots(){
add(cluePanel, BorderLayout.NORTH);
add(slotPanel, BorderLayout.CENTER);
JLabel visibleDefinition = new JLabel(clueDef);
cluePanel.add(visibleDefinition);
//add the selected vocab definition as a Label.
//definition given as a clue to help player figure out what the word is.
slots = new JButton[numLetters];
for(int i = 0; i < numLetters; i++){
slots[i] = new JButton("__");
slots[i].addActionListener(this);
slotPanel.add(slots[i]);
} //based on the number of letters
}
private boolean isWrong(JButton choice){
char [] correctLetters = new char[winner.length()];
for(int i = 0; i < winner.length(); i++){
correctLetters[i] = winner.charAt(i);
}
for(int i = 0; i < correctLetters.length; i++){
if(!(choice.getLabel().equals(correctLetters[i]))){
return true;
}
}
return false;
}
public void actionPerformed(ActionEvent e){
for(int i = 0; i < slots.length; i++){
if(e.getSource() == slots[i]){
if(slots[i].getLabel().equals("")){ //if the slot chosen is empty
slots[i].setBackground(Color.yellow);
if(e.getSource() == a){
if(!(isWrong(a))){
slots[i].setLabel("A");
}else{
error.setText("Wrong guess!");
}
}
if(e.getSource() == b){
slots[i].setLabel("B");
}
if(e.getSource() == c){
slots[i].setLabel("C");
}
if(e.getSource() == d){
slots[i].setLabel("D");
}
if(e.getSource() == e){
slots[i].setLabel("E");
}
if(e.getSource() == f){
slots[i].setLabel("F");
}
if(e.getSource() == g){
slots[i].setLabel("G");
}
if(e.getSource() == h){
slots[i].setLabel("H");
}
}
}
}
}
public static void main(String[] args) {
JFrame frame = new HangMan();
frame.setVisible(true);
}
}
Anyways, my current problem is that my JButtons to make the letters aren't showing up on my buttonPanel JPanel (it isn't showing up on my JFrame at least when I run it). The JButtons to make empty slots appeared very nicely (look at method getSlots()), but the letter buttons seem to be invisible. O.o On the old files that had to be replaced, they initially read my JButtons fine and put all of them up, including the letters. Just for some reason now the letters aren't appearing on the current file I'm using.
Here is my VERY WORK IN PROGRESS code for now. Alot of it is empty, but I'll fill them in later once I can figure out the issues. I am not a bright programmer; I've been taking a class for almost a year and I still don't understand alot of stuff.
I appreciate anyone's help. :D I am also open to suggestions to change/add/remove anything. Again, this needs to be done by Monday.