一个按钮JFrame

时间:2018-12-01 05:53:23

标签: java swing user-interface jbutton event-dispatch-thread

所以我正在一个项目上,想知道如何将多个侦听器放在一个按钮上。我希望能够设置某些文本字段的setText并一键启动游戏。但是,直到游戏结束,才更新文本字段。 (我只输入了我认为相关的代码,按下了按钮,因为它们非常相似,所以只从开关中放入了一种情况)。 我当时在想,如果该按钮可以有多个事件侦听器,那么它可以运行两种单独的方法来设置文本字段并一键玩游戏。输入来自不同的事件侦听器。

  private void startActionPerformed(java.awt.event.ActionEvent evt) {
      Scanner input = new Scanner(System.in);
    /**Prompts the user for their name*/
    System.out.println("What is your name?: ");
    /**Plays the game*/
    if((Hyd > 0 && Hung > 0) && (Temp > 0 && Rest > 0)){
    try{

        Deck cards = new Deck();


        cardNum =  ((int)(Math.random() * 5) + 1);;

        /**Plays a specific card method depending on the randomly generated number*/
        switch (cardNum)
        {
            case 1: question = ("You've run into a bear!");
                    greenChoice = ("Green Button: You RUN away!");
                    redChoice = ("Red Button: You FIGHT the bear!");
// i want to change this textfield as soon as the button is clicked
                               questionK.setText(question);
                    System.out.println("(Hold the button you choose until the game replies)");

                    /**Count down timer for the user*/
                    for (int i = 10; i >= 0; i--)
                    {
                        System.out.println("Timer: " + i);
                        Thread.sleep(i * 350);
                    }

0 个答案:

没有答案