简体   繁体   English

将mouseListener添加到JPanel数组中

[英]add mouseListener to an array of JPanels

i have an 2D array with JPanels and i want to add a mouseListener in every JPanel in the array so i use 2 for loops to add them ,but i want to pass the variables i used in for loops in every mouseListener but when i try to do that all mouseListener have the same value of the last variables used in the last for loops .so what am i doing wrong ? 我有一个带有JPanels的2D数组,我想在数组中的每个JPanel中添加一个mouseListener,所以我使用2 for循环来添加它们,但我想在每个mouseListener中传递我在for循环中使用的变量但是当我尝试这样做所有mouseListener都具有与上一个for循环中使用的最后一个变量相同的值。所以我做错了什么?

here is my code : 这是我的代码:

 for (i=0 ; i<3; i++) {
    for (k=0; k<3; k++) {
       a[i][k].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e){
                  temp = a[i-1][k];
                  a[i-1][k] = a[i][k];
                  a[i][k] = temp;
                  //some
                  //code here 
            public void mouseClicked (MouseEvent e) {}
            public void mouseReleased(MouseEvent e) 
                {
                    invalidate();
                    revalidate();
                    repaint();
                }
            public void mouseEntered (MouseEvent e) 
                {}
            public void mouseExited  (MouseEvent e) {

                }


            });

        }
    }

I just need to know if there is a way to pass to mouselisteners the variables i,k as arguments to the mouseListener 我只需要知道是否有办法将变量i,k传递给mouselisteners作为mouseListener的参数

You can only pass final local variables and class fields into anonymous methods. 您只能将final局部变量和类字段传递给匿名方法。

I recommend creating a new class that implements MouseAdapter that takes the array and the appropriate indices as arguments in the constructor. 我建议创建一个实现MouseAdapter的新类, MouseAdapter数组和相应的索引作为构造函数中的参数。 Then you can save them as fields in the class and use them when the MouseEvent s are called. 然后,您可以将它们保存为类中的字段,并在调用MouseEvent时使用它们。

If you need to access additional variables that you haven't mentioned here, you can always pass them into this new class's constructor. 如果您需要访问此处未提及的其他变量,您始终可以将它们传递给此新类的构造函数。

Code: 码:

public AppletMouseListener extends MouseAdapter {
  private final JApplet theApplet;
  private final Container[][] a;
  private final int i;
  private final int j;

  public AppletMouseListener(JApplet theApplet, Container[][] a, int i, int k) {
    this.theApplet = theApplet;
    this.a = a;
    this.i = i;
    this.k = k;
  }

  @Override
  public void mousePressed(MouseEvent e) {
    JComponent temp = a[i-1][k];
    a[i-1][k] = a[i][k];
    a[i][k] = temp;
    //some
    //code here 
  }

  @Override
  public void mouseReleased(MouseEvent e) {
    theApplet.invalidate();
    theApplet.revalidate();
    theApplet.repaint();
  }
}

Your code will be clearer if you use a normal (named) class instead of an anonymous one. 如果使用普通(命名)类而不是匿名类,则代码将更清晰。 Then you can pass the relevant things ( a , i , and k ) to the constructor. 然后你可以将相关的东西( aik )传递给构造函数。

Anonymous classes can't have constructors but they can access local variables declared final . 匿名类不能有构造函数,但它们可以访问声明为final局部变量。

for (int ii=0 ; ii<3; ii++) {
    for (int kk=0; kk<3; kk++) {
        final int i = ii;
        final int k = kk;
        a[i][k].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e){
                JPanel temp = a[i-1][k]; // index out of bounds
                a[i-1][k] = a[i][k];
                a[i][k] = temp;
            }
            public void mouseClicked (MouseEvent e) {}
            public void mouseReleased(MouseEvent e)
            {
                invalidate();
                revalidate();
                repaint();
            }
            public void mouseEntered (MouseEvent e) {}
            public void mouseExited  (MouseEvent e) {}
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM