Java彩票app
导入Java . awt . dimension;
导入Java . awt . event . action event;
导入Java . awt . event . action listener;
导入Java . awt . event . key event;
导入Java . awt . event . key listener;
导入Java . io . file;
导入Java . io . file output stream;
导入Java . io . io exception;
导入Java . io . output streamwriter;
导入Java . util . random;
导入Java . util . vector;
导入javax . swing . jbutton;
导入javax . swing . jframe;
导入javax . swing . jscrollpane;
导入javax . swing . jtextarea;
导入javax . swing . jtextfield;
公共类Ballot扩展JFrame实现ActionListener,KeyListener{
//窗口构建,成员变量
JTextArea _ txt
JButton _ ballot
JTextField _ print
JScrollPane _ panel
int n _ o _ p;
int索引;
int _ index
String[]名称;
静态向量& lt整数& gtnum =新矢量& lt整数& gt();
静态随机r = new Random();
//
boolean num _ print = true
公共静态void main(String[] args) {
新选票();
}
公众投票()
{
n _ o _ p = 0;
索引= 0;
_ index = 0;
_ txt = new JTextArea();
_txt.setText("请在上面输入人数,回车结束!");
_ txt . set editable(false);
_ panel = new JScrollPane(_ txt);
_ballot=new JButton("抽签");
_ print = new JTextField(10);
this . set layout(new BorderLayout());
this.add(_panel,BorderLayout。中心);
this.add(_print,BorderLayout。北);
this.add(_ballot,BorderLayout。南);
_ print . addkey listener(this);
_ ballot . addactionlistener(this);
this . set visible(true);
this.setSize(新维度(400,200));
this . setdefaultcloseoperation(JFrame。EXIT _ ON _ CLOSE);
}
@覆盖
public void action performed(action event e){
// TODO自动生成的方法存根
if(e.getSource()==_ballot)
{
if(index = = n _ o _ p & amp;& amp_index!=索引)
{
boolean _ b = true
while(_b)
{
int _ I = r . nextint(index);
for(int I = 0;我& ltnum . size();i++)
{
if(_i==num.get(i))
{
num . remove(I);
_ _ txt . append(" \ r \ n r \ n序列号"+_ I+" Name:"+names[_ index]);
_ index++;
_ b =假;
}
}
}
}
if(_index==index)
{
File f = new File(system . getproperty(" user . dir ")+"/save . txt ");
if(f.exists())
{
尝试{
output streamwriter ow = new output streamwriter(new file output stream(f));
ow . write(_ txt . gettext());
ow . close();
} catch (IOException e1) {
// TODO自动生成的catch块
e 1 . printstacktrace();
}
}否则
{
尝试{
f . create new file();
output streamwriter ow = new output streamwriter(new file output stream(f));
ow . write(_ txt . gettext());
ow . close();
} catch (IOException e1) {
// TODO自动生成的catch块
e 1 . printstacktrace();
}最后
{
_txt.setText("信息可以保存在"+f.getPath()+"下!");
}
}
}
}
}
@覆盖
公共void按键(按键事件e) {
// TODO自动生成的方法存根
if(e.getKeyCode()==KeyEvent。VK _回车)
{
if(num_print){
if(_print.getText()。匹配("[0-9]+")
{
num _ print = false
n _ o _ p = integer . parse int(_ print . gettext());
names =新字符串[n _ o _ p];
_ print . settext(" ");
_txt.setText("还需要输入"+n_o_p+"个人姓名!回车完成单名输入!”);
}否则
{
_txt.setText("输入的人数不合法!请重新输入!”);
}
}否则
{
if(_print.getText()!= null & amp& amp!_print.getText()。等于(" " & amp& amp指数!=n_o_p)
{
names[index]= _ print . gettext();
index++;
_txt.setText("还需要输入"+(n_o_p-index)+"个人姓名!回车完成单名输入!”);
}
}
if(index==n_o_p)
{
for(int I = 0;我& lt指数;i++)
{
num . add(I);
}
_txt.setText("请点击按钮抽签!");
}
}
}
@覆盖
public void key released(key event e){
// TODO自动生成的方法存根
}
@覆盖
公共void键入的(KeyEvent e) {
// TODO自动生成的方法存根
}
}