JButton数组上的ActionListener

4
我创建了一个 JButtons 数组,当创建时自动分配随机颜色,而不是手动创建每个按钮并为其分配随机颜色。现在我想通过单击任何一个按钮来随机更改该按钮的颜色。我希望像目前创建和添加按钮一样使用循环来完成它。
尽管我认为这种方式会失败。我收到“本地变量从内部类中访问;需要声明 final”的错误消息。如果我使用 final,它就不能更改,现在我陷入了困境。
是否有可能的解决方法?
package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.EventHandler;
import java.lang.String;
import java.util.Random;

public class TEST

{

/**
 * @param args the command line arguments
 */
public static Random rand = new Random();
public static int oh;

public void btnPress(ActionEvent e, JButton[] jButts, float r, float g, float b) {
    for (int y = 0; y < jButts.length; y++) {
        if (e.getSource() == jButts[y]) {
            jButts[y].setBackground(Color.getHSBColor(r, g, b));
        }
    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame("Suhp, Brah?");
    frame.setLayout(new BorderLayout());
    frame.setVisible(true);
    frame.setBackground(Color.magenta);
    frame.setSize(400, 400);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(4, 4));
    String[] numbs = {"0", "1", "2", "3", "4", "5", "6", "7"};
    final JButton[] jButts = new JButton[numbs.length];//0-7

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

        jButts[i] = new JButton(numbs[i].toString());
        //String leString = rand.nextInt(255).toString;
        jButts[i].setBackground(Color.getHSBColor(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()));
    }
    for (int x = 0; x < 8; x++) {
        frame.add(jButts[x]);
    }
    //ActionListener
    for (int i =0; i < 8; i++) {

        jButts[i].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                i++;
                jButts[i].setBackground(Color.getHSBColor(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()));
            }
        });
    }

}
}
3个回答

5

ActionListener中没有必要使用i,可以使用ActionEvent#getSource来获取按钮:

jButts[i].addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();
        button.setBackground(Color.getHSBColor(rand.nextFloat(),
                rand.nextFloat(), rand.nextFloat()));
    }
});

或者,您可以将局部变量“i”移动到实例变量中。但这不是一个好的做法。 - Iswanto San
考虑过那个,但我认为这样更简单 :) - Reimeus
这太完美了。真希望我自己能找到它。 - GoodBoyNYC

3

这里有一个解决方法,

//ActionListener
for (int i =0; i < 8; i++) 
{
    final int temp = i; // assign to temporary variable
    jButts[temp].addActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // i++; Not sure what you're trying to do here..
            jButts[temp].setBackground(Color.getHSBColor(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()));
        }
    });
}

但是我强烈建议重新考虑你的方法。


2
// ActionListener
    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton) e.getSource();
            button.setBackground(Color.getHSBColor(rand.nextFloat(),
                    rand.nextFloat(), rand.nextFloat()));
        }
    };

    for (int i = 0; i < 8; i++)
        jButts[i].addActionListener(listener);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接