简体   繁体   中英

Java command line exit command?

I'm making a server, and it is on a Text Based Raspberry Pi. basically, everything is running from the command line, so when the server runs, there is no graphics, and it prints everything out using System.out.println(); . so my question is, instead of having a button that runs a shutdown() method, how can i make it so at any point in time, i am able to push, say, 'e', and the program will run the shutdown() method? i've done some searching, and am not sure quite how to phrase the question. i was thinking adding a keylistener , but im not sure if that can be added to nothing graphic? anyway, any help would be appreciated!!! thanks in advance

To register keyboard events you first need to have the focus on your program, and for that you need a gui. I suggest:

1.- Create a JLabel(and a scrollbar).

2.- Instead of using System.out.print("text");, use myJLabel.append("text" + "/n");. 

3.- Add a keyboard listener. Register key events so that the x key closes your server, the s key stops it, ...

As you have described your app, you can't write input to the server, you can only read output from it. I recomend step 4.

4.-Add a JTextFiel to send input to the server.

5.-To make it user-friendly , you could use a JEditorPane instead of a JLabel, and add HTML to your output.

Alright, so based on the comments, (which i up voted btw), i made a thread that constantly used scanner to see if i typed "exit". Thanks for all the help!

如果你不想拥有一个可见的gui,你可以与监听器一起创建一个“始终聚焦的隐形grafical接口”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM