Gui没有加载,因为程序陷入无限循环

时间:2018-01-08 17:28:26

标签: java swing

我想等待我的gui(使用swing)完成,并让用户点击他想要的按钮。 使用此代码和此while (this.choice == -2) {},我的程序在这一点上陷入困​​境,并且不再回答。 gui实际上没有加载

我想在用户选择后返回PlayATurn()上的好号码。 我唯一不能改变的是PlayATurn()的标题,并且返回的数字是1,0,-1。

知道如何解决这个问题吗?

这是代码

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;
}

0 个答案:

没有答案