2013-02-28 91 views
4

我创建了一个JButton数组,它在创建时为它们指定了随机颜色,而不是手动创建每个按钮并为它们分配随机颜色。我现在正处在一个我想要使用的点上随意更改颜色,无论点击哪个按钮。我想按照我创建的方式进行操作,并添加按钮(通过使用循环)。JButtons数组上的ActionListener

虽然这样做我认为会工作的方式失败了。我得到"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())); 
      } 
     }); 
    } 

} 
} 

回答

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

或者,您可以将本地变量'i'到实例variable..but这不是一个好的做法.. – 2013-02-28 00:29:12

+0

没有考虑,但认为这是简单的:) – Reimeus 2013-02-28 00:30:15

+0

这是完美的。希望我自己碰到过这个。 – GoodBoyNYC 2013-02-28 00:42:24

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