I added more prompts now and the message boxes should say different things but they are stuck at "You go forward and hit a wall" and "You are now facing the opposite direction" They don't change when they should and the third option that I added isn't showing up. I can already get the messages to display right the first time but after you make the first decision and the buttons change, the message boxes do not. Again, here's my code:
package game;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class game extends JFrame implements ActionListener {
JLabel prompt;
JTextField name;
JTextField name1;
JButton click;
JButton click1;
String storeName;
String storeName1;
JButton click2;
private int gameState = 0;
public game(){
setLayout(null);
setSize(550,250);
setTitle("Text Adventure");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
prompt = new JLabel("You find yourself in a tunnel.");
click = new JButton("Go forward!");
click1 = new JButton("Turn around!");
click2 = new JButton();
name = new JTextField();
name1 = new JTextField();
prompt.setBounds(170,30,1300,30);
click.setBounds(100,130,100,30);
click.addActionListener(this);
click1.setBounds(300,130,125,30);
click1.addActionListener(this);
click2.setBounds(185,175,145,30);
click2.addActionListener(this);
add(click);
add(click1);
add(name);
add(name1);
add(prompt);
}
public void actionPerformed(ActionEvent e) {
if (gameState == 0){
if(e.getSource() == click) {
JOptionPane.showMessageDialog(null, "You go forward and hit a wall.");
}
if(e.getSource() == click1) {
JOptionPane.showMessageDialog(null, "You are now facing the opposite direction.");
prompt.setBounds(50,30,1300,30);
prompt.setText("What would you like to do now that you are facing the opposite direction?");
click.setText("Go forward!");
click1.setText("Turn on light.");
}
gameState = 1;
}
else if (gameState == 1){
if(e.getSource() == click) {
JOptionPane.showMessageDialog(null, "You walk down the tunnel until you hit an intersection.");
prompt.setBounds(100,30,1300,30);
prompt.setText("Which way would you like to go at the intersection?");
click.setText("Left!");
click1.setText("Right!");
add(click2);
click2.setText("Keep going!");
}
if(e.getSource() == click1){
JOptionPane.showMessageDialog(null, "You turn on your light.");
prompt.setBounds(220,30,1300,30);
prompt.setText("What now?");
click.setText("Go forward!");
click1.setText("Sit down.");
}
gameState = 2;
}
else if (gameState == 2) {
if (e.getSource() == click) {
JOptionPane.showMessageDialog(null, "You turn left and walk into a dark room.");
prompt.setBounds(210,30,1300,30);
prompt.setText("What do you do?");
click.setText("Cry");
click1.setText("Shout 'Hello?'");
click2.setText("Explore the room");
}
}
}
public static void main(String args[]){
game s = new game();
s.setVisible(true);
}
}