模拟抽奖系统
今天天气晴,心情挺好~~~
写了个小程序,模拟抽奖系统,功能待完善~~~~
//这里是界面代码
package luck.liubao;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
*
* @author Administrator
*/
public class LuckFrameTest {
public static void main(String[] args) {
new LuckFrame();
}
}
class LuckFrame extends JFrame {
JTextArea jta;
JScrollPane scroll;
JPanel buttonPanel, textPanel;
JButton startbutton, endbutton;
LuckMothd lm;
String s;
Thread thread;
public LuckFrame() {
this.setVisible(true);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("抽奖系统");
lm=new LuckMothd();
jta = new JTextArea();
scroll = new JScrollPane(jta);
this.add(scroll, BorderLayout.CENTER);
buttonPanel = new JPanel();
startbutton = new JButton("开始");
endbutton = new JButton("停止");
buttonPanel.add(startbutton);
buttonPanel.add(endbutton);
this.add(buttonPanel, BorderLayout.SOUTH);
thread=new Thread(new linestart());
startbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
thread.start();
}
});
endbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lm.stop();
JOptionPane.showMessageDialog(null, "恭喜你! "+s+"! 你中奖了!");
}
});
}
public class linestart implements Runnable{
public linestart() {
}
public void run() {
while(lm.isFlag()){
s=lm.start();
jta.append(s+"\n");
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
JOptionPane.showMessageDialog(null, "错误!");
}
}
}
}
}
//这里是对象方法
package luck.liubao;
import java.util.Random;
import javax.net.ssl.SSLContext;
public class LuckMothd {
private boolean flag=true;
Person[] ss;
public LuckMothd() {
ss = new Person[10];
ss[0] = new Person("张三","15031526715","唐山");
ss[1] = new Person("李四","15031529411","内蒙");
ss[2] = new Person("小明","15031522915","石家庄");
ss[3] = new Person("王五","15031527781","承德");
ss[4] = new Person("力强","15031556891","廊坊");
ss[5] = new Person("小利","15031577561","辽宁");
ss[6] = new Person("老毕","15031529733","大连");
ss[7] = new Person("小崔","15031529415","哈尔滨");
ss[8] = new Person("李阳","15031526213","海南");
ss[9] = new Person("小马","15031529882","广州");
}
public void stop(){
flag=false;
}
public boolean isFlag() {
return flag;
}
public String start(){
Random r=new Random();
int i=r.nextInt(10);
return ss[i].toString();
}
}
class Person{
private String name;
private String phonenumber;
private String address;
public Person(String name, String phonenumber, String address) {
this.name = name;
this.phonenumber = phonenumber;
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}
@Override
public String toString() {
return name + "\t" + phonenumber + "\t" + address ;
}
}
总结:
在刚开始完成的程序,当开始抽奖后,界面将完全卡死,这就让哥郁闷的~疼,想了想,应该是线程问题,看了看书,试了好多次,居然出结果了,可喜的,出的结果和我预想结果极其相似,心情一乐,就发上来了,代码仍在待优化中~~~~~~~~~~~