我试图建立JPanels矩阵和清晰按钮,当你按下屏幕时,面板在黑色的特定位置,当你按下清除时,一切都变白了。我不知道为什么但是在我的动作监听器类中它不会识别来自Painters类的参数和对象。
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);
}
}
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);
}
}
答案 0 :(得分:3)
(在Painter $ MyActionListener类中) - 当您在this
类中使用ActionListener
时,this
指的是ActionListener
类,(没有那些变量)而不是Painter
类变量。 (row,col,matrix)。因此,请删除您在this
课程中使用的所有ActionListener
或使用Painter.this.variable
(在Painter类中) - Matrix matrix
需要[][]
,因为您尝试使用matrix[i][j]
。我看到Matrix类有一个2d的Cells数组。您可能希望在Matix类中使用getMatrix()
方法返回2d数组Cell。而不是Marix matrix
使用Cell[][] martrix
和Matrix 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];
}
}