[color=blue]package lyt; 

import java.awt.*; 

import java.awt.event.*; 


public class MainFrame extends Frame { 


private Button bt_ok,bt_out,bt_delete; 


private TextField[] tf = new TextField[3]; 


public TextArea ta1; 


private Label area_title_label,digit_label,number_count; 


private Choice c1; 


private DataInput di; 



private static final long serialVersionUID = 1L; 


public static void main(String[] args) { 

 new MainFrame().launchFrame(); 

} 


public void launchFrame() { 

 this.setSize(500, 500); 

 this.setLocation(300, 50); 

 this.addWindowListener(new WindowAdapter() { 

 public void windowClosing(WindowEvent e) { 

 setVisible(false); 

 System.exit(0); 

 } 

 }); 


 //将文本域添加到p1当中 

 area_title_label = new Label("下面是随机产生的双色球,最后一位为蓝球"); 

 //area_title_label.setSize(50, 50); 

 ta1 = new TextArea(27,8); 

 Panel p1 = new Panel(); 

 p1.setLayout(new BorderLayout()); 

 p1.add(area_title_label,BorderLayout.NORTH); 

 p1.add(ta1,BorderLayout.SOUTH); 


 //将选择列表和文本框及各个按钮添加到p2当中,其中P2也包含了一个Panel 

 c1 = new Choice(); 

 c1.add(" "); 

 c1.add("第1位"); 

 c1.add("第2位"); 

 c1.add("第3位"); 

 c1.add("第4位"); 

 c1.add("第5位"); 

 c1.add("第6位"); 

 c1.add("第7位"); 

 digit_label = new Label("选择位数:"); 

 //通过循环的方法产生文本输入框 

 for(int i=0; i<tf.length; i++) { 

 tf[i] = new TextField(); 

 tf[i].setColumns(2); 

 } 


 bt_ok = new Button("确定"); 

 //为确定按钮添加事件监听程序! 

 bt_ok.addActionListener(new bt_Action()); 

 bt_delete = new Button("删除"); 

 bt_out = new Button("保存"); 

 Panel p2_1 = new Panel(); 

 p2_1.add(c1); 


 for(int i=0; i<tf.length; i++) { 

 p2_1.add(tf[i]); 

 } 

 //程序左边的面板 

 Panel p2 = new Panel(); 

 p2.add(digit_label); 

 p2.add(p2_1); 

 p2.add(bt_ok); 

 p2.add(bt_delete); 

 p2.add(bt_out); 

 //p2.add(number_count); 


 //将p1,p2添加到主面板当中并用网格进行排列 

 Panel top_p = new Panel(); 

 top_p.setLayout(new GridLayout(1,2)); 

 top_p.add(p1); 

 top_p.add(p2); 

 this.add(top_p); 

 this.setVisible(true); 



 di = new DataInput(); 

} 


public class bt_Action implements ActionListener { 


 public void actionPerformed(ActionEvent e) { 

 if(e.getActionCommand().equals("确定")) { 

 di.getInputInt(c1, tf); //将所选择的下拉列表项,及文本输入框传入到数据产生类当中 

 ta1.append(di.outInt()+ "\n");//将数据产生类所产生的数据追加到文本域当中并换行 

 } 

 } 


} 


} 



package lyt; 

import java.awt.Choice; 

import java.awt.TextArea; 

import java.awt.TextField; 

import java.util.Arrays; 

import java.util.Random; 


