2016-03-15 85 views
0

我有下面的代碼爲jframe是25個按鈕,我試圖用我的顏色數組隨機使每個按鈕的顏色之一。如何從顏色數組中隨機設置jbutton顏色?

import java.awt.event.*; // Needed for ActionListener and ActionEvent 
import javax.swing.*; // Needed for JFrame and JButton 
import java.awt.Color; 
import java.awt.Graphics; 

public class ColorToggleGui extends JFrame implements ActionListener { 

    // This stores all buttons 
    JButton[][] buttons; 
    //Stores colors 
    Color[] colors; 

    public ColorToggleGui(String title) { 
    super(title); 
    setLayout(null); 

    //Allocate the size of the array 
    colors = new Color[4]; 

     //Initialize the values of the array 
    colors[0] = Color.red; 
    colors[1] = Color.blue; 
    colors[2] = Color.yellow; 
    colors[3] = Color.green; 


    buttons = new JButton[5][5]; 
    String[] buttonLabels = { "", "", "", "", "", "", "", "", "", "", "","","","","","","","","","","","","","","" }; 
    for(int row=0; row<5; row++) { 
     for (int col=0; col<5; col++) { 
     buttons[row][col] = new JButton(buttonLabels[row*3+col]); 
     buttons[row][col].setLocation(10+col*55, 10+row*55); 
     buttons[row][col].setSize(50,50); 
     buttons[row][col].addActionListener(this); 
     add(buttons[row][col]); 
     } 
    } 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setSize(300,450); 
    } 


    // This is the single event handler for all the buttons 
    public void actionPerformed(ActionEvent e) { 
    System.out.println("Button " + e.getActionCommand() + " was pressed."); 
    } 

    public static void main(String args[]) { 
    ColorToggleGui frame = new ColorToggleGui("Julian's Colour Toggle"); 
    frame.setVisible(true); 
    } 

} 

我怎麼會隨機使每個按鈕從我製作的顏色數組中獲取顏色?

+0

可能重複的[在特定範圍內生成隨機整數](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range) –

+0

是什麼問題?生成隨機數字?或... –

+0

'setLayout(null);'...'GridLayout'或'GridBagLayout'可以實現您想要的輸出,但問題要少得多 – MadProgrammer

回答

0

你必須爲0和3之間產生隨機數,所以你可以映射那些紅色,藍色,黃色和綠色之間

你的構造可以看起來像:

public ColorToggleGui(String title) { 
     super(title); 
     setLayout(null); 

     // Allocate the size of the array 
     colors = new Color[4]; 

     // Initialize the values of the array 
     colors[0] = Color.red; 
     colors[1] = Color.blue; 
     colors[2] = Color.yellow; 
     colors[3] = Color.green; 


     buttons = new JButton[5][5]; 
     final String[] buttonLabels = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
       "" }; 

     // Here random color logic 

     final Random r = new Random(); 
     int rInt = r.nextInt(4); 
     for (int row = 0; row < 5; row++) { 
      for (int col = 0; col < 5; col++) { 
       rInt = r.nextInt(4); // here generate the random integer 
       System.out.println(rInt); 
       buttons[row][col] = new JButton(buttonLabels[row * 3 + col]); 
       buttons[row][col].setLocation(10 + col * 55, 10 + row * 55); 
       buttons[row][col].setSize(50, 50); 
       buttons[row][col].addActionListener(this); 
       buttons[row][col].setBackground(colors[rInt]); // here set the background color 
       add(buttons[row][col]); 
      } 
     } 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setSize(300, 450); 
    } 

的其餘部分是同...

輸出可以是:

enter image description here