1
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;


    class DD {
    public static void main(String[] args){

    JFrame myFrame = new JFrame("#########");
    myFrame.setSize(640,480);
    myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    myFrame.setVisible(true);
    JTextArea textArea = new JTextArea();
    myFrame.add(textArea);
    new JTextArea();
    textArea.setEditable(false);

    //System objects
    Scanner in = new Scanner(System.in);
    boolean running = true;
    textArea.append("\t\n########################################");
    textArea.append("\t\n>#############");
    textArea.append("\t\n>Th#############11!");
    textArea.append("\t\n>Typ#############enture!");
    textArea.append("\t\n########################################");
    String input = in.nextLine();
    if(input.equals("start")){
            { ///beginning of story.
        if(running)
        textArea.append("\t\nYo#############.");
        textArea.append("\t\n#############");
        textArea.append("\t\n1.#############t.");
        textArea.append("\t\n2.G#############t.");
        String input1 = in.nextLine();
        if(input1.equals("1")){
            textArea.append("\n>Y#############");
            textArea.append("\n>#############");
            textArea.append("\n>A#############");
            textArea.append("\n>1.#############");
            textArea.append("\n>2.#############");
        if(input.equals("1")){
                textArea.append("\n>#############");
                textArea.append("\n>#############");
                textArea.append("\n>Ga#############d.");
                    }
        if(input.equals("2")){
                textArea.append("\n>#############");
                textArea.append("\n>#############");
                textArea.append("\n>Y#############ars");
                textArea.append("\n>Y#############");
                    }
        }
        else if(input1.equals("2")){
            textArea.append("\n>Y#############.");
            textArea.append("\n>Y#############.");
            }
        }
    }
}
}

This is my text adventure game, i'm stuck on how to get users input. i have read about 'action listener' i don't know how to use it, but i really want the input to be entered like its in a console or terminal/cmd like program. the user just needs to enter 1, 2 or 3 to preform an action.

eels11
  • 13
  • 5
  • possible duplicate http://stackoverflow.com/questions/5287538/how-to-get-basic-user-input-for-java – Deh Aug 25 '15 at 06:58
  • @Deh I wouldn't think so, I think (or I hope) the OP is trying to use a GUI to get the user input, but they simply don't understand the basic concepts of an event driven environment – MadProgrammer Aug 25 '15 at 07:02
  • @MadProgrammer oh yes that's right was tricked by the title, then this one may be a duplicate of http://stackoverflow.com/questions/16390503/java-swing-getting-input-from-a-jtextfield – Deh Aug 25 '15 at 07:05

2 Answers2

2

Dump the Scanner to start with. Use a JTextField. You need to understand that you're operating in an event driven environment, this means that you need to make use of the Observer Pattern to be notified when something changes

Start by taking a look at Creating a GUI With JFC/Swing, How to Use Text Fields, How to Write an Action Listeners for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • i had scanner on because my game was running in a console in eclipse. – eels11 Aug 25 '15 at 07:10
  • 2
    Well, if you want to use a GUI, then use a GUI. If you want to use the console, then use the console, not both – MadProgrammer Aug 25 '15 at 07:13
  • i forgot to remove it, because currently i'm learning to make my game run in the GUI and when i test my game to see if the story is working i use the console and just remove the Jframe code. i'm in the process of making my game stable in a GUI.. – eels11 Aug 25 '15 at 07:39
  • @eels11 The scanner reacts to the user pressing the enter key before something happens, do you want the GUI version with a textfield to do the same? If so: I'd still do a step in between and first make it work by pressing a button and when you've got that working, try to remove the button and make it work by pressing the enter key. – Gimby Aug 25 '15 at 07:54
  • how do i add a add method? sorry but i'm new to java programming, – eels11 Aug 26 '15 at 06:39
  • You don't. Look at the linked tutorials, they show you how to create a `JFrame` and how to add components to a container – MadProgrammer Aug 26 '15 at 06:43
  • I have been reading the links and they have helped a lot, but i have one problem when i use Frame.pack(); it makes my window small (only the text field shows, until you stretch the window/applet). how would i keep the window the same size (i have tried .setResizable(false))? also .pack(); makes my textfield appear... without it i have no text field in my game – eels11 Aug 26 '15 at 09:24
  • hopefully this is my last question (i hope i'm not annoying you), my JTextfield covers my whole screen, and i just want it on the top of my screen... i have jt.setHorizontalAlignment(JTextField.TRAILING); . i don't know what to try anymore.... (it covers the whole background and grows if i resize the window, its also behind my textArea..) – eels11 Aug 27 '15 at 08:38
  • `JFrame` uses a `BorderLayout` by default, I think you want to add your `JTextArea` to the center position, which is the default position and your `JTextField` to the `SOUTH` (maybe `NORTH`); `frame.add(textField, BorderLayout.SOUTH);`, see [How to Use BorderLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html) for more details ;) – MadProgrammer Aug 27 '15 at 08:40
  • i have made a action listener and i want it to add text to my TextArea. is that possible? (my action listener does nothing right now). – eels11 Aug 28 '15 at 05:32
  • Yes, `JTextArea#append` would be the best solution (`JTextField#getText` to get the text), but you will need to have access to the components, normally best to use class instance fields – MadProgrammer Aug 28 '15 at 05:34
0

For starters there is a leak here:

myFrame.add(textArea);
**new JTextArea();**
textArea.setEditable(false);

(The line in bold located in the first lines of main, and it needs to be modified or removed)

Regarding your actual Q, I think that since it is meant to be a text based game, you could do something like this to accept user input:

(...)

public static void main(String[] args)
{
   (...)
   // create a scanner to read the command-line input
   Scanner scanner = new Scanner(System.in);
   bool running = true;

   init();

   //game loop
   while (running)
   {
    if(scanner.next() == 1)
     {
        // do this
     }else if ( == 2)
     {
        // do that
     }//etc
    //and simply change running to false, when you want the game to end
   }
   (...)
}

//this is simply a function to present the initial choices and general info to the user.
   private void init(){System.out.println("Add instructions for the user here, e.g. telling them that they have to press 1,2, or 3");}
Mechanic
  • 172
  • 1
  • 10