2013-03-26 255 views
3

我正在制作一个服务器,它位于基于文本的树莓派上。基本上,一切都从命令行运行,所以当服务器运行时,没有图形,并使用System.out.println();打印所有内容。所以我的问题是,我没有一个运行shutdown()方法的按钮,我怎么能在任何时候使它成为如此,我能够推动'e',并且程序将运行shutdown()方法?我已经做了一些搜索,我不太确定如何解释这个问题。我想增加一个keylistener,但我不知道如果这可以添加到什么图形?无论如何,任何帮助将不胜感激!在此先感谢Java命令行退出命令?

+0

你的意思是像Tomcat命令启动和停止的东西? – 2013-03-26 00:28:47

+0

老实说,我不知道tomcat是什么,但更像是,我做了一个名为shutdown()的方法来运行一切。我只需要按下一个键就可以关闭它,但我不知道如何...... – PulsePanda 2013-03-26 00:29:31

+1

@PaulVargas宁可扫描仪,检测'出口'键入在cmd行 – dantuch 2013-03-26 00:29:46

回答

0

好吧,所以基于评论,(我上调btw),我做了一个线程,不断使用scanner看看我是否键入“退出”。感谢所有的帮助!

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

如果你不希望有一个可见的图形用户界面,您可以创建一个“始终专注隐形grafical接口”与听众。