public class DataInput { 

private MainFrame mf ; 

Random r = new Random(); 

private int[] number_red; 

private int[] number_blue ; 

private int red_number; 

private int blue_number; 

private int[] containRedNumber; 

private TextArea ta1; 

private Choice c1; 

private TextField[] tf; 

private int tf1_number; 

private int tf2_number; 

private int tf3_number; 

private int[] tf_sum_number; 



//得到蓝色球号码,蓝色球号码的范围为1-16 

public int getBlueNumber() { 

 number_blue = new int[16]; 


 for(int i=0; i<number_blue.length; i++) { 

 number_blue[i] = ++i; 

 } 



 blue_number = r.nextInt(number_blue.length) + 1; //随机得到号码,为防止出现0及不出现16,将结果再加1 


 return blue_number; 


} 


//得到红色球号码,红色球号码的范围为1-32 

public int getRedNumber() { 

 number_red = new int[32]; 


 for(int i=0; i<number_red.length; i++) { 

 number_red[i] = ++i; 

 } 


 return red_number = r.nextInt(number_red.length) + 1;//随机得到号码,为防止出现0及不出现32,将结果再加1 

} 


//从输入的数据当中随机取得一个数字,目的是让输入的三个数据随机出现 

public int getInputNumber() { 

 int i = r.nextInt(tf_sum_number.length); 

 return tf_sum_number[i]; 

} 


//判断产生的数字是否会与前面已产生的数字重复,此处通过将以产生的数字放入到一个数组里,然后再将后面产生的数字与数组中的数字进行对比 

public boolean isContainRedNumber(int red_number) { 


 for(int i=0; i<containRedNumber.length; i++) { 

 if(containRedNumber[i] == red_number) { 

 return true; 

 } 

 } 

 return false; 

} 


//通过调用获得号码的方法来产生数字,并进行重复的判断,将最后产生的号码返回 

public String outInt() { 

 containRedNumber = new int[7]; 

 int number1; 

 int number2; 

 int number3; 

 int number4; 

 int number5; 

 int number6; 

 int number7; 


 //第一个数字不需要进行重复判断,只需将产生的数字放入到用于判断重复的数组当中 

 if(c1.getSelectedItem().equals("第1位")) { //此处的if语句是为了判断是否选择了下拉列表,如果选择了则所选择的下拉列表项中的位数 

 number1 = this.getInputNumber(); //就应该是从所输入的三个数据当中随机产生,而不是从其它的随机方法中产生。以下相同 

 containRedNumber[0] = number1; 

 }else { 

 number1 = this.getRedNumber(); 

 containRedNumber[0] = number1; 

 } 


 //将产生的第二个数字进行重复判断,如果为重复则重新产生一个数字,再进行判断!如果不重复则放入到数组当中,以备后续的数字进行重复判断 

 if(c1.getSelectedItem().equals("第2位")) { 

 number2 = this.getInputNumber(); 

 if(this.isContainRedNumber(number2)) { 

 number2 = this.getRedNumber(); 

 } 

 containRedNumber[1] = number2; 

 }else { 

 number2 = this.getRedNumber(); 

 if(this.isContainRedNumber(number2)) { 

 number2 = this.getRedNumber(); 

 } 

 containRedNumber[1] = number2; 

 } 


 //第3个数字与第二个数字类似 

 if(c1.getSelectedItem().equals("第3位")) { 

 number3 = this.getInputNumber(); 

 if(this.isContainRedNumber(number3)) { 

 number3 = this.getRedNumber(); 

 } 

 containRedNumber[2] = number3; 

 }else { 

 number3 = this.getRedNumber(); 

 if(this.isContainRedNumber(number3)) { 

 number3 = this.getRedNumber(); 

 } 

 containRedNumber[2] = number3; 

 } 


 //第4个数字与第二个数字类似 

 if(c1.getSelectedItem().equals("第4位")) { 

 number4 = this.getInputNumber(); 

 if(this.isContainRedNumber(number4)) { 

 number4 = this.getRedNumber(); 

 } 

 containRedNumber[3] = number4; 

 }else { 

 number4 = this.getRedNumber(); 

 if(this.isContainRedNumber(number4)) { 

 number4 = this.getRedNumber(); 

 } 

 containRedNumber[3] = number4; 

 } 


 //第5个数字与第二个数字类似 

 if(c1.getSelectedItem().equals("第5位")) { 

 number5 = this.getInputNumber(); 

 if(this.isContainRedNumber(number5)) { 

 number5 = this.getRedNumber(); 

 } 

 containRedNumber[4] = number5; 

 }else { 

 number5 = this.getRedNumber(); 

 if(this.isContainRedNumber(number5)) { 

 number5 = this.getRedNumber(); 

 } 

 containRedNumber[4] = number5; 

 } 


 //第6个数字与第二个数字类似 

 if(c1.getSelectedItem().equals("第6位")) { 

 number6 = this.getInputNumber(); 

 if(this.isContainRedNumber(number6)) { 

 number6 = this.getRedNumber(); 

 } 

 containRedNumber[5] = number6; 

 }else { 

 number6 = this.getRedNumber(); 

 if(this.isContainRedNumber(number6)) { 

 number6 = this.getRedNumber(); 

 } 

 containRedNumber[5] = number6; 

 } 


 //第7个数字为蓝球,不需要判断是否重复 

 if(c1.getSelectedItem().equals("第7位")) { 

 number7 = this.getInputNumber(); 

 }else { 

 number7 = this.getBlueNumber(); 

 } 


 //将结果打印输出 

 int[] sum_number = {number1,number2,number3,number4,number5,number6,number7}; 

 String[] sum_string = new String[7]; 

 String s = new String() ; 


 for(int i=0; i<sum_number.length; i++) { 

 if(sum_number[i]<10) { //如果产生的数字小于10,则在前面加上0,将结果转换为字符串存入字符串数组当中 

 String s1 = String.valueOf(sum_number[i]); 

 sum_string[i] = "0" + s1; 

 }else { 

 sum_string[i] ="" + sum_number[i]; 

 } 

 //通过判断位数的不同使用不同的分割符,将结果连成一条字符串如:"1, 2, 3, 4, 5, 6: 7" 

 if(i==6) { 

 s+=sum_string[i]; 

 }else if (i==5) { 

 s+= sum_string[i] + ":"; 

 }else { 

 s+= sum_string[i] + ","; 

 } 


 } 

 return s; 


} 



//从窗口中获得输入的数据,进行处理 

public void getInputInt(Choice c1,TextField[] tf) { 

 this.c1 = c1; 

 this.tf = tf; 

 tf_sum_number = new int[tf.length]; 

 int index = 0; 


 //获得文本框输入的数据 

 for(int i=0; i<tf.length; i++) { 

 //通过判断输入框的内容不等于空来判断是否输入了数据 

 if(tf[i].getText() !=null && !tf[i].getText().equals("")) { 

 tf_sum_number[index] = Integer.parseInt(tf[i].getText()); 

 index++; 

 } 

 } 

} 

}[/color]