簡體   English   中英

JButtons數組上的ActionListener

[英]ActionListener on array of JButtons

我創建了一個JButton數組,在創建時為其分配了隨機顏色,而不是手動創建每個按鈕並為其指定隨機顏色。 我現在正處於一個我想要使用的點,隨機更改任何一個按鈕的顏色。 我想以與我創建的方式相同的方式完成它並添加按鈕到目前為止(通過使用循環)。

雖然按照我認為可行的方式做到但卻失敗了。 我得到"local variable is accessed from within inner class; needs to be declared 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()));
            }
        });
    }

}
}

沒有必要在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()));
    }
});

這是一個解決方法,

//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()));
        }
    });
}

但我強烈建議重新考慮你的方法。

// 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM