Java 编译器无法识别操作侦听器类中的参数

Java 编译器无法识别操作侦听器类中的参数,java,swing,compiler-construction,jpanel,actionlistener,Java,Swing,Compiler Construction,Jpanel,Actionlistener,我试图建立一个JPanel和clear按钮的矩阵,当你按下屏幕时,面板在特定的地方用黑色表示,当你按下clear时,所有的东西都变成了白色。我不知道为什么,但在我的action listener类中,它无法从Painters类中识别参数和对象 Cell.java Matrix.java (在Painter$MyActionListener类中)-当您在ActionListener类中使用this时,this指的是ActionListener类(它没有这些变量),而不是Painter类变量。(行、

我试图建立一个JPanel和clear按钮的矩阵,当你按下屏幕时,面板在特定的地方用黑色表示,当你按下clear时,所有的东西都变成了白色。我不知道为什么,但在我的action listener类中,它无法从Painters类中识别参数和对象

Cell.java Matrix.java
  • (在Painter$MyActionListener类中)-当您在
    ActionListener
    类中使用
    this
    时,
    this
    指的是
    ActionListener
    类(它没有这些变量),而不是
    Painter
    类变量。(行、列、矩阵)。因此,请去掉您在
    ActionListener
    类中使用的所有
    this
    ,或者使用
    Painter.this.variable

  • (在画师课程中)矩阵矩阵需要是一个
    [[]]
    ,因为您试图使用
    矩阵[i][j]
    。我看到Matrix类有一个2d单元格数组。您可能需要在Matix类中使用一个
    getMatrix()
    方法返回二维单元格数组。使用
    Cell[][]martrix
    matrix m
    代替
    Marix矩阵
    ,然后执行
    matrix=m.getMatrix()
    。或者达到那种程度

  • 您可以访问此的指定类

    尝试执行此操作, “this”是指您正在使用“this”关键字的类的调用实例。您可以直接在内部类中访问外部类的数据成员。不能在循环的
    条件部分中使用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);
                        }
                    }
                }
            }
        }
    }
    
    私有类MyActionListener实现ActionListener{
    已执行的公共无效操作(操作事件e){
    如果(如getSource()==btn){
    对于(int i=0;i对于(int j=0;j这仍然会导致编译错误。
    Matrix Matrix
    不是2D数组;-)@peeskillet您是对的。我刚刚指出了通过此访问主类的主要思想,这仍然会导致编译错误。
    Matrix
    类没有
    getCell()
    method;-)很抱歉,我创建了一个。我正在更新答案。@peeskillet谢谢you@nIcE奶牛:我想接受你的编辑。我该怎么做呢?@Varun:你不必这么做,我的,就够了,就这么做了:-)一旦你获得了
    2000
    的声誉,你也可以这么做。也发布了链接,让你看看。你是最受欢迎并保持微笑:-)
    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();
                }
            }
        }
    }
    
    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);
                            }
                        }
                    }
                }
            }
        }
    }
    
    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);
        }
    }
    
    Painter.this.matric[i][j]
    
    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);
                        }
                    }
                }
            }
        }
    }
    
    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];
        }
    }