我正在制作一个类似于“谁想成为百万富翁?”的游戏。我有一个“下一步”按钮,它把玩家带到下一个问题。然而,由于我允许最多四个玩家玩,我希望这个‘下一步’按钮禁用后,最多点击3次(或更少,如果有一个/两个/三个玩家发挥)。
到目前为止,下面是“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.");
}
}
});发布于 2018-03-18 16:36:24
你有没有想过使用一个简单的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
}
});发布于 2018-03-18 16:44:33
我想我会把它加起来作为答案。这是一个工作样本,没有Android-yitter。
我们将使用int counter。每次有人单击actionPerformed-block中的增量计数器。如果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;
}
}
}应该给你你所需要的。
https://stackoverflow.com/questions/49350109
复制相似问题