Reputation: 4862
I'm trying to make a program with a 'new game' button which when pressed asks the user a new question. It's a question about state capitals and I want each new game to just change the name of the state in the question. This is probably a dumb way to do it but I'm just experimenting and trying to learn more about java.
The problem I have is that it only works once. I press new game and it changes ok the first time and then it does nothing. I've tried using various combination of while and for loops but with no luck. Sometimes the program even crashed when I tried using counter++ in some combination of for loop! So, yeah I'm pretty stuck so any hel would be appreciated greatly thanks.
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args){
Gui a = new Gui();
List<String> stateList = new ArrayList<String>();
List<String> capsList = new ArrayList<String>();
for(String x: StateData.states)
stateList.add(x);
for(String z: StateData.caps)
capsList.add(z);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Gui extends JFrame {
private static final long serialVersionUID = 1L;
JLabel instructions = new JLabel("What is the capital of Alabama?");
JLabel aLabel = new JLabel("Answer: ");
JTextField aField = new JTextField(16);
JLabel result = new JLabel();
JButton submit = new JButton("Submit");
JButton reset = new JButton("Reset");
JButton ng = new JButton("New Game");
final Random rand = new Random();
final int randPairNo = rand.nextInt(50);
final Rng rngOb1 = new Rng();
final int[] shuffList = rngOb1.numFinder();
public Gui() {
super("State Capitals Game");
setLookAndFeel();
setSize(325, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.insets = new Insets(5, 5, 5, 5);
gc.gridx = 0;
gc.gridy = 0;
add(instructions, gc);
gc.gridx = 0;
gc.gridy = 1;
add(aLabel, gc);
gc.gridx = 0;
gc.gridy = 2;
add(aField, gc);
gc.gridx = 0;
gc.gridy = 3;
add(result, gc);
gc.gridwidth = 3;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 4;
add(submit, gc);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userIP = aField.getText();
String mg = StateData.caps[randPairNo];
if (userIP.equalsIgnoreCase(mg)) {
result.setText("That's correct!");
} else {
result.setText("Sorry, that's incorrect.");
}
}
});
gc.gridwidth = 3;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 5;
add(reset, gc);
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
aField.setText("");
}
});
gc.gridwidth = 3;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 6;
add(ng, gc);
//This is the new game button I'm trying to get to work.
ng.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int counter = 0;
if (counter < shuffList.length) {
instructions.setText("What is the capital of "
+ StateData.states[shuffList[counter]] + "?");
counter++;
} else {
counter = 0;
}
}
});
}
private void setLookAndFeel() {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
}
}
import java.util.Random;
public class Rng {
int[] numFinder() {
int[] list = new int[50];
for (int i = 0; i < list.length; i++) {
list[i] = i;
}
Random rgen = new Random();
for (int i = 0; i < list.length; i++) {
int rnd = rgen.nextInt(list.length);
int temp = list[i];
list[i] = list[rnd];
list[rnd] = temp;
}
return list;
}
}
public class StateData {
public static String[] states = {"Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii",
"Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisianna",
"Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
"Missouri", "Montana", "Nebraska", "Nevada", "New Hampsire", "New Jersey",
"New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
"Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia",
"Winsconsin", "Wyoming" };
public static String [] caps = {"Montgomery", "Juneau", "Pheonix", "Little Rock", "Sacremento", "Denver",
"Hartford", "Dover", "Talahasee", "Atlanta", "Honolulu", "Boise", "Springfield", "Idianapolis",
"Des Moines", "Topka", "Franfort", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing",
"St Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Concord",
"Trenton", "Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus", "Oklahama City",
"Salem", "Harrisburg", "Providence", "Columbia", "Pierre", "Nashville", "Austin", "Salt Lake City",
"Montpelier", "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne"};
}
Upvotes: 2
Views: 814
Reputation: 14705
Your problem seems to lie in the ActionListener. You have
public void actionPerformed(ActionEvent e) {
int counter = 0;
This will reset counter each time the button is pressed. Consistent with your description.
Declare counter as a field in your Gui class and you should be fine.
Upvotes: 4