在 Java 中将 ActionListener() 添加到 JButton-Array:编译错误

我正处于我的编程生涯的开始:)并为自己设定了编写一个简单的国际象棋程序的目标。我还没有实现任何逻辑。


不幸的是,我通过以下代码收到此错误消息: Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem


不过,我仍然可以正常启动程序并获取棋盘图案的字段。仅当我按下按钮时,我才会收到上述错误。


import java.awt.*;

import javax.swing.*;

import java.awt.event.*;


public class Chess extends JFrame {


    //Define variables

    private JButton[][] buttons = new JButton[8][8];

    private Container board;

    private int size = 600;


    // Main class opens constructor of Chess

    public static void main(String[] args) {

        new Chess();

    }


    // constructor

    public Chess() {


        //initialize the Chessboard

        board = getContentPane();

        board.setLayout(new GridLayout(8, 8));

        setSize(size, size);

        setVisible(true);


        //Add buttons to the frame

        for (int y = 0; y < 8; y++) {

            for (int x = 0; x < 8; x++) {

                buttons[y][x] = new JButton();

                board.add(buttons[y][x]);

                buttons[y][x].setBorderPainted(false);


                //color buttons in the checkerboard pattern

                if ((y + x) % 2 != 0) {

                    buttons[y][x].setBackground(new Color(201, 166, 113));

                } else {

                    buttons[y][x].setBackground(Color.WHITE);

                }


                //Add event listener

                ActionListener buttonListener = new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                        pressedButton(y,x);

                    }

                };

                buttons[y][x].addActionListener(buttonListener);


            }

        }


    }


    public void pressedButton(int y, int x) {

        System.out.println(x + " " + y);

    }

}


繁星coding
浏览 92回答 1
1回答

qq_笑_17

我快速设置了一个沙箱,并使用 java 版本 11 和语言级别 11 让它工作。您不能在 ActionListener 中传递x或内部。yfor (int y = 0; y < 8; y++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int x = 0; x < 8; x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tx = x;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int ty = y;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buttons[y][x] = new JButton();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; board.add(buttons[y][x]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buttons[y][x].setBorderPainted(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //color buttons in the checkerboard pattern&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((y + x) % 2 != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buttons[y][x].setBackground(new Color(201, 166, 113));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buttons[y][x].setBackground(Color.WHITE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Add event listener&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ActionListener buttonListener = new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pressedButton(ty, tx);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buttons[y][x].addActionListener(buttonListener);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }请注意添加的tx和ty使其起作用。问候
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java