代码之家  ›  专栏  ›  技术社区  ›  Tal

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

  •  0
  • Tal  · 技术社区  · 6 年前

    我正在制作一个类似于“谁想成为百万富翁?”我有一个“下一步”按钮,可以让玩家进入下一个问题。然而,由于我最多允许四名玩家玩,我希望在最多三次单击后禁用此“下一步”按钮(如果有一/两/三名玩家玩,则禁用更少)。

    以下是我到目前为止“下一步”按钮的代码:

    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.");
                }
            }
        });
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   yoav    6 年前

    您是否想过使用一个简单的int变量来计算一个玩家按下该按钮的次数?

    您对此有何看法:

        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
    
                }
            });
    
        2
  •  0
  •   eli    6 年前

    我想我会把它作为一个答案。这是一个工作示例,没有一些Android yitter。

    我们将使用 int counter . 每次有人单击您时,在 actionPerformed -阻止。如果 counter 大于2,已单击3次。我们将禁用 nextButton 具有 #setEnabled

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

    应该给你你需要的。