java - 内部类 Action 监听器

标签 java swing multidimensional-array jbutton getter-setter

在代码的不同部分,我将运行类似的变体:

        _grid[4][4].addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            moveBeginningPieceLeft(_square[4][11]);
            refreshBoard();
        }

    });

唯一会改变的是 _grid 的坐标。我之前尝试做一些事情,我制作了一个 Action 监听器,但是 Eclipse 迫使我将整数定为最终的。我想知道是否可以轻松编写一个使用正在调用的网格值的 addListener 方法(_grid 是 JButton 数组,_square 是堆栈数组)。因此,如果我在 _grid[2][5] 上调用 addListener,它只会从 _grid 获取坐标 [2][5] 并将其放置在 Action 监听器的 _square 部分中。所以,

_grid[0][5].addListener();

相同
        _grid[0][5].addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            moveBeginningPieceLeft(_square[0][5]);
            refreshBoard();
        }

    });

最佳答案

I'm wondering if it is easy to write an addListener method that uses the values of the grid that it is being called on(_grid is an array of JButtons and _square is an array of stacks).

  • 不,没有

I tried doing something earlier where I made an actionlistener, but eclipse was forcing me to make the integers final.

  • 将所需的 vasriable 声明为局部变量

  • 有关使用put/getClientProperty的最简单方法的示例,您可以将此方法与另一个附加值结合使用

  • 为了获得更好的帮助,请尽快发布 SSCCE ,短,可运行,可编译,精简且仅关于 JButton 的网格

关于java - 内部类 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15776235/

相关文章:

java - 带有滚动条的 JPanel 设置内容不可调整大小

javascript - 将二维数组中的 ID 与另一个二维数组中的 ID 进行比较

java - bean 类 [Country] : Bean property 'name' is not writable or has an invalid setter method. 的属性 'name' 无效 您的意思是 'cname' 吗?

java - 多行打印数组

java - 无法在 JPanel 上绘制矩形

java - 如何在文本冒险游戏中获取用户输入(Java/Applet)

c - 如何使用 MPI 散布二维数组?

python - 如何返回至少 4D : efficient method to simulate numpy. atleast_4d 的数组

java - 来自不同目录的“预链接”JSP

java - LibGDX 可以在桌面上加载皮肤,但不能在 Android 上加载皮肤