2013-03-26 245 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接口」與聽衆。