第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Java 中將 ActionListener() 添加到 JButton-Array:編譯錯誤

在 Java 中將 ActionListener() 添加到 JButton-Array:編譯錯誤

繁星coding 2023-08-09 15:11:01
我正處于我的編程生涯的開始:)并為自己設定了編寫一個簡單的國際象棋程序的目標。我還沒有實現(xiàn)任何邏輯。不幸的是,我通過以下代碼收到此錯誤消息: 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);    }}
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

我快速設置了一個沙箱,并使用 java 版本 11 和語言級別 11 讓它工作。


您不能在 ActionListener 中傳遞x或內部。y


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

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

                int tx = x;

                int ty = y;

                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(ty, tx);

                    }

                };

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


            }

        }

請注意添加的tx和ty使其起作用。問候


查看完整回答
反對 回復 2023-08-09
  • 1 回答
  • 0 關注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號