首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将数组传递到ActionListener,另一个数组触发事件

将数组传递到ActionListener,另一个数组触发事件
EN

Stack Overflow用户
提问于 2015-07-10 04:11:52
回答 2查看 199关注 0票数 1

下面是我的密码。我试图创建一个由6个随机数和30个复选框组成的数组。在动作侦听器中,我想验证用户单击的六个框恰好是我的六个随机数。但是,我不能将随机数数组拉到random侦听器。有人能给我指点吗?我在这方面完全是个新手。int[] rand数组让我感到悲伤。

代码语言:javascript
运行
复制
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class JLottery2 extends JFrame implements ActionListener
{
   int actionCounter = 0;
   int matchTally = 0;

   final int MAXBOXES = 6; 
   final int WIDTH = 500; 
   final int HEIGHT = 200;
   JCheckBox[] boxes = new JCheckBox [30];

   public JLottery2()
   {
      super ("~*~*~ JLOTTERY 2 ~*~*~");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout (new FlowLayout());
      setSize(WIDTH, HEIGHT);

      System.out.println("Constructor");

      //great the check boxes and assign them a value      
      for (int count=0 ; count < 30; count++)
      {
         boxes[count] = new JCheckBox (Integer.toString(count));
         add(boxes[count]);
         boxes[count].addActionListener(this);
      }

      int[] rand = new int[MAXBOXES]; 
      for (int i = 0; i < MAXBOXES; i++)
      {
         rand[i] = (int)(Math.random() * 30);
         //incase it tries to generate the same random number 
         for (int j = 0; j < i; j++)
         {
            if(rand[i] == rand[j])
            {
              i--; 
            }
         }
      }


      setVisible(true);
   }

   public void actionPerformed(ActionEvent e, )
   {      
      System.out.println(rand[5]);

      if(actionCounter < MAXBOXES)
      {
         System.out.println("Action Triggered");
         Object source = e.getActionCommand();
         System.out.println(source);         
         actionCounter++;
         System.out.println(actionCounter);   
      }
      else 
      {
         System.out.println("You reached the max at " + actionCounter);
      }

   } 

   public static void main(String[] args)
   {
      JLottery2 prog = new JLottery2();
   }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-10 04:27:17

使rand成为类实例字段,如boxes

代码语言:javascript
运行
复制
public class JLottery2 extends JFrame implements ActionListener
{
     //...
     JCheckBox[] boxes = new JCheckBox [30];
     int[] rand;

     public JLottery2()
     {
         //...
         rand = new int[MAXBOXES]; 

这将为字段提供一个类级别的上下文,从而允许您从类中的任何位置访问。

票数 1
EN

Stack Overflow用户

发布于 2015-07-10 04:27:46

actionPerformed方法无法访问rand,因为rand是在JLottery2构造函数的本地作用域中声明的。如果rand是在全局范围中声明的,就像在所有方法(如MAXBOXES, WIDTH, HEIGHT等)之外的那样,那么它将是可访问的。请注意,您仍然可以在构造函数中初始化它:

代码语言:javascript
运行
复制
public class JLottery2 extends JFrame implements ActionListener
{
    int actionCounter = 0;
    int matchTally = 0;
    int[] rand;
    ......
    public JLottery2()
    {
        ....
        rand = new int[MADBOXES];

编辑: 下面是是一个很好的链接,供您查看。它用基本的Java代码解释作用域。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31332309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档