I created a text-based game similar to Zork and I need a gui to run it outside of Eclipse. I want to run it as a jar. (by the way I'm on a mac if that changes anything). I only need an output field and an input field. What would be the easiest way to achieve this? And how much of my code would I need to change? (I used System.out.print for output and a Scanner for input)
-
1what's wrong in running it in the Mac console? – JB Nizet Mar 15 '13 at 15:43
-
Take a look at http://stackoverflow.com/questions/9244108/redirect-system-in-to-swing-component – Richard Neish Mar 15 '13 at 15:45
2 Answers
If you want to crate GUI like console the simple way to do it is to add textarea component to your frame or or panel that has scroll bars through the viewport. Create a stream that feeds the component with text. Then simply redirect standard output to that stream. Finish. Start your GUI and enjoy the console.
If you don't want to run this on a terminal, you should probably use Swing with a JTextArea in which you append all the messages to the user, and a simple JTextField for the user to enter his commands.
Here's a quick example of JTextArea so you get an idea. You'll need to read more about events on Swing to make things like reacting to the user pressing the ENTER key to read the contents of the text field and run the game logic.
Note that the screenshot on the example above uses the "Metal" look and feel, but it should look much closer to a native application on the Mac.

- 3,271
- 1
- 17
- 22