public String letterTry() {
String letterToTry;
letterToTry=tryLetterJTextField.getText();
return letterToTry;
}
现在,我希望此方法等待用户按下Enter键,然后返回letterToTry并清除tryLetterJTextField。我正在尝试addKeyListener,但不幸的是我不理解它们,并且我已经尝试研究答案几个小时了。帮助将不胜感激 那是我的gui课
public class Demo extends KeyAdapter {
private JFrame frame=new JFrame("Hangman");
private JLabel firstLine=new JLabel("Guess title of this movie!");
private JLabel encryptedMovieLabel=new JLabel();
private Container container=new Container();
private JTextField tryLetter=new JTextField();
public void createGUI(String encryptedMovieTitle){
firstLine.setBounds(20,10,200,20);
encryptedMovieLabel.setBounds(20,30,200,20);
encryptedMovieLabel.setText(encryptedMovieTitle);
tryLetter.setBounds(20,60,200,20);
container.setPreferredSize(new Dimension(300,300));
container.setLayout(null);
container.add(firstLine);
container.add(encryptedMovieLabel);
container.add(tryLetter);
frame.add(container);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public String letterTry() {
String letterToTry;
letterToTry=tryLetter.getText();
return letterToTry;
}
}