1
代碼應該基本上爲1到45之間的數字指定唯一的顏色,並且每次繪製特定數字時,分配給該特定數字的顏色應該是文本框的背景。問題是我無法給一個數字分配一個顏色。顏色分類java
該代碼******************************************** *******************
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class Lottery extends Frame implements ActionListener{
private TextField[] t = new TextField[100];
private Button b;
public Lottery(){
for(int i=0;i<7;i++){
this.t[i] = new TextField(5);
t[i].setEditable(false);
}
b = new Button("Draw Numbers");
b.addActionListener(this);
this.add(b);
for(int i=0;i<7;i++){
this.add(t[i]);
}
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent windowevent){
System.exit(0);
}
});
this.setLayout(new FlowLayout());
this.setTitle("Lottery Draw");
this.pack();
this.setVisible(true);
}
public static int[] lotteryNumbers(){
int[] numbers = new int[7];
int i = 0;
while(i<7){
int value = 1 + (int)(45*Math.random());
if(isUnique(numbers,value)){
numbers[i] = value;
i++;
}
}
return numbers;
}
public static boolean isUnique(int[] array, int value){
for(int i=0; i<array.length; i++)
{
if(array[i] == value) return false;
}
return true;
}
@Override
public void actionPerformed(ActionEvent e) {
int[] numbers = lotteryNumbers();
Arrays.sort(numbers);
for(int i=0;i<7;i++)
{
t[i].setText(Integer.toString(numbers[i]));
}
}
public static void main(String[] args) {
System.out.println("Start of Application");
Lottery lot = new Lottery();
System.out.println("End of Application");
}
}
從這段代碼中我不清楚你想要做什麼。也許你需要使用類對象列表,而不是原始數組。 – mcraen
你在哪裏設置背景顏色? – gonzo
它不在那裏,因爲我不知道該怎麼做 – Shane