import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class RaffleDraw {
private static String man;
private static String money;
public static void main(final String[] args) {
JFrame f = new JFrame();
f.setSize(600, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridBagLayout());
JButton raffleBtn = new JButton("開始抽獎");
JLabel lab = new JLabel("人數");
JLabel lab2 = new JLabel("獎金");
final JTextArea screen = new JTextArea("",32,90);
screen.setEditable(false);
screen.setLineWrap(true);
screen.setAutoscrolls(true);
final JTextField text1 = new JTextField("",20);
final JTextField text2 = new JTextField("",20);
JScrollPane scrollPane = new JScrollPane(screen,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
raffleBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
man = text1.getText();
money = text2.getText();
List
StringBuffer sbf = new StringBuffer();
for(int i=0;i
}
if(winnerList.size() > 0){
screen.setText(sbf.toString());
}
}
});
int att[][] = {
{0,0,1,1,1,1,GridBagConstraints.BOTH},
{0,1,1,1,1,1,GridBagConstraints.BOTH},
{1,0,1,1,1,1,GridBagConstraints.BOTH},
{1,1,1,1,1,1,GridBagConstraints.BOTH},
{0,3,2,1,1,1,GridBagConstraints.HORIZONTAL},
{0,4,2,2,1,1,GridBagConstraints.BOTH},
};
add(f,lab,att[0]);
add(f,lab2,att[1]);
add(f,text1,att[2]);
add(f,text2,att[3]);
add(f,raffleBtn,att[4]);
add(f,scrollPane,att[5]);
f.pack();
f.setVisible(true);
}
public static List
int man = 0;
try{
man = Integer.parseInt(mans);
Integer.parseInt(money);
}catch(Exception e){
List
exList.add("人數及獎金請輸入數字");
return exList;
}
String str = "";
List
List
List
try {
FileInputStream fr = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(fr,"Big5"));
boolean isfirst = true;
String title = "";
while((str = br.readLine()) != null){
if(isfirst){
isfirst = false;
title = str;
continue;
}
String[] tempLine = str.split("\\,");
try{
String s = tempLine[3];
already.add(str);
}catch(Exception e){
standbyList.add(str);
}
}
br.close();
fr.close();
Random rand = new Random();
if(standbyList.size() < man){
winnerList.add("名單人數小於抽獎人,請重新輸入抽獎人數 剩餘未中獎人數為:"+standbyList.size());
}else{
int countMan = man;
for(int i = 1; standbyList.size() >= countMan && i<=man;i++){
String alreadyData = standbyList.remove(rand.nextInt(standbyList.size()));
winnerList.add(alreadyData);
already.add(alreadyData+","+money);
countMan--;
};
FileOutputStream fo = new FileOutputStream(path);
if(!isfirst){
fo.write(title.getBytes());
fo.write("\r\n".getBytes());
}
for(int i =0;i
fo.write("\r\n".getBytes());
}
for(int i =0;i
fo.write("\r\n".getBytes());
}
fo.close();
}
return winnerList;
} catch (Exception e) {
e.printStackTrace();
}
return winnerList;
}
private static void add(Container con ,Component com,int att[]){
GridBagConstraints cons = new GridBagConstraints();
cons.gridx = att[0];
cons.gridy = att[1];
cons.gridwidth = att[2];
cons.gridheight = att[3];
cons.weightx = att[4];
cons.weighty = att[5];
cons.fill = att[6];
con.add(com,cons);
}
}
沒有留言:
張貼留言