我曾經使用下面的掃描儀類來偵聽用戶通過cmd提示符輸入,但我試圖讓Java應用程序通過改用keyTyped方法來偵聽文本。如何在Java中使用keyTyped?
public static void main(String[] args) throws IOException {
new AsteroidsGame();
InputStream inputstream = new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
};
try {
FileOutputStream output = new FileOutputStream("HighScore.txt",true);
Scanner input = new Scanner(System.in);
PrintStream printStream = new PrintStream(output);
printStream.println(input.next() + " " + points);
printStream.close();
System.out.println("Success!");
}
catch(IOException e){
System.out.println(e);
}
}
我不知道該怎麼做,這將讓它偵聽按鍵和字母組合成一個字的方法。 (這是一個遊戲得到一個人的名字的方式)
@Override
public void keyTyped(KeyEvent e) {
if(lives == 0)
{
e.getKeyCode() = KeyEvent.???
}
}
任何建議,將不勝感激,因爲Java庫和因特網的其餘部分具有非常小的例子或幫助方面所提供的。
重點監聽器/事件爲Swing GUI的,無法安慰。什麼是KeyEvent的完全限定名? – user1803551
對不起,我沒有說清楚。這是一個擺動GUI。它是一款名爲小行星的遊戲,這是我在玩完遊戲後獲取用戶名的嘗試。至於名稱,這是我的問題,我不知道我需要放在哪一行,將接受某人輸入他們的名字英寸 – Thereisnospoon
如果這是一個Swing GUI,你沒有顯示任何組件的用法。你在畫什麼遊戲對象?什麼組件應該收到這個名字?爲什麼不使用正常的文本字段?小心與關鍵聽衆,他們可能會給你焦點問題,請參閱https://stackoverflow.com/questions/22741215/how-to-use-key-bindings-instead-of-key-listeners。 – user1803551