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);
};
}