如果我使用的是GUI,並且有一個用戶可以鍵入的textField,然後該程序將鍵入,那麼在方法期間如何訪問KeyEvent? (keyEvent是當按下Enter鍵時 - >文本字段中的文本會產生響應)在方法中使用KeyEvent
例如:如果程序詢問用戶(通過方法)「你想吃這個蛋糕嗎? ?」然後用戶將輸入textField「yes」或「no」,根據響應,程序會給出另一個方法中的另一個問題或情況。
僞代碼:
public void cakeQuestion(){
eventList.setText(eventList.getText() + "\nWould You Like To Eat This Cake?"); //eventList is a textArea
//***KeyEvent takes place, perhaps saving the user's input as a String called resposne
if(response.equals("yes"){
eatCake //eatCake is another method with another situation
}
else if(response.equals("no"){
eatPie //eatPie is another method with another situation
}
else{eventList.setText(eventList.getText() + "\nI don't understand that response");}
}
@NullSaint:確實,一個不錯的答案。 1+ –