编译器不识别动作侦听器类中的参数

时间:2014-07-14 11:54:19

标签: java swing compiler-construction jpanel actionlistener

我试图建立JPanels矩阵和清晰按钮,当你按下屏幕时,面板在黑色的特定位置,当你按下清除时,一切都变白了。我不知道为什么但是在我的动作监听器类中它不会识别来自Painters类的参数和对象。

Cell.java

package Paint;

import java.awt.Color;
import javax.swing.*;

public class Cell extends JPanel {

    private JPanel p;

    public Cell() {
        this.p = new JPanel();
        this.setSize(50, 50);
        this.setBackground(Color.white);
    }
}

Matrix.java

package Paint;

import java.awt.GridLayout;
import javax.swing.*;

public class Matrix extends JPanel {

    private Cell[][] matrix;

    public Matrix(int row , int col) {
        this.setLayout(new GridLayout(row, col));
        this.matrix = new Cell[row][col];
        for(int i=0; i< row ; i++) {
            for(int j=0; j<col; j++) {
                this.matrix[i][j] = new Cell();
            }
        }
    }
}

Painter.java

package Paint;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;    
import javax.swing.*;

public class Painter extends JPanel {

    private JButton btn;
    private Matrix matrix ;
    private int row , col;

    public Painter(int row , int col) {
        this.row = row;
        this.col = col;
        this.matrix = new Matrix(row, col);
        this.setLayout(new BorderLayout());
        btn = new JButton("clear");
        this.add(matrix , BorderLayout.CENTER);
        this.add(btn , BorderLayout.SOUTH);
    }

    private class MyActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == btn) {
                for(int i= 0; i< row; i++) {
                    for (int j= 0 ; j < col ; j++) {
                        this.matrix[i][j].setBackground(Color.white);
                    }
                }
            } else {
                for(int i= 0; i< this.row; i++) {
                    for (int j= 0 ;this.col ; j++) {
                        if(e.getSource()== this.matrix[i][j]) {
                            if(this.matrix[i][j].getBackground()== Color.white)
                                this.matrix[i][j].setBackground(Color.black);
                            else
                                this.matrix[i][j].setBackground(Color.white);
                        }
                    }
                }
            }
        }
    }
}

Tester.java

package Paint;

import javax.swing.JFrame;    
import ShayExam.MyPanel;    
import javax.swing.JFrame;

public class Tester {

    public static void main(String[] args) {
        JFrame frame = new JFrame("matrix");
        frame.setSize(750, 750);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        System.out.println("before Painter");
        Painter p = new Painter( 6 , 6);
        frame.add(p);
        frame.setVisible(true);
    }
}

3 个答案:

答案 0 :(得分:3)

  1. (在Painter $ MyActionListener类中) - 当您在this类中使用ActionListener时,this指的是ActionListener类,(没有那些变量)而不是Painter类变量。 (row,col,matrix)。因此,请删除您在this课程中使用的所有ActionListener或使用Painter.this.variable

  2. (在Painter类中) - Matrix matrix需要[][],因为您尝试使用matrix[i][j]。我看到Matrix类有一个2d的Cells数组。您可能希望在Matix类中使用getMatrix()方法返回2d数组Cell。而不是Marix matrix使用Cell[][] martrixMatrix m然后执行matrix = m.getMatrix()。或者那种程度的东西

答案 1 :(得分:3)

Painter.this.matric[i][j]

您可以访问指定this

的类

答案 2 :(得分:3)

尝试这样做, “this”指的是您使用“this”关键字的类的调用实例。您可以直接在内部类中访问外部类的数据成员。您不能在for循环的条件部分中使用col。 actionPerfomed()将不会被调用,因为您必须将ActionListener添加到任何实例。

此代码没有编译错误。

private class MyActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == btn) {
            for(int i= 0; i< row; i++) {
                for (int j= 0 ; j < col ; j++) {
                    matrix.getCell(i,j).setBackground(Color.white);
                }
            }
        } else {
            for(int i= 0; i< row; i++) {
                for (int j= 0 ;j<col ; j++) {
                    if(e.getSource()== matrix.getCell(i,j)) {
                        if(matrix.getCell(i,j).getBackground()== Color.white)
                            matrix.getCell(i,j).setBackground(Color.black);
                        else
                            matrix.getCell(i,j).setBackground(Color.white);
                    }
                }
            }
        }
    }
}

和Matrix类

public class Matrix extends JPanel {

    private Cell[][] matrix;

    public Matrix(int row , int col) {
        this.setLayout(new GridLayout(row, col));
        this.matrix = new Cell[row][col];
        for(int i=0; i< row ; i++) {
            for(int j=0; j<col; j++) {
                this.matrix[i][j] = new Cell();
            }
        }
    }

    public Cell getCell(int i, int j) {
        return matrix[i][j];
    }
}