I am a beginner writing a quiz game in Swing Java, i want the user/s to answer a question by pressing a key. I have trouble in making the program wait till a key is pressed.
I have tried calling the method and then having a while loop with a Getter method for answer1 to "pause" the program until the variable is the answer that i want but the program just froze when i did that.
I have searched a lot but nothing quite works. Here is a sample of the code:
public static void addKeyBinding(int keyCode, String id, ActionListener lambda){
InputMap im = GUI().getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = GUI().getActionMap();
im.put(KeyStroke.getKeyStroke(keyCode, 0 ,false), id);
am.put(id, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
lambda.actionPerformed(e);
}
});
}
public char keyOperationSinglePlayer(){
addKeyBinding(KeyEvent.VK_A, "A", (evt) -> {
answer1 = 'a';
});
addKeyBinding(KeyEvent.VK_S, "S", (evt) -> {
answer1 = 'b';
});
addKeyBinding(KeyEvent.VK_D, "D", (evt) -> {
answer1 = 'c';
});
addKeyBinding(KeyEvent.VK_F, "F", (evt) -> {
answer1 = 'd';
});
return answer1;
}`
Thanks
public class GUI extends JPanel
Well, it's been a year and I stumbled across my old project and decided for anyone interested to post what i finally did.
/**
* Assigns keys and then invokes the methods of "answer processing" logic
*
* @throws IOException , exception
*/
public void keyAssignment() throws IOException {
//key assignment
CorrectAnswer ca = CorrectAnswer.CorrectAnswer();
Betting b = Betting.Betting();
Questions questions = Questions.Question();
Rounds rounds = Rounds.Rounds();
//reset keys
answer1 = ' ';
answer2 = ' ';
game3Panel.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
//super.keyTyped(e);
char temp;
switch (e.getKeyChar()){
case 'a' :
//a for player 1
answer1 = 'a';
delUpdatedScreen();
temp = questions.getCorrectLetter();
if(rounds.getRound().equals("ca")){
ca.setPoints(resultPane(answer1, temp), 1);
}
if(rounds.getRound().equals("b")){
b.setPoints(resultPane(answer1, temp), 1);
}
break;
case 's' :
//b for player 1
answer1 = 'b';
delUpdatedScreen();
temp = questions.getCorrectLetter();
if(rounds.getRound().equals("ca")){
ca.setPoints(resultPane(answer1, temp), 1);
}
if(rounds.getRound().equals("b")){
b.setPoints(resultPane(answer1, temp), 1);
}
break;
case 'd' :
//c for player 1
answer1 = 'c';
delUpdatedScreen();
temp = questions.getCorrectLetter();
if(rounds.getRound().equals("ca")){
ca.setPoints(resultPane(answer1, temp), 1);
}
if(rounds.getRound().equals("b")){
b.setPoints(resultPane(answer1, temp), 1);
}
break;
case 'f' :
//d for player 1
answer1 = 'd';
delUpdatedScreen();
temp = questions.getCorrectLetter();
if(rounds.getRound().equals("ca")){
ca.setPoints(resultPane(answer1, temp), 1);
}
if(rounds.getRound().equals("b")){
b.setPoints(resultPane(answer1, temp), 1);
}
break;
case 'h' :
if(players == 2){
//a for player 2
answer2 = 'a';
delUpdatedScreen();
temp = questions.getCorrectLetter();
if(rounds.getRound().equals("ca")){
ca.setPoints(resultPane(answer2, temp), 2);
}
if(rounds.getRound().equals("b")){
b.setPoints(resultPane(answer2, temp), 2);
}
break;
}
case 'j' :
if(players == 2){
//b for player 2
answer2 = 'b';
delUpdatedScreen();
temp = questions.getCorrectLetter();
if(rounds.getRound().equals("ca")){
ca.setPoints(resultPane(answer2, temp), 2);
}
if(rounds.getRound().equals("b")){
b.setPoints(resultPane(answer2, temp), 2);
}
break;
}
case 'k' :
if(players == 2){
//c for player 2
answer2 = 'c';
delUpdatedScreen();
temp = questions.getCorrectLetter();
if(rounds.getRound().equals("ca")){
ca.setPoints(resultPane(answer2, temp), 2);
}
if(rounds.getRound().equals("b")){
b.setPoints(resultPane(answer2, temp), 2);
}
break;
}
case 'l' :
if (players == 2){
//d for player 2
answer2 = 'd';
delUpdatedScreen();
temp = questions.getCorrectLetter();
if(rounds.getRound().equals("ca")){
ca.setPoints(resultPane(answer2, temp), 2);
}
if(rounds.getRound().equals("b")){
b.setPoints(resultPane(answer2, temp), 2);
}
break;
}
}
}
});
}
And this is the snippet of code that invokes it.
game3Panel = new JPanel();
game3Panel.setOpaque(false);
game3Panel.setVisible(false);
game3Panel.setBackground(Color.WHITE);
BoxLayout boxlayout = new BoxLayout(game3Panel, BoxLayout.Y_AXIS);
game3Panel.setLayout(boxlayout);
picLabel = new JLabel();
picLabel.setIcon(null);
game3Panel.add(picLabel, BorderLayout.CENTER);
cat = new JLabel("");
game3Panel.add(cat);
question = new JLabel("");
game3Panel.add(question);
ans1 = new JLabel("");
game3Panel.add(ans1);
ans2 = new JLabel("");
game3Panel.add(ans2);
ans3 = new JLabel("");
game3Panel.add(ans3);
ans4 = new JLabel("");
game3Panel.add(ans4);
frame.setVisible(true);
frame.setFocusable(false);
try {
keyAssignment();
} catch (IOException e) {
e.printStackTrace();
}
This is not the best solution to this problem and it is kinda rushed but i was new to programming and in a hurry to finish before the deadline, so if you have a better solution, or want more clarifications on the code, don't hesitate to let me know! :)