Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试添加一组动作侦听器_Java - Fatal编程技术网

Java 尝试添加一组动作侦听器

Java 尝试添加一组动作侦听器,java,Java,我试着让我的按钮都有自己的动作监听器,但我总是遇到一个奇怪的错误 “无法访问按钮类型的封闭实例。必须使用按钮类型的封闭实例限定分配(例如,x.new A(),其中x是按钮的实例)。” 在我添加第一个动作侦听器的行上。 非常感谢您提出的任何解决方案或想法 这是我的密码 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Buttons

我试着让我的按钮都有自己的动作监听器,但我总是遇到一个奇怪的错误

“无法访问按钮类型的封闭实例。必须使用按钮类型的封闭实例限定分配(例如,x.new A(),其中x是按钮的实例)。”

在我添加第一个动作侦听器的行上。 非常感谢您提出的任何解决方案或想法

这是我的密码

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;
}
}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.*;
公共类按钮
{
静态int numPress=0;
静态int n=0;
静态int fromX;
静态int-fromY;
静态int-toX;
静态智力玩具;
静态JButton按钮[]=新JButton[15];
静态int[]mult=新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}
};
公共静态void main(字符串[]args)
{
pegGame=新pegGame();
JFrame f=新JFrame(“按钮”);
int行=5;
int size=50;
对于(int j=1;j 1)
{
计数=0;
}否则
{
showMessageDialog(null,“您赢了!”);
}
返回false;
}
}

您的btn0和其他类是内部类,不是静态的,因此它们需要buttons类的外部封闭实例。将这些设置为静态或将初始化代码放入类按钮的实例方法中