gpt4 book ai didi

java - 如何执行 JButton Action 监听器一定次数

转载 作者:行者123 更新时间:2023-12-01 22:30:04 25 4
gpt4 key购买 nike

我有一些代码,其中有 5 个 JButtons 的代码,并且有一个单独的数组,可以生成 1-6 之间的随机数(骰子滚动),并且在操作监听器内,我只有将按钮添加到面板的部分(panel.add(roll1)) 在 Action 监听器之前,我有一个生成随机数的数组,然后有一个开关,表示如果随机数为 1,则将图像设置为 dice1,如果数组为 2,则将图像设置为 dice2。所以我现在已经完成了所有这些工作,因为我正在制作 Yahtzee 游戏,所以我需要知道如何使其每次移动最多可运行 3 次。现在,JButton 仅单击一次,它会输出带有随机骰子图像的其他 JButton,但是当我再次单击滚动按钮按钮时,骰子不会滚动,它保持不变。你会怎么做?

public static void randomRoll(final JPanel panel) throws Exception
{

final ImageIcon icon = new ImageIcon(new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Dice-1.png/45px-Dice-1.png"));
final ImageIcon icon1 = new ImageIcon(new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/1/18/Dice-2.png/45px-Dice-2.png"));
final ImageIcon icon2 = new ImageIcon(new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Dice-3.png/45px-Dice-3.png"));
final ImageIcon icon3 = new ImageIcon(new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Dice-4.png/45px-Dice-4.png"));
final ImageIcon icon4 = new ImageIcon(new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/6/6c/Dice-5.png/45px-Dice-5.png"));
final ImageIcon icon5 = new ImageIcon(new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Dice-6.png/45px-Dice-6.png"));

final ImageIcon [] diceIcons = {icon, icon1, icon2, icon3, icon4, icon5};

int array [] = new int [5];


for(int i = 0; i < 5; i++)
{
array [i]= (int) (Math.random () * 6) + 1;
System.out.println(array[i]);
}

final JButton roll1 = new JButton(diceIcons[array[0]-1]);
final JButton roll2 = new JButton(diceIcons[array[1]-1]);
final JButton roll3 = new JButton(diceIcons[array[2]-1]);
final JButton roll4 = new JButton(diceIcons[array[3]-1]);
final JButton roll5 = new JButton(diceIcons[array[4]-1]);
final JButton dice = new JButton ("Roll Dice");

dice.setBounds(40, 40, 100, 30);
panel.add(dice);
panel.setLayout(null);

dice.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
roll1.setBounds(40, 100, 70, 70);
roll2.setBounds(40, 180, 70, 70);
roll3.setBounds(40, 260, 70, 70);
roll4.setBounds(40, 340, 70, 70);
roll5.setBounds(40, 420, 70, 70);

//Adding to JFrame
panel.add(roll1);
panel.add(roll2);
panel.add(roll3);
panel.add(roll4);
panel.add(roll5);
panel.doLayout();
panel.repaint();
panel.revalidate();
}
});


}

更新

dice.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int array [] = new int [5];

for(int i = 0; i < 5; i++)
{
array [i]= (int) (Math.random () * 6) + 1;
System.out.println(array[i]);
}
final JButton roll1 = new JButton(diceIcons[array[0]-1]);
final JButton roll2 = new JButton(diceIcons[array[1]-1]);
final JButton roll3 = new JButton(diceIcons[array[2]-1]);
final JButton roll4 = new JButton(diceIcons[array[3]-1]);
final JButton roll5 = new JButton(diceIcons[array[4]-1]);


roll1.setBounds(40, 100, 70, 70);
roll2.setBounds(40, 180, 70, 70);
roll3.setBounds(40, 260, 70, 70);
roll4.setBounds(40, 340, 70, 70);
roll5.setBounds(40, 420, 70, 70);

//Adding to JFrame
panel.add(roll1);
panel.add(roll2);
panel.add(roll3);
panel.add(roll4);
panel.add(roll5);
panel.doLayout();
panel.repaint();
panel.revalidate();
}
});

最佳答案

您可以使用 setActionCommand("add"+ "1"); 为每个 JButton 设置一个 actionCommand;//假设 '1' 是 for 循环中的 'i' 值

然后,在 ActionListiner 的实现方法中:

@Override
public void actionPerformed(ActionEvent e) {

String cmd = e.getActionCommand();

if (cmd.equals("add1")){
System.out.println("from add1");
}


}

关于java - 如何执行 JButton Action 监听器一定次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27993933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com