首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在按钮被点击一定次数后禁用它?

如何在按钮被点击一定次数后禁用它?
EN

Stack Overflow用户
提问于 2018-03-18 16:16:37
回答 2查看 68关注 0票数 0

我正在制作一个类似于“谁想成为百万富翁?”的游戏。我有一个“下一步”按钮,它把玩家带到下一个问题。然而,由于我允许最多四个玩家玩,我希望这个‘下一步’按钮禁用后,最多点击3次(或更少,如果有一个/两个/三个玩家发挥)。

到目前为止,下面是“Next”按钮的代码:

代码语言:javascript
复制
nextButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            StatusPage.setVisible(false);
            QuestionPage.setVisible(true);
            option1.setEnabled(true);
            option2.setEnabled(true);
            option3.setEnabled(true);
            option4.setEnabled(true);

            if (Player2JButton.isEnabled()){
                counter = 1;
            }
            else if (Player3JButton.isEnabled()){
                counter = 2;
            }
            else if (Player4JButton.isEnabled()){
                counter = 3;
            }
            else {
                System.out.println("Error getting next question.");
            }

            if (generalKnowledge.isEnabled()){

                currentQuest = quest.setQnA(option1, option2, option3, option4, question1, "generalKnowledge");
            }
            else if (geography.isEnabled()){

                currentQuest = quest.setQnA(option1, option2, option3, option4, question1, "geography");
            }
            else if (hipHopMusic.isEnabled()){

                currentQuest = quest.setQnA(option1, option2, option3, option4, question1, "hipHopMusic");
            }
            else {
                System.out.println("Error getting next question.");
            }
        }
    });
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-18 16:36:24

你有没有想过使用一个简单的int变量来计算玩家按那个按钮的次数?

你对此有何看法?

代码语言:javascript
复制
    final int NUMBER_OF_PLAYERS=4;
    int count=0;



     nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                count++; 
                if(count-1==NUMBER_OF_PLAYERS) 
                  {
                  nextButton.setEnabled(false);  //disable the button
                  }

              ///Your code

            }
        });
票数 0
EN

Stack Overflow用户

发布于 2018-03-18 16:44:33

我想我会把它加起来作为答案。这是一个工作样本,没有Android-yitter。

我们将使用int counter。每次有人单击actionPerformed-block中的增量计数器。如果counter大于2时,则单击3次。我们将禁用nextButton#setEnabled

代码语言:javascript
复制
public class ButtonCycle extends JPanel implements ActionListener {
    private int counter = 0;
    private JButton btn;

    public ButtonCycle() {
        this.btn = new JButton("Next");
        this.btn.addActionListener(this);

        add(this.btn);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame("Button cycling through animations");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setPreferredSize(new Dimension(250,250));
                f.setContentPane(new ButtonCycle());
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent a) {
        switch(this.counter) {
            case 0:
            case 1:
                this.counter++;
                break;
            case 2:
                ((JButton) a.getSource()).setEnabled(false);
                // or like this
                this.btn.setEnabled(false);
                break;
        }
    }
}

应该给你你所需要的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49350109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档