⽤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 = (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("结果");
*有趣的java小程序
* 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(""); if (ists() != true)
* { try { ateNewFile(); } catch (Exception e) {
* e.printStackTrace(); }
*/
int number = 1;
Thread ht = new Thread() {
public void run() {
try {
FileOutputStream os = new FileOutputStream("", true); PrintWriter pW = new PrintWriter(os);
// BufferedWriter bW = new BufferedWriter(os);
while (true) {
String string = Text();
// 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); };
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论