本文主要是介绍多线程的简单应用----彩票摇号器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
效果图:
package game;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class YaoHao extends JFrame {
Container c;
JMenuBar mb;
JMenu JFile,JHelp;
JMenuItem mExit,mCopyright;
JPanel pl;
JLabel num[]=new JLabel[6];
JButton begin,end;
boolean flag=true;
private void init(){
c=this.getContentPane();
c.setLayout(new BorderLayout());
mb=new JMenuBar();
JFile=new JMenu("文件(F)");
JHelp=new JMenu("帮助(H)");
mb.add(JFile);mb.add(JHelp);
mExit=new JMenuItem("退出(E)");
mCopyright=new JMenuItem("版权信息");
JFile.add(mExit);JHelp.add(mCopyright);
c.add(mb,BorderLayout.NORTH);
mCopyright.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "本游戏由暗伤无痕出品", "版权声明", JOptionPane.INFORMATION_MESSAGE);
}
});
mExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
YaoHao.this.dispose();
}
});
pl=new JPanel();pl.setLayout(null);
c.add(pl);
for(int i=0;i<6;i++){
num[i]=new JLabel("0");
pl.add(num[i]);
num[i].setBounds((i+2)*25,30,60,30);
}
begin=new JButton("开始");
end=new JButton("结束");
pl.add(begin);pl.add(end);
begin.setBounds(220,30,60,30);
end.setBounds(290,30,60,30);
begin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
flag=true;
Numble n=new Numble();
n.start();
}
});
end.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Numble n=new Numble();
flag=false;
}
});
this.setSize(400,300);
this.setVisible(true);
}
public YaoHao(String title){
super(title);
init();
}
class Numble extends Thread{
Random random=new Random();
public void run(){
while(flag){
for(int i=0;i<6;i++){
num[i].setText(Integer.toString(random.nextInt(10)));
}}
try {
sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
YaoHao yh=new YaoHao("彩票摇号器");
}
}
这篇关于多线程的简单应用----彩票摇号器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!