2011-12-29 4 views
0

시간 제한이 만료되기 전에 최대한 빨리 나타나는 단어를 입력해야하는 유형 게임이 있지만 단어를 입력 할 때마다 마우스를 움직여 입력을 클릭하고 다시 클릭해야합니다 입력하여 다음 단어를 입력하십시오. "keyCode.VK_Enter"를 사용하여 JButton에서 호출하는 Action Command를 실행하는 방법이 있다면 나는 바랬습니다. 내 코드의JButton의 키보드 단축키를 설정 하시겠습니까?

일부 조각 :

ENTER 버튼과 사용자 입력 및 출력 :

enter = new JButton("Enter"); 
    enter.setFont(serif); //serif is specified earlier 
    enter.setActionCommand("Enter"); 
    enter.addActionListener(this); 
    container.add(enter); 

userOutput = new JTextField(50); 
    userOutput.setFont(serif); 
    container.add(userOutput); 
    userOutput.setEditable(false); 

userInput = new JTextField(43); 
    userInput.setFont(serif); 
    container.add(userInput); 
    userInput.setEditable(false); 

엔터 버튼의 액션 명령을 받고 actionPerformed 메소드 :

if(userInput.getText().equals(userOutput.getText())){ 
      userInput.setText(""); 
      score += 100; 

답변

5

왜 돈 ' JTextField에 actionlistener를 추가하기 만하면됩니다 (사용자가 엔터를 치면 트리거됩니다).

userInput.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) { 
     // Do something 
    } 

}); 
+2

+1 예, ActionListener는 버튼과 텍스트 필드에서 공유 할 수 있습니다. – camickr

+0

와우! 고마워, ActionCommands를 공유 할 수 있는지 모르겠다. 감사! –

+0

@AmundeepSingh, 그러면 대답을 수락해야합니까? – camickr

관련 문제