Java 如何执行JButton';s动作侦听器一定次数
我有一些代码,其中我有5个JButtons的代码,我有一个单独的数组,生成1-6之间的随机数(骰子滚动),在动作侦听器中,我只有一个将按钮添加到面板的部分(panel.add(roll1))在动作监听器之前,我有一个生成随机数的数组,然后我有一个开关,说如果随机数是1,那么将一个图像设置为dice1,如果数组是2,将图像设置为dice2。所以我现在所有这些都在工作,因为我正在制作yahtzee游戏,我需要知道如何使它在每次移动最多3次的情况下运行。现在JButton只被点击一次,它输出其他带有随机骰子图像的JButton,但是当我再次点击滚动按钮时,骰子不会滚动,它保持不变。你会怎么做Java 如何执行JButton';s动作侦听器一定次数,java,arrays,swing,random,actionlistener,Java,Arrays,Swing,Random,Actionlistener,我有一些代码,其中我有5个JButtons的代码,我有一个单独的数组,生成1-6之间的随机数(骰子滚动),在动作侦听器中,我只有一个将按钮添加到面板的部分(panel.add(roll1))在动作监听器之前,我有一个生成随机数的数组,然后我有一个开关,说如果随机数是1,那么将一个图像设置为dice1,如果数组是2,将图像设置为dice2。所以我现在所有这些都在工作,因为我正在制作yahtzee游戏,我需要知道如何使它在每次移动最多3次的情况下运行。现在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();
}
});
}
publicstaticvoidrandomroll(最终JPanel面板)引发异常
{
最终图像图标=新图像图标(新URL(“http://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Dice-1.png/45px-Dice-1.png"));
最终图像图标icon1=新图像图标(新URL(“http://upload.wikimedia.org/wikipedia/commons/thumb/1/18/Dice-2.png/45px-Dice-2.png"));
最终图像图标icon2=新图像图标(新URL(“http://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Dice-3.png/45px-Dice-3.png"));
最终图像图标图标3=新图像图标(新URL(“http://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Dice-4.png/45px-Dice-4.png"));
最终图像图标图标4=新图像图标(新URL(“http://upload.wikimedia.org/wikipedia/commons/thumb/6/6c/Dice-5.png/45px-Dice-5.png"));
最终图像图标图标5=新图像图标(新URL(“http://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Dice-6.png/45px-Dice-6.png"));
最终图像图标[]图标={icon,icon1,icon2,icon3,icon4,icon5};
int数组[]=新的int[5];
对于(int i=0;i<5;i++)
{
数组[i]=(int)(Math.random()*6)+1;
System.out.println(数组[i]);
}
最终JButton roll1=新JButton(diceIcons[array[0]-1]);
最终JButton roll2=新JButton(diceIcons[array[1]-1]);
最终JButton roll3=新JButton(diceIcons[array[2]-1]);
最终JButton roll4=新JButton(diceIcons[array[3]-1]);
最终JButton roll5=新JButton(diceIcons[array[4]-1]);
最终扣子骰子=新扣子(“掷骰子”);
骰子。后退(40,40,100,30);
面板。添加(骰子);
panel.setLayout(空);
dice.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
滚动1.立根(40,100,70,70);
滚动2.立根(40、180、70、70);
滚动3.后退(40、260、70、70);
滚动4.立根(40,340,70,70);
滚动5.立根(40、420、70、70);
//添加到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();
}
});
dice.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
int数组[]=新的int[5];
对于(int i=0;i<5;i++)
{
数组[i]=(int)(Math.random()*6)+1;
System.out.println(数组[i]);
}
最终JButton roll1=新JButton(diceIcons[array[0]-1]);
最终JButton roll2=新JButton(diceIcons[array[1]-1]);
最终JButton roll3=新JButton(diceIcons[array[2]-1]);
最终JButton roll4=新JButton(diceIcons[array[3]-1]);
最终JButton roll5=新JButton(diceIcons[array[4]-1]);
滚动1.立根(40,100,70,70);
滚动2.立根(40、180、70、70);
滚动3.后退(40、260、70、70);
滚动4.立根(40,340,70,70);
滚动5.立根(40、420、70、70);
//添加到JFrame
panel.add(roll1);
panel.add(roll2);
panel.add(roll3);
panel.add(roll4);
panel.add(roll5);
panel.doLayout();
panel.repaint();
panel.revalidate();
}
});
您可以使用setActionCommand(“添加“+”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");
}
}
您可以使用setActionCommand(“添加”+“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");
}
}
您可以使用setActionCommand(“添加”+“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");
}
}
您可以使用setActionCommand(“添加”+“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");
}
}
你的随机化代码
int array [] = new int [5];
for(int i = 0; i < 5; i++)
{
array [i]= (int) (Math.random () * 6) + 1;
System.out.println(array[i]);
}
使用JLabel和布局管理器的示例:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class DiceRoller extends JPanel {
private static final String BASE_PATH = "http://upload.wikimedia.org/wikipedia/commons/thumb/";
private static final String[] DICE_PATH = {
"c/c5/Dice-1.png/45px-Dice-1.png",
"1/18/Dice-2.png/45px-Dice-2.png",
"7/70/Dice-3.png/45px-Dice-3.png",
"a/a9/Dice-4.png/45px-Dice-4.png",
"6/6c/Dice-5.png/45px-Dice-5.png",
"5/5c/Dice-6.png/45px-Dice-6.png"
};
private static final int BUTTON_PRESS_TOTAL_COUNT = 3;
private static final int BUTTON_COUNT = 5;
private List<Icon> dieImageList = new ArrayList<>();
private Icon emptyIcon;
private JLabel[] dieLabels = new JLabel[BUTTON_COUNT];
public DiceRoller() throws IOException {
for (String dicePath : DICE_PATH) {
String path = BASE_PATH + dicePath;
URL url = new URL(path);
BufferedImage img = ImageIO.read(url);
dieImageList.add(new ImageIcon(img));
}
int biWidth = dieImageList.get(0).getIconWidth();
int biHeight = dieImageList.get(0).getIconHeight();
BufferedImage emptyImage = new BufferedImage(biWidth, biHeight,
BufferedImage.TYPE_INT_ARGB);
emptyIcon = new ImageIcon(emptyImage);
int gap = 8;
JPanel btnPanel = new JPanel(new GridLayout(0, 1, 0, gap));
btnPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
for (int i = 0; i < dieLabels.length; i++) {
dieLabels[i] = new JLabel(emptyIcon);
btnPanel.add(dieLabels[i]);
}
JPanel topPanel = new JPanel();
topPanel.add(new JButton(new ButtonAction("Roll")));
setLayout(new BorderLayout());
add(topPanel, BorderLayout.PAGE_START);
add(btnPanel, BorderLayout.CENTER);
}
private class ButtonAction extends AbstractAction {
private Random random = new Random();
private int buttonPressCount = 0;
public ButtonAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
if (buttonPressCount >= BUTTON_PRESS_TOTAL_COUNT) {
return; // we've exceeded our max
}
for (JLabel jLabel : dieLabels) {
int index = random.nextInt(dieImageList.size());
jLabel.setIcon(dieImageList.get(index));
}
buttonPressCount++;
if (buttonPressCount >= BUTTON_PRESS_TOTAL_COUNT) {
setEnabled(false);
}
}
}
private static void createAndShowGui() {
DiceRoller roller = null;
try {
roller = new DiceRoller();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
JFrame frame = new JFrame("Roll");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(roller);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
导入java.awt.BorderLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.image.buffereImage;
导入java.io.IOException;
导入java.net.URL;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
导入javax.imageio.imageio;
导入javax.swing.*;
@抑制警告(“串行”)
公共类dicejpanel{
私有静态最终字符串基本路径=”http://upload.wikimedia.org/wikipedia/commons/thumb/";
私有静态最终字符串[]骰子路径={
“c/c5/Dice-1.png/45px-Dice-1.png”,
“1/18/Dice-2.png/45px-Dice-2.png”,
“7/70/Dice-3.png/45px-Dice-3.png”,
“a/a9/Dice-4.png/45px-Dice-4.png”,
“6/6c/Dice-5.png/45px-Dice-5.png”,
“5/5c/Dice-6.png/45px-Dice-6.png”
};
专用静态最终整数按钮\u按下\u总数\u计数=3;
私有静态最终整数按钮计数=5;
私有列表dieMageList=newArrayList();
私人图标emptyIcon;
私有JLabel[]dieLabels=新JLabel[按钮计数];
公共掷骰子者()抛出IOException