public class Lottery extendsJFrame {private JTextField infoField; //抽奖号码确认文本框
private NumField[] numFields; //随机号码文本框数组
private RandomNum randomThread=newRandomNum();public static voidmain(String args[]) {
EventQueue.invokeLater(newRunnable() {public voidrun() {try{
Lottery frame= newLottery();
frame.setVisible(true);
}catch(Exception e) {
e.printStackTrace();
}
}
});
}//创建窗体界面的构造方法
publicLottery() {super();final BorderLayout borderLayout_1 = newBorderLayout();
borderLayout_1.setVgap(10);
getContentPane().setLayout(borderLayout_1);//设置布局管理器
setBounds(100, 100, 420, 256);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JPanel contentPanel = new JPanel(); //创建中间的内容面板
final BorderLayout borderLayout = newBorderLayout();
borderLayout.setVgap(10);
borderLayout.setHgap(10);
contentPanel.setLayout(borderLayout);//设置内容面板布局管理器
getContentPane().add(contentPanel); //添加内容面板到窗体
final JPanel numPanel = new JPanel(); //创建显示随机数的面板
contentPanel.add(numPanel); //添加随机数面板到内容面板
final GridLayout gridLayout = new GridLayout(1, 0);
gridLayout.setHgap(10);
numPanel.setLayout(gridLayout);//设置随机数面板布局管理器
numFields = new NumField[5]; //创建随机数文本框数组
for(int i=0;i
numFields[i]=new NumField(); //初始化数组元素
numPanel.add(numFields[i]); //添加文本框到随机数面板
}final JPanel infoPanel = new JPanel(); //创建显示抽奖号码的面板
infoPanel.setLayout(new BorderLayout()); //设置面板布局管理器
contentPanel.add(infoPanel, BorderLayout.SOUTH); //添加面板到窗体
final JLabel label_1 = new JLabel(); //布局抽奖号码面板
label_1.setFont(new Font("", Font.BOLD, 20));
label_1.setText("随机抽奖的中将号码是:");
infoPanel.add(label_1, BorderLayout.WEST);
infoField= newJTextField();
infoPanel.add(infoField);final JLabel logoLabel = new JLabel(); //布局LOGO标签
logoLabel.setFont(new Font("隶书", Font.PLAIN, 72));
logoLabel.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().add(logoLabel, BorderLayout.NORTH);
logoLabel.setText("随机抽奖");final JPanel controlPanel = new JPanel(); //创建控制按钮面板
final FlowLayout flowLayout = newFlowLayout();
flowLayout.setHgap(25);
controlPanel.setLayout(flowLayout);//设置面板布局
getContentPane().add(controlPanel, BorderLayout.SOUTH); //添加面板到窗体底部
final JButton startButton = new JButton(); //创建开始按钮
startButton.addActionListener(new ActionListener() { //添加事件监听器
public void actionPerformed(finalActionEvent e) {
do_startButton_actionPerformed(e);
}
});
startButton.setText("开始");
controlPanel.add(startButton);//添加按钮到面板
final JButton lotteryButton = new JButton(); //创建抽奖按钮
lotteryButton.addActionListener(new ActionListener() { //添加事件监听器
public void actionPerformed(finalActionEvent e) {
do_lotteryButton_actionPerformed(e);
}
});
lotteryButton.setText("抽奖");
controlPanel.add(lotteryButton);final JButton exitButton = new JButton(); //创建退出按钮
exitButton.addActionListener(new ActionListener() { //添加事件监听器
public void actionPerformed(finalActionEvent e) {
do_exitButton_actionPerformed(e);
}
});
exitButton.setText("退出");
controlPanel.add(exitButton);
}//生成随机数字的内部线程类
class RandomNum extendsThread {private boolean stop=false; //线程状态变量
public voidrun() {while (!stop) {for (finalNumField nf : numFields) {try{
Thread.sleep(1);
}catch(InterruptedException e) {
e.printStackTrace();
}final int num = (int) (Math.random() * 10); //生成随机数
EventQueue.invokeLater(newRunnable() {public voidrun() {
nf.setText(num+ "");
}
});
}
}
}//停止线程的方法
public voidstopLottery() {this.stop = true;
}
}//开始按钮的事件处理方法
protected void do_startButton_actionPerformed(finalActionEvent e) {if(randomThread!=null) //如果存在上一个线程对象
randomThread.stopLottery(); //停止它
randomThread=new RandomNum(); //创建新的线程对象
randomThread.start(); //启动线程
}//抽奖按钮的事件处理方法
protected void do_lotteryButton_actionPerformed(finalActionEvent e) {if(randomThread!=null) //如果存在线程对象
randomThread.stopLottery(); //停止它
try{
randomThread.join();//等抽奖线程结束
} catch(InterruptedException e1) {
e1.printStackTrace();
}
EventQueue.invokeLater(new Runnable() { //在事件队列中更新抽奖信息
public voidrun() {
String code= ""; //抽奖代码字符串
for (final NumField nf : numFields) { //遍历数字文本框
code += nf.getText(); //连接5个数字字符
}
infoField.setText(code);//更新抽奖信息文本框
}
});
}//退出按钮的事件处理方法
protected void do_exitButton_actionPerformed(finalActionEvent e) {
System.exit(0); //退出程序
}
}