Java命令行退出命令?

时间:2013-03-26 00:26:15

标签: java command-line terminal keylistener shutdown

我正在制作服务器,它位于基于文本的Raspberry Pi上。基本上,一切都是从命令行运行的,所以当服务器运行时,没有图形,它使用System.out.println();打印出所有内容。所以我的问题是,我没有一个运行shutdown()方法的按钮,而是如何在任何时间点都这样做,我能够推,比如说'e',程序将运行shutdown()方法?我做了一些搜索,我不确定如何说出这个问题。我正在考虑添加一个keylistener,但我不确定是否可以添加到任何图形?无论如何,任何帮助将不胜感激!提前谢谢

3 个答案:

答案 0 :(得分:1)

要注册键盘事件,首先需要关注程序,为此你需要一个gui。我建议:

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, ...

正如您所描述的应用程序,您无法将输入写入服务器,您只能从中读取输出。我推荐第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.

答案 1 :(得分:0)

好吧,所以根据评论,(我投了btw),我做了一个线程,不断使用scanner来查看我是否输入了“退出”。谢谢你的帮助!

答案 2 :(得分:0)

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