用java写抽签,Java抽签小程序

array = new ArrayList();

String line = null;

try {

while ((line = br.readLine()) != null) {

array.add(line);

}

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

br.close();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

// 随机产生一个索引

Random r = new Random();

int index = r.nextInt(array.size());

// 根据该索引获取一个值

String name = array.get(index);

/*

* if(jb ==e.getSource()){ textarea(); }

*/

JOptionPane.showMessageDialog(null, name, "信息消息提示框", JOptionPane.INFORMATION_MESSAGE);

}

});

this.add(jb);

}

/*

* public void textarea(){

*

* jp.setLayout(null);

*

* jta.setBounds(20,20,150,100);

*

* jp.add(jta);

*

* jta.setLineWrap(true);

*

* this.add(jp);

*

* this.setTitle("结果");

*

* this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

*

* this.setResizable(false);

*

* this.setBounds(100, 100, 200, 200);

*

* this.setVisible(true);

*

*

* }

*/

// 主程序

public void start() throws AWTException, Throwable {

// 注册窗口

JFrame jf = new JFrame("注册");

jf.setSize(300, 300);

jf.setLocationRelativeTo(null);

jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

JPanel panel = new JPanel();

jf.setVisible(true);

// 创建文本框

final JTextField text = new JTextField(8);

text.setFont(new Font(null, Font.PLAIN, 20));

Panel panel1 = new Panel();

panel1.add(text);

// 创建按钮

JButton btn1 = new JButton("关闭");

btn1.setFont(new Font(null, Font.PLAIN, 20));

btn1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

jf.dispatchEvent(new WindowEvent(jf, WindowEvent.WINDOW_CLOSING));

}

});

panel1.add(btn1);

JButton btn = new JButton("提交");

btn.setFont(new Font(null, Font.PLAIN, 20));

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// if (btn == e.getSource()) {

// 文件写入

/**

* File file = new File("Name.txt"); if (file.exists() != true)

* { try { file.createNewFile(); } catch (Exception e) {

* e.printStackTrace(); }

*/

int number = 1;

Thread ht = new Thread() {

public void run() {

try {

FileOutputStream os = new FileOutputStream("Name1.txt", true);

PrintWriter pW = new PrintWriter(os);

// BufferedWriter bW = new BufferedWriter(os);

while (true) {

String string = text.getText();

// pW.write("\r\n");

pW.println(string);

// pW.append(string);

pW.close();

}

// if (btn1 == e.getSource()) {

// os.close();

// }

} catch (Exception a) {

a.printStackTrace();

}

;

}

};

ht.start();

}

});

panel1.add(btn);

jf.setContentPane(panel1);

};

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值