我正在尝试为按钮提供所有自己的动作侦听器,但我不断收到奇怪的错误消息
“无法访问类型为Buttons的封闭实例。必须使用类型为Buttons的封闭实例(例如x.new A(),其中x是Buttons的实例)来限定分配。”
在添加第一个动作侦听器的行上。 任何解决方案或想法都将不胜感激。
这是我的代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Buttons
{
static int numPress = 0;
static int n = 0;
static int fromX;
static int fromY;
static int toX;
static int toY;
static JButton button[] = new JButton[15];
static int[][] mult = new int[][]
{
{0, 1, 1, 1, 1},
{0, 1, 1, 1, 1},
{0, 0, 1, 1, 1},
{0, 0, 0, 1, 1},
{0, 0, 0, 0, 1}
};
public static void main(String []args)
{
pegGame game = new pegGame();
JFrame f = new JFrame("Button");
int row = 5;
int size = 50;
for (int j = 1; j <= row; j++)
{
for (int i = 0; i < j; i++)
{
button[n] = new JButton();
button[n].setBounds((size*row/2 + i*size - j*size/2),size*j - size,size,size);
f.add(button[n]);
numPress++;
n++;
}
}
bttnTxt();
button[0].addActionListener(new btn0());
button[1].addActionListener(new btn1());
button[2].addActionListener(new btn2());
button[3].addActionListener(new btn3());
button[4].addActionListener(new btn4());
button[5].addActionListener(new btn5());
button[6].addActionListener(new btn6());
button[7].addActionListener(new btn7());
button[8].addActionListener(new btn8());
button[9].addActionListener(new btn9());
button[10].addActionListener(new btn10());
button[11].addActionListener(new btn11());
button[12].addActionListener(new btn12());
button[13].addActionListener(new btn13());
button[14].addActionListener(new btn14());
f.setSize(row*size,row*size);
f.setLayout(null);
f.setVisible(true);
}
class btn0 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 0;
fromY = 0;
numPress++;
} else{
toX = 0;
toY = 0;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn1 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 0;
fromY = 1;
numPress++;
} else{
toX = 0;
toY = 1;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn2 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 1;
fromY = 1;
numPress++;
} else{
toX = 1;
toY = 1;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn3 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 0;
fromY = 2;
numPress++;
} else{
toX = 0;
toY = 2;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn4 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 1;
fromY = 2;
numPress++;
} else{
toX = 1;
toY = 2;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn5 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 2;
fromY = 2;
numPress++;
} else{
toX = 2;
toY = 2;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn6 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 0;
fromY = 3;
numPress++;
} else{
toX = 0;
toY = 3;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn7 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 1;
fromY = 3;
numPress++;
} else{
toX = 1;
toY = 3;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn8 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 2;
fromY = 3;
numPress++;
} else{
toX = 2;
toY = 3;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn9 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 3;
fromY = 3;
numPress++;
} else{
toX = 3;
toY = 3;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn10 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 0;
fromY = 4;
numPress++;
} else{
toX = 0;
toY = 4;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn11 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 1;
fromY = 4;
numPress++;
} else{
toX = 1;
toY = 4;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn12 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 2;
fromY = 4;
numPress++;
} else{
toX = 2;
toY = 4;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn13 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 3;
fromY = 4;
numPress++;
} else{
toX = 3;
toY = 4;
moveCheck(fromX, fromY, toX, toY);
}
}
}
class btn14 implements ActionListener
{
public void actionPerformed(ActionEvent x)
{
if(numPress == 0)
{
fromX = 4;
fromY = 4;
numPress++;
} else{
toX = 4;
toY = 4;
moveCheck(fromX, fromY, toX, toY);
}
}
}
public static void moveCheck(int fromX, int fromY, int toX, int toY)
{
if (mult[toX][toY] == 0 && mult[fromX][fromY] == 1)
{
if (fromX + 2 == toX && fromY + 2 == toY && mult[toX][toY] == 0 && mult[fromX + 1][fromY + 1] == 1)
{
mult[fromX][fromY] = 0;
mult[(fromX + 1)][(fromY + 1)] = 0;
mult[toX][toY] = 1;
} else if(fromX - 2 == toX && fromY - 2 == toY && mult[fromX - 1][fromY - 1] == 1)
{
mult[fromX][fromY] = 0;
mult[(fromX - 1)][(fromY - 1)] = 0;
mult[toX][toY] = 1;
} else if(fromX + 2 == toX && fromY == toY && mult[fromX + 1][fromY] == 1)
{
mult[fromX][fromY] = 0;
mult[fromX + 1][fromY] = 0;
mult[toX][toY] = 1;
} else if(fromX - 2 == toX && fromY == toY && mult[fromX - 1][fromY] == 1)
{
mult[fromX][fromY] = 0;
mult[fromX - 1][fromY] = 0;
mult[toX][toY] = 1;
}else if(fromY + 2 == toY && fromX == toX && mult[fromX][fromY + 1] == 1)
{
mult[fromX][fromY] = 0;
mult[fromX][fromY + 1] = 0;
mult[toX][toY] = 1;
}else if(fromY - 2 == toY && fromX == toX && mult[fromX][fromY - 1] == 1)
{
mult[fromX][fromY] = 0;
mult[fromX][fromY - 1] = 0;
mult[toX][toY] = 1;
}else
{
JOptionPane.showMessageDialog(null, "Please pick a legal move");
}
}else
{
JOptionPane.showMessageDialog(null, "Please pick a legal move");
}
bttnTxt();
numPress = 0;
}
public static void bttnTxt()
{
button[0].setText(Integer.toString(mult[0][0]));
button[1].setText(Integer.toString(mult[0][1]));
button[2].setText(Integer.toString(mult[1][1]));
button[3].setText(Integer.toString(mult[0][2]));
button[4].setText(Integer.toString(mult[1][2]));
button[5].setText(Integer.toString(mult[2][2]));
button[6].setText(Integer.toString(mult[0][3]));
button[7].setText(Integer.toString(mult[1][3]));
button[8].setText(Integer.toString(mult[2][3]));
button[9].setText(Integer.toString(mult[3][3]));
button[10].setText(Integer.toString(mult[0][4]));
button[11].setText(Integer.toString(mult[1][4]));
button[12].setText(Integer.toString(mult[2][4]));
button[13].setText(Integer.toString(mult[3][4]));
button[14].setText(Integer.toString(mult[4][4]));
}
public static boolean checkWin()
{
int count = 0;
for(int i = 0; i < mult.length; i++)
{
for(int j = 0 ; j < mult.length; j++)
{
if(mult[i][j] != 0)
{
count ++;
}
}
}
if (count > 1)
{
count = 0;
} else
{
JOptionPane.showMessageDialog(null, "You Win!");
}
return false;
}
}
答案 0 :(得分:0)
您的btn0和其他类是 inner 类,它们不是静态的,因此它们需要button类的外部封闭实例。将它们设为静态,或将初始化代码放入类按钮的实例方法中。