I would like to wait for my gui (using swing) to be complete and for the user to click on the button he would like.
With this code and this while (this.choice == -2) {}
, my program get stucked at this point and doesn't answer anymore. The gui is not loading in fact
I would like to return the good number on PlayATurn() after the user take his choice. The only thing that I can't change are the header of PlayATurn() and the number to give back wich are 1,0,-1.
Any idea how to resolve this ?
Here's the code
public class Human extends Player { //Player can be also a bot, that's why Human extend player
//The
private int choix = -2;
private void setChoix(int n) {
this.choix = n;
}
public int getChoix() { return this.choix; }
public void myWindow() {
JFrame frame = new JFrame();
frame.setTitle("My title);
frame.setSize(400, 150);
frame.setBackground(Color.white);
frame.setResizable(false);
//pan.setLayout(new BorderLayout());
JPanel pan = new JPanel();
pan.setBackground(Color.white);
JLabel displayLabel = new JLabel ("Player 1 : It's your turn to play");
JPanel buttonPanel = new JPanel();
JButton buttonGoDown = new JButton("Go up");
JButton buttonGoUP = new JButton("Go down");
JButton buttonTake = new JButton("Take an item");
buttonGoDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
setChoice(1);
}
});
buttonGoUP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
setChoice(-1);
}
});
buttonTake.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
setChoice(0);
}
});
buttonPanel.add(buttonGoDown);
buttonPanel.add(buttonGoUp);
buttonPanel.add(buttonTake);
pan.add(displayLabel,BorderLayout.NORTH);
pan.add(buttonPanel, BorderLayout.SOUTH);
frame.setContentPane(pan);
//frame.setContentPane(pan);
frame.setVisible(true);
}
@Override
public int playATurn() {
myWindow();
while (this.choice == -2) {} // THE PART THAT I WOULD LIKE TO REMOVE
return choice;
}