I am trying to create a small Java program that has a simple command line like widget not so different from that of idle. I was originally planning to use a JTextArea and try to detect when someone presses enter, but I was wondering if there would be a better way of doing this?
I think, it is more stylish way to have ENTER to terminate. If you want to have along with some button that will not easy as idle one. I suggestion you have JTEXTAREA, you user can have wide area to see the things that they have keyed. I do not have much idea on this :) thanks
Related
I'm programming on java, so I have to make a game-like program. The game should be able to "pause" at any point. Can you press a key at any point so a message is displayed saying "game is paused"? Then when you press that key again it will say "game resumed"?
Thanks
It looks like you need to be able to read input directly from the keyboard into your game? Is your game built into a GUI or in the console?
This can be done by using the KeyListener interface Java provides.
https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html
I'll assume your using swing so this tutorial should be pretty useful.
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
Sorry I can't go into more detail, more information about what you are trying to do in the comments would help me help you.
Edit: You mentioned you're using the command line. To get user input you can use java.util.Scanner
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Make a boolean, isPaused. If space is pressed, switch isPaused from true to false or vice versa. If isPaused is true, run the game, if not, run a pause screen instead.
So I want to be able to simulate mouse clicks on my window/frame, but in the background. This means I cant use the Robot class because it takes control of the OSes mouse which is not what I want (please guys, I know of and use the Robot class, it's not what I need).
I want the program to think I clicked on part of it (x,y on the frame) even when minimized (doesnt have to minimized but at least out of focus). I dont want it to take over my computer since it should ideally be a background task.
I did a little bit of research before posting and I read that I could make an Applet and then use the "reflection class"(?) and mouselisteners to invoke mouse events on the frame itself rather than through the OS. Not sure if itll work cause the explanation was pretty meh and the guy said they could communicate individually if he had problems :/.
Kind of wondering if it's possible at this point. If it cant be done in java, I know a little python, so if there's a solution for it in python, that could work to.
TLDR: Need to simulate mouseclicks on my frame, but it cant use the os mouse and should ideally work when the application is out of focus/in the background.
Thanks in advance :D
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)
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.
I was wondering if it was possible if I could open Multiple swing dialog boxes simultaneously.
Can't think of any reason why you couldn't, but surely this is something that would take you 5 minutes to write a simple app just to test it?
You can do it, just make sure the secondary dialog is owned by the primary one. I think I've seen some weird z-order issues if they're not parented correctly. Swing can also be temperamental at times.
I am trying to get my options straight with some inputs that I want to enter in an application I am developing.
I want the user to input a list(of Strings) of which the size will be his decision.
I have thought of some crude solutions, like going with JOptionPane until user enters a specific input, but I would like a solution where the user can see his previous entries before he submits them all (unlike the JOptionPane solution). So I guess my question is, if there's any fast/easy way to do this - similar to JOptionPane's easiness and speed, instead of making a whole JPane design for it.
Thanks
I don't know of a fast and easy solution like JOptionPane. I think your best bet is to create a one-column JTable inside a scroll pane. There's a fairly straight-forward JTable tutorial that should get you started.