JAVA抽奖系统源代码

发布时间:2019-09-12 19:42:13   来源:文档文库   
字号:

/*主界面

* MainFrame.java

*

* Created on __DATE__, __TIME__

*/

package com.insigma.frame;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URLDecoder;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.UIManager;

import org.jvnet.substance.SubstanceLookAndFeel;

import org.jvnet.substance.painter.StandardGradientPainter;

import org.jvnet.substance.theme.SubstanceTerracottaTheme;

import com.insigma.commutil.CardThread;

import com.insigma.commutil.CommUtil;

import com.insigma.commutil.ImageThread;

/**

*

* @author __USER__

*/

public class MainFrame extends javax.swing.JDialog

{

/** Creates new form MainFrame */

public MainFrame(java.awt.Frame parent, boolean modal)

{

super(parent, modal);

initComponents();

this.setLocationRelativeTo(null);// 居中显示

try

{

UIManager.setLookAndFeel(new SubstanceLookAndFeel());

JFrame.setDefaultLookAndFeelDecorated(true);

JDialog.setDefaultLookAndFeelDecorated(true);

SubstanceLookAndFeel

.setCurrentTheme(new SubstanceTerracottaTheme());

//SubstanceLookAndFeel.setSkin(new EmeraldDuskSkin());

//SubstanceLookAndFeel.setCurrentButtonShaper(new

//ClassicButtonShaper());

//SubstanceLookAndFeel.setCurrentWatermark(new

//SubstanceBubblesWatermark());

//SubstanceLookAndFeel.setCurrentBorderPainter(new

// StandardBorderPainter());

// SubstanceLookAndFeel.setCurrentGradientPainter(new

//StandardGradientPainter());//水晶蓝

// SubstanceLookAndFeel.setCurrentTitlePainter(new

// FlatTitlePainter());

} catch (Exception e)

{

System.err.println("Something went wrong!");

}

try

{

showPerson();

} catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void showPerson() throws IOException

{

String filename = URLDecoder.decode(Class.class.getResource(

"/com/insigma/config/name.txt").getPath());

FileInputStream fileInputStream = new FileInputStream(filename);

InputStreamReader inputStreamReader = new InputStreamReader(

fileInputStream, "gbk");

BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

String name = "";

String bufname = "";

int key = 1;

while ((name = bufferedReader.readLine()) != null)

{

if (bufname.equals(""))

bufname = name;

else

bufname = bufname + "\n" + name;

CommUtil.map.put(key, name);

key++;

}

this.jTextArea4.setText(bufname);

this.jLabel4.setText("准备......!");

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

//GEN-BEGIN:initComponents

//

private void initComponents()

{

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

jScrollPane1 = new javax.swing.JScrollPane();

jTextArea1 = new javax.swing.JTextArea();

jScrollPane2 = new javax.swing.JScrollPane();

jTextArea2 = new javax.swing.JTextArea();

jScrollPane3 = new javax.swing.JScrollPane();

jTextArea3 = new javax.swing.JTextArea();

jLabel4 = new javax.swing.JLabel();

jLabel5 = new javax.swing.JLabel();

jScrollPane4 = new javax.swing.JScrollPane();

jTextArea4 = new javax.swing.JTextArea();

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();

jLabel6 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

setTitle("\u516c\u53f8\u5e74\u7ec8\u62bd\u5956\u7cfb\u7edf");

jLabel1.setText("\u4e00\u7b49\u5956\u83b7\u5f97\u8005\uff1a");

jLabel2.setText("\u4e8c\u7b49\u5956\u83b7\u5f97\u8005\uff1a");

jLabel3.setText("\u4e09\u7b49\u5956\u83b7\u5f97\u8005\uff1a");

jTextArea1.setColumns(20);

jTextArea1.setRows(5);

jScrollPane1.setViewportView(jTextArea1);

jTextArea2.setColumns(20);

jTextArea2.setRows(5);

jScrollPane2.setViewportView(jTextArea2);

jTextArea3.setColumns(20);

jTextArea3.setRows(5);

jScrollPane3.setViewportView(jTextArea3);

jLabel4.setFont(new java.awt.Font("宋体", 1, 18));

jLabel4.setForeground(new java.awt.Color(255, 0, 0));

jLabel4.setBorder(javax.swing.BorderFactory

.createLineBorder(new java.awt.Color(0, 0, 0)));

jLabel5.setText("\u62bd\u5956\u8005\u540d\u5355\uff1a");

jTextArea4.setColumns(20);

jTextArea4.setRows(5);

jScrollPane4.setViewportView(jTextArea4);

jButton1.setText("\u5f00\u59cb");

jButton1.addActionListener(new java.awt.event.ActionListener()

{

public void actionPerformed(java.awt.event.ActionEvent evt)

{

jButton1ActionPerformed(evt);

}

});

jButton2.setText("\u6682\u505c");

jButton2.setEnabled(false);

jButton2.addActionListener(new java.awt.event.ActionListener()

{

public void actionPerformed(java.awt.event.ActionEvent evt)

{

jButton2ActionPerformed(evt);

}

});

jButton3.setText("\u91cd\u7f6e");

jButton3.setEnabled(false);

jButton3.addActionListener(new java.awt.event.ActionListener()

{

public void actionPerformed(java.awt.event.ActionEvent evt)

{

jButton3ActionPerformed(evt);

}

});

jLabel6.setFont(new java.awt.Font("微软雅黑", 1, 36));

jLabel6.setForeground(new java.awt.Color(153, 0, 153));

jLabel6.setText("\u516c\u53f8\u5e74\u7ec8\u62bd\u5956");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(

getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(layout

.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(

layout.createSequentialGroup()

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(

layout.createSequentialGroup()

.addGap(98, 98,

98)

.addComponent(

jButton1)

.addGap(47, 47,

47)

.addComponent(

jButton3)

.addGap(44, 44,

44)

.addComponent(

jButton2)

.addGap(104,

104,

104))

.addGroup(

layout.createSequentialGroup()

.addContainerGap()

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(

layout.createSequentialGroup()

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(

jScrollPane1,

javax.swing.GroupLayout.PREFERRED_SIZE,

142,

javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(

jLabel1))

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED,

14,

Short.MAX_VALUE)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(

jLabel2)

.addComponent(

jScrollPane2,

javax.swing.GroupLayout.PREFERRED_SIZE,

137,

javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(33,

33,

33)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(

jScrollPane3,

javax.swing.GroupLayout.PREFERRED_SIZE,

126,

javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(

jLabel3)))

.addGroup(

layout.createSequentialGroup()

.addGap(108,

108,

108)

.addComponent(

jLabel6))

.addComponent(

jLabel5)

.addGroup(

layout.createSequentialGroup()

.addComponent(

jScrollPane4,

javax.swing.GroupLayout.PREFERRED_SIZE,

134,

javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED,

80,

Short.MAX_VALUE)

.addComponent(

jLabel4,

javax.swing.GroupLayout.PREFERRED_SIZE,

116,

javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(122,

122,

122)))

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)))

.addGap(13, 13, 13)));

layout.setVerticalGroup(layout

.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(

layout.createSequentialGroup()

.addComponent(jLabel6)

.addGap(26, 26, 26)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(

layout.createSequentialGroup()

.addComponent(

jLabel1)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(

jScrollPane1,

javax.swing.GroupLayout.PREFERRED_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE,

javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(

layout.createSequentialGroup()

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(

jLabel3)

.addComponent(

jLabel2))

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(

jScrollPane3,

javax.swing.GroupLayout.PREFERRED_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE,

javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(

jScrollPane2,

javax.swing.GroupLayout.PREFERRED_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE,

javax.swing.GroupLayout.PREFERRED_SIZE))))

.addGap(28, 28, 28)

.addComponent(jLabel5)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(

jLabel4,

javax.swing.GroupLayout.DEFAULT_SIZE,

148, Short.MAX_VALUE)

.addComponent(

jScrollPane4,

javax.swing.GroupLayout.DEFAULT_SIZE,

148, Short.MAX_VALUE))

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jButton1)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(

jButton3)

.addComponent(

jButton2)))

.addContainerGap()));

pack();

}//

//GEN-END:initComponents

/**

* 重置事件

* @param evt

*/

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)

{

this.jButton1.setEnabled(true);

this.jButton3.setEnabled(false);

CommUtil.boo = false;

//重置计数器

CommUtil.firstsum = 0;

CommUtil.secondsum = 0;

CommUtil.thirdsum = 0;

//清空抽奖框

this.jTextArea1.setText("");

this.jTextArea2.setText("");

this.jTextArea3.setText("");

//重置人员

try

{

showPerson();

} catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 暂停事件

* @param evt

*/

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)

{

CommUtil.boo = false;

this.jButton2.setEnabled(false);

this.jButton1.setEnabled(true);

}

/**

* 开始事件

* @param evt

*/

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)

{

CommUtil.boo = true;

this.jLabel4.setText(null);

CardThread cardthread = new CardThread(this);

cardthread.start();

ImageThread imageThread = new ImageThread(this);

imageThread.start();

new Thread(cardthread).start();

new Thread(cardthread).start();

new Thread(cardthread).start();

new Thread(cardthread).start();

this.jButton1.setEnabled(false);

this.jButton2.setEnabled(true);

}

/**

* @param args the command line arguments

*/

public static void main(String args[])

{

java.awt.EventQueue.invokeLater(new Runnable()

{

public void run()

{

MainFrame dialog = new MainFrame(new javax.swing.JFrame(), true);

dialog.addWindowListener(new java.awt.event.WindowAdapter()

{

public void windowClosing(java.awt.event.WindowEvent e)

{

System.exit(0);

}

});

dialog.setVisible(true);

}

});

}

//GEN-BEGIN:variables

// Variables declaration - do not modify

public javax.swing.JButton jButton1;

public javax.swing.JButton jButton2;

public javax.swing.JButton jButton3;

public javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

public javax.swing.JLabel jLabel4;

private javax.swing.JLabel jLabel5;

private javax.swing.JLabel jLabel6;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JScrollPane jScrollPane2;

private javax.swing.JScrollPane jScrollPane3;

private javax.swing.JScrollPane jScrollPane4;

public javax.swing.JTextArea jTextArea1;

public javax.swing.JTextArea jTextArea2;

public javax.swing.JTextArea jTextArea3;

public javax.swing.JTextArea jTextArea4;

// End of variables declaration//GEN-END:variables

}

抽奖功能:

package com.insigma.commutil;

import com.insigma.frame.MainFrame;

public class CardThread extends Thread

{

private MainFrame mainframe;

public CardThread(MainFrame mainframe)

{

this.mainframe = mainframe;

}

public void run()

{

synchronized (mainframe)

{

firstCard();

secondCard();

thirdCard();

}

if (CommUtil.isEnd())

{

mainframe.jButton1.setEnabled(false);

mainframe.jLabel4.setText("抽奖结束!");

mainframe.jButton2.setEnabled(false);

mainframe.jButton3.setEnabled(true);

}

}

/**

* 抽一等奖

*/

public void firstCard()

{

while (CommUtil.boo && CommUtil.firstsum < CommUtil.firstcount)

{

int key = (int) (Math.random() * 60 + 1);

if (CommUtil.getPerson(key))

{

String name = CommUtil.map.get(key);

if (mainframe.jTextArea1.getText() != null

&& !mainframe.jTextArea1.getText().equals(""))

{

mainframe.jTextArea1.setText(mainframe.jTextArea1.getText()

+ "\n" + name);

} else

{

mainframe.jTextArea1.setText(name);

}

CommUtil.deletePerson(key);

String bufname = CommUtil.mapToString();

mainframe.jTextArea4.setText(bufname);

CommUtil.firstsum++;

try

{

sleep(1000);

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

/**

* 抽三等奖

*/

public void thirdCard()

{

while (CommUtil.boo && CommUtil.thirdsum < CommUtil.thirdcount)

{

int key = (int) (Math.random() * 60 + 1);

if (CommUtil.getPerson(key))

{

String name = CommUtil.map.get(key);

if (mainframe.jTextArea3.getText() != null

&& !mainframe.jTextArea3.getText().equals(""))

{

mainframe.jTextArea3.setText(mainframe.jTextArea3.getText()

+ "\n" + name);

} else

{

mainframe.jTextArea3.setText(name);

}

CommUtil.deletePerson(key);

String bufname = CommUtil.mapToString();

mainframe.jTextArea4.setText(bufname);

CommUtil.thirdsum++;

try

{

sleep(1000);

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

/**

* 抽二等奖

*/

public void secondCard()

{

while (CommUtil.boo && CommUtil.secondsum < CommUtil.secondcount)

{

int key = (int) (Math.random() * 60 + 1);

if (CommUtil.getPerson(key))

{

String name = CommUtil.map.get(key);

if (mainframe.jTextArea2.getText() != null

&& !mainframe.jTextArea2.getText().equals(""))

{

mainframe.jTextArea2.setText(mainframe.jTextArea2.getText()

+ "\n" + name);

} else

{

mainframe.jTextArea2.setText(name);

}

CommUtil.deletePerson(key);

String bufname = CommUtil.mapToString();

mainframe.jTextArea4.setText(bufname);

CommUtil.secondsum++;

try

{

sleep(1000);

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

工具类:

package com.insigma.commutil;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

public class CommUtil

{

public static boolean boo = false;

public static Map map = new HashMap<>();

/**

* 奖项获奖人数

*/

public static int firstcount = 1;

public static int secondcount = 3;

public static int thirdcount = 5;

/**

* 计数器

*/

public static int firstsum = 0;

public static int secondsum = 0;

public static int thirdsum = 0;

/**

* 查找人员

*/

public static boolean getPerson(int key)

{

boolean boo = false;

if(map.get(key)!=null && !map.get(key).equals(""))

{

boo = true;

}

return boo;

}

public static boolean isEnd()

{

boolean boo = false;

if(firstcount == firstsum && secondcount==secondsum && thirdcount==thirdsum)

{

boo = true;

}

return boo;

}

/**

* 删除人员

*/

public static boolean deletePerson(int key)

{

boolean boo = false;

map.remove(key);

return boo;

}

/**

* map转换为字符串

*/

public static String mapToString()

{

String bufname = "";

Set> entries = map.entrySet();

for (Map.Entry entry : entries)

{

String name = entry.getValue();

if(bufname.equals(""))

bufname = name;

else

bufname = bufname+"\n"+name;

}

return bufname;

}

}

显示头像:

package com.insigma.commutil;

import com.insigma.frame.MainFrame;

public class ImageThread extends Thread

{

private MainFrame mainframe;

public ImageThread(MainFrame mainframe)

{

this.mainframe = mainframe;

}

public void run()

{

showImage();

if (CommUtil.isEnd())

{

mainframe.jLabel4.setIcon(null);

}

}

public void showImage()

{

while (CommUtil.boo && !CommUtil.isEnd())

{

int key = (int) (Math.random() * 60 + 1);

if (CommUtil.getPerson(key))

{

String name = CommUtil.map.get(key);

if (name != null)

{

name = name.substring(0, name.indexOf(","));

mainframe.jLabel4.setIcon(new javax.swing.ImageIcon(

getClass().getResource(

"/com/insigma/imag/" + name + ".jpg")));

try

{

sleep(1000);

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

}

本文来源:https://www.2haoxitong.net/k/doc/d84696a8366baf1ffc4ffe4733687e21ae45ff4b.html

《JAVA抽奖系统源代码.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式