0

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. 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;


public game(){

    setLayout(null);
    setSize(300,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(60,30,1300,30);
    click.setBounds(50,130,100,30);
    click.addActionListener(this);
    click1.setBounds(150,130,125,30);
    click1.addActionListener(this);
    click2.setBounds(125,160,125,30);
    click2.addActionListener(this);
    add(click);
    add(click1);
    add(name);
    add(name1);
    add(prompt);


}

public void actionPerformed(ActionEvent e) {

    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.setText("What would you like to do now?");
        click.setText("Go forward!");
        click1.setText("Turn on light.");

    }
    }
}

public void actionPerformed1(ActionEvent e) {

    if(e.getSource() == click) {

        JOptionPane.showMessageDialog(null, "You walk down the tunnel until you hit an intersection.");
        prompt.setText("Which way would you like to go?");
        click.setText("Left!");
        click1.setText("Right!");
        add(click2);
        click2.setText("Keep going!");
    }

    if(e.getSource() == click1) {

    }
}

public static void main(String args[]){

    game s = new game();
    s.setVisible(true);
    }
}
Dogrules23
  • 21
  • 1
  • 5
  • What about marking the options you can't choose as disabled ([JComponent#setEnabled(boolean)](https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#setEnabled%28boolean%29)) or making them invisible ([JComponent#setVisible(boolean)](https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#setVisible%28boolean%29))? – mezzodrinker Nov 16 '15 at 22:32
  • Possible duplicate of [I am making a GUI based text adventure game in Java. How do I make the message boxes update to show the outcomes?](http://stackoverflow.com/questions/33759338/i-am-making-a-gui-based-text-adventure-game-in-java-how-do-i-make-the-message-b) – Frakcool Dec 08 '15 at 17:59
  • This was the old one. – Dogrules23 Dec 08 '15 at 18:14

1 Answers1

0

In your actionPerformed function, use

prompt.setText(" new prompt");

to set the prompt to another prompt.

You might also change the options with:

click.setText(" new click command");
click1.setText(" new click1 command");
WillShackleford
  • 6,918
  • 2
  • 17
  • 33