2013-02-28 79 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);