如何执行JButton的动作侦听器一定次数 [英] How to perform a JButton's action listener a certain number of times

查看:126
本文介绍了如何执行JButton的动作侦听器一定次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code在那里我有code。与5 Jbutton中,我有一个生成1-6(掷骰子)之间的随机数一个单独的阵列和动作监听器里我刚才的一部分增加我的按钮,我的面板(panel.add(roll1))和动作侦听器之前,我有一个产生随机数的数组,然后我有一个开关说,如果随机数是1,那么设置图像dice1如果数组2集图像dice2。所以,我有这一切的工作,因为现在我正在做的Yahtzee的比赛,我需要知道如何让以每移动3次,最大可运行此。眼下的JButton只点击一次,它输出等Jbutton将随机骰子图像,但是当我点击滚动按钮按钮重新骰子滚不它保持不变。你会怎样做呢?

 公共静态无效randomRoll(最终的JPanel面板)抛出异常
  {    最终的ImageIcon图标=新的ImageIcon(新URL(http://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Dice-1.png/45px-Dice-1.png));
    最终的ImageIcon ICON1 =新的ImageIcon(新URL(http://upload.wikimedia.org/wikipedia/commons/thumb/1/18/Dice-2.png/45px-Dice-2.png));
    最终的ImageIcon图标2 =新的ImageIcon(新URL(http://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Dice-3.png/45px-Dice-3.png));
    最终的ImageIcon ICON3 =新的ImageIcon(新URL(http://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Dice-4.png/45px-Dice-4.png));
    最终的ImageIcon图标4 =新的ImageIcon(新URL(http://upload.wikimedia.org/wikipedia/commons/thumb/6/6c/Dice-5.png/45px-Dice-5.png));
    最终的ImageIcon icon5 =新的ImageIcon(新URL(http://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Dice-6.png/45px-Dice-6.png));    最终的ImageIcon [] = diceIcons {图标,ICON1,图标2,ICON3,图标4,icon5};    int数组[] =新INT [5];
    的for(int i = 0;我小于5;我++)
    {
      数组[我] =(INT)(的Math.random()* 6)+ 1;
      的System.out.println(数组[我]);
    }    最后一个JButton roll1 =的新的JButton(diceIcons [数组[0] -1]);
    最后一个JButton roll2 =的新的JButton(diceIcons [阵列[1] -1]);
    最后一个JButton roll3 =的新的JButton(diceIcons [数组[2] -1]);
    最后一个JButton roll4 =的新的JButton(diceIcons [阵列[3] -1]);
    最后一个JButton roll5 =的新的JButton(diceIcons [数组[4] -1]);
    最后一个JButton骰子=的新的JButton(掷骰子);    dice.setBounds(40,40,100,30);
    panel.add(骰子);
    panel.setLayout(NULL);    dice.addActionListener(新的ActionListener()
    {
      公共无效的actionPerformed(ActionEvent的五)
      {
        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);        //添加到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()
{
  公共无效的actionPerformed(ActionEvent的五)
  {
    int数组[] =新INT [5];    的for(int i = 0;我小于5;我++)
    {
      数组[我] =(INT)(的Math.random()* 6)+ 1;
      的System.out.println(数组[我]);
    }
最后一个JButton roll1 =的新的JButton(diceIcons [数组[0] -1]);
最后一个JButton roll2 =的新的JButton(diceIcons [阵列[1] -1]);
最后一个JButton roll3 =的新的JButton(diceIcons [数组[2] -1]);
最后一个JButton roll4 =的新的JButton(diceIcons [阵列[3] -1]);
最后一个JButton roll5 =的新的JButton(diceIcons [数组[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);    //添加到JFrame中
    panel.add(roll1);
    panel.add(roll2);
    panel.add(roll3);
    panel.add(roll4);
    panel.add(roll5);
    panel.doLayout();
    panel.repaint();
    panel.revalidate();
 }
});


解决方案

您可以通过设置每个JButton的一个actionCommand setActionCommand(加+1); / /说,'1'是你的我从价值的循环

然后,在从ActionListiner的实现的方法

  @覆盖
公共无效的actionPerformed(ActionEvent的五){    串CMD = e.getActionCommand();    如果(cmd.equals(ADD1)){
        的System.out.println(从ADD1);
    }
}

I have some code where I have code with 5 JButtons and I have a seperate array that generates a random number between 1-6 (dice roll) and inside the action listener I have just the part that adds my button to my panel (panel.add(roll1)) and before the action listener I have an array which generates the random number and then I have a switch saying if the random number is 1 then set an image to dice1 if array is 2 set image to dice2. So I have all this working now since I'm making the game of yahtzee I need to know how to make this runnable at a max of 3 times per move. Right now the JButton is only clicked once and it outputs other JButtons with random dice images, but when I click the roll button button again the dice does not roll it stays the same. How would you do this?

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


  }

Update

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

解决方案

You can set each JButton an actionCommand using setActionCommand("add" + "1"); //say that '1' is your 'i' value from the for loop

Then, in the implemented method from ActionListiner:

@Override
public void actionPerformed(ActionEvent e) {

    String cmd = e.getActionCommand();

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


}

这篇关于如何执行JButton的动作侦听器一定次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