Java做的一个小程序,随机点名器

2022-05-11 14:32:30   文档大全网     [ 字体: ] [ 阅读: ]

#文档大全网# 导语】以下是®文档大全网的小编为您整理的《Java做的一个小程序,随机点名器》,欢迎阅读!
点名,随机,程序,一个,Java
Java做的一个小程序,随机点名器

1. import java.io.*; 2. import java.awt.*; 3. import java.awt.event.*; 4. import java.util.List; 5. import java.util.ArrayList; 6.

7. public class DianName extends Frame{ 8.

9. private String fileName="a.txt"; 10. private File f; 11. private FileReader fr; 12. private BufferedReader br;

13. private List names=new ArrayList(); 14. private String strName; 15.

16. private Label labelName; 17. private Button btOK; 18.

19. public static void main(String[] args){ 20. DianName dn=new DianName(); 21. dn.newFrame(); 22. dn.read(); 23. } 24.

25. public void newFrame(){

26. labelName=new Label("显示姓名"); 27. btOK=new Button("开始"); 28.

29. this.setLocation(300,300); 30. this.setResizable(false); 31. this.setSize(500,400);

32. this.add(labelName,BorderLayout.NORTH); 33. this.add(btOK,BorderLayout.SOUTH); 34. this.pack();

35. this.setVisible(true);

36. this.addWindowListener(new WindowAdapter(){ 37. public void windowClosing(WindowEvent e){ 38. System.exit(0); 39. } 40. };

41. btOK.addActionListener(new ButtonAction()); 42. }


43.

44. public void read(){ 45. try{

46. f=new File(fileName); 47. if(!f.exists()){ 48. f.createNewFile(); 49. }

50. fr=new FileReader(f); 51. br=new BufferedReader(fr); 52. String str=br.readLine(); 53. while(str!=null){ 54. names.add(str); 55. str=br.readLine(); 56. }

57. }catch(Exception e){ 58. e.printStackTrace(); 59. } 60. 61. } 62.

63. public void write(){ 64. try{

65. int index=(int)(Math.random()*names.size()); 66. strName=names.get(index); 67. }catch(Exception e){ 68. e.printStackTrace(); 69. } 70. 71. } 72.

73. private class ButtonAction implements ActionListener{ 74. public void actionPerformed(ActionEvent e){ 75. write();

76. labelName.setText(strName); 77. } 78. } 79. }




本文来源:https://www.wddqxz.cn/668131bebcd126fff7050bae.html

相关推荐