第一步:这个是我们的双色球主类,包括界面设计等!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package  ball;
import  java.awt.BorderLayout;
import  java.awt.Color;
import  java.awt.Font;
import  java.awt.Image;
import  java.awt.Toolkit;
import  java.awt.event.ActionEvent;
import  java.awt.event.ActionListener;
import  java.net.URL;
import  javax.swing.JButton;
import  javax.swing.JFrame;
import  javax.swing.JLabel;
import  javax.swing.JOptionPane;
import  javax.swing.JPanel;
import  javax.swing.JTextField;
/**
  *
  *@author huyongjian Oracle(Compus Solution Group)
  * @Date  2013-7-18
  * @version 2.0
  * @since JDK1.6(建议)
    Copy Right Information    COMPUS SOLUTION GROUP
    IDE:Eclipse
    class:Ball 双色球
  */
public  class  Ball  extends  JFrame  implements  ActionListener{
     JPanel pn1,pn2,pn3; //窗体上有三个面板
     JTextField tf1,tf2; //两个文本框
     JLabel lb1,lb2,lb3; //
     JButton btn1,btn2,btn3; //三个按钮
     boolean  flag =  false ; //定义一个标记
     Ball(){
         pn1 =  new  JPanel();
         pn2 =  new  JPanel();
         pn3 =  new  JPanel();
         tf1 =  new  JTextField( "红色球" , 20 ); //长度20
         tf2 =  new  JTextField( "蓝色球" , 8 ); //长度8
         btn1 =  new  JButton( "开始" );
         btn2 =  new  JButton( "停止" );
         btn3 =  new  JButton( "关于" );
         this .setLayout( new  BorderLayout());
         this .add(pn1,BorderLayout.NORTH); //窗体边框布局,默认情况下是CENTER
         this .add(pn2,BorderLayout.CENTER);
         this .add(pn3,BorderLayout.SOUTH);
         lb2 =  new  JLabel( "红色球从1到33中选择六个球" );
         lb2.setForeground(Color.black);
         lb3 =  new  JLabel( "蓝色球从1到16中选择一个球" );
         lb3.setForeground(Color.black);
//      FlowLayout flow = new FlowLayout();
//      pn1.setLayout(flow);
         Font font1= new  Font( "黑体" ,Font.BOLD, 12 );
         pn1.add(lb2);
         pn1.add(tf1);
         tf1.setForeground(Color.red);
         tf1.setFont(font1);
         pn1.add(lb3);
         pn1.add(tf2);
         tf2.setForeground(Color.blue);
         tf2.setFont(font1);
         Font fnt =  new  Font( "华文隶书" ,Font.BOLD, 20 );
         lb1 =  new  JLabel( "双色球奖池已达到458398235元,祝你好运!!!" );
         lb1.setFont(fnt);
         lb1.setForeground(Color.red);
         pn2.add(lb1);
         pn3.add(btn1);
         pn3.add(btn2);
         pn3.add(btn3);
         btn1.addActionListener( this );
         btn2.addActionListener( this );
         btn3.addActionListener( this );
         this .setSize( 600 , 150 ); //窗体大小
         //显示位置居中
         this .setLocationRelativeTo( null ); //显示位置居中
          //修改java左上角的图标
         URL url =  this .getClass().getResource( "ball.png" );
         Image img = Toolkit.getDefaultToolkit().getImage(url);
         this .setIconImage(img);
         this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //程序关闭时结束javaw.exe的运行
         this .setTitle( "双色球自动抽奖机" );
         this .setVisible( true );
         this .setResizable( false );
         start();
     }
     @Override
     public  void  actionPerformed(ActionEvent e) {
         // TODO Auto-generated method stub
         if (e.getSource() == btn1){
             btn1.setEnabled( false );
             btn2.setEnabled( true );
             flag =  true ;
         }
                                                                                                          
         if (e.getSource() == btn2){
             btn2.setEnabled( false );
             btn1.setEnabled( true );
             flag =  false ;
         }
         if (e.getSource()==btn3){
             JOptionPane.showMessageDialog( null "<html><body>程序参与人员 :<br>技 术 总 监&nbsp; : &nbsp;&nbsp;杨强<br>分 析 设 计&nbsp; :&nbsp;&nbsp;小夜的传说<br>代 码 编 写&nbsp; :&nbsp;&nbsp;小夜的传说<br>E-mail&nbsp; :&nbsp;&nbsp;yongjian.hu@oraclecsg.com<body></html>" );
                                                                                                              
         }
                                                                                                          
                                                                                                          
     }
     public  void  start(){
         String str1 =  "" ;
         String str2 =  "" ;
         while ( true ){
             if (flag){
                 str1 =  "" ;
                 for ( int  i =  0 ; i <  6 ; i++){
                     int  m = ( int )(Math.random() *  32  1 );
                     if  (m <  10 ){
                         str1 = str1 +  " 0"  + m;
                     }
                     else {
                         str1 = str1 +  " "  + m;
                     }
                 }
                 str2 =  "" ;
                 for ( int  i= 0 ;i< 2 ;i++){
                 int  n = ( int )(Math.random() *  16  1 );
                                                                                                                  
                                                                                                                  
                 if (n< 10 ){
                     str2=str2+ " 0" +n;
                 }
                 else {
                     str2=str2+ " " +n;
                 }
                 }
             }
             try {
                 Thread.sleep( 10 );
             }
             catch (InterruptedException e){
                 e.printStackTrace();
             }
             tf1.setText(str1);
             tf2.setText(str2);
         }
     }
}

第二步是:下是我们的主函数类调用我们写的Ball()方法!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package  ball;
/**
  *
  *@author huyongjian Oracle(Compus Solution Group)
  * @Date  2013-7-18
  * @version 2.0
  * @since JDK1.6(建议)
    Copy Right Information    COMPUS SOLUTION GROUP
    IDE:Eclipse
    class:BallTest 测试
  */
public  class  BallTest {
public  static  void  main(String[] args) {
     new  Ball();
}
}

更多Java编写的源码请进群:160243674--Java程序猿联盟!

效果展示:

130632372.png