单击一定次数后如何禁用按钮?
How to disable a button after it is clicked a certain number of times?
我正在制作一个类似于 'who wants to be a millionaire?' 的游戏,我有一个 'Next' 按钮,可以让玩家进入下一个问题。但是,由于我最多允许四名玩家玩游戏,因此我希望此 'Next' 按钮在最多点击 3 次后禁用(如果有 one/two/three 玩家玩游戏,则点击次数更少)。
这是我目前为 'Next' 按钮编写的代码:
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.");
}
}
});
您是否考虑过使用一个简单的 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
}
});
我想我会把它添加为答案。这是一个工作示例,没有一些 Android-yitter.
我们将使用 int counter
。每次有人点击时,您都会在 actionPerformed
块内增加计数器。如果 counter
大于 2,则它被点击了 3 次。我们将使用 #setEnabled
.
禁用 nextButton
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;
}
}
}
应该给你你需要的。
我正在制作一个类似于 'who wants to be a millionaire?' 的游戏,我有一个 'Next' 按钮,可以让玩家进入下一个问题。但是,由于我最多允许四名玩家玩游戏,因此我希望此 'Next' 按钮在最多点击 3 次后禁用(如果有 one/two/three 玩家玩游戏,则点击次数更少)。
这是我目前为 'Next' 按钮编写的代码:
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.");
}
}
});
您是否考虑过使用一个简单的 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
}
});
我想我会把它添加为答案。这是一个工作示例,没有一些 Android-yitter.
我们将使用 int counter
。每次有人点击时,您都会在 actionPerformed
块内增加计数器。如果 counter
大于 2,则它被点击了 3 次。我们将使用 #setEnabled
.
nextButton
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;
}
}
}
应该给你你需要的。