简体   繁体   English

Java命令行退出命令?

[英]Java command line exit command?

I'm making a server, and it is on a Text Based Raspberry Pi. 我正在制作服务器,它位于基于文本的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(); 基本上,一切都是从命令行运行的,所以当服务器运行时,没有图形,它使用System.out.println();打印出所有内容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? 所以我的问题是,不是让一个按钮运行shutdown()方法,我怎么能在任何时间点这样做,我能够推,比如'e',程序将运行shutdown()方法? 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? 我在想添加一个keylistener ,但我不确定是否可以添加到任何图形? 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. 要注册键盘事件,首先需要关注程序,为此你需要一个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步。

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". 好吧,所以基于评论,(我投了btw),我做了一个线程,不断使用scanner ,看看我输入“退出”。 Thanks for all the help! 感谢您的帮助!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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