案例3:JAVA GUI 随机点名程序
先开发一个姓名维护的界面,输入学生的姓名,每行录入一个学生姓名,点击保存的时候将学生的姓名保存到一个txt文件中。
再开发一个点名的程序,从维护好的txt文件中,随机读取一个学生的姓名显示出来,表示该学生已被点名。
1.维护名称
2.随机点名
3.源码
CallNamePage.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | //GUI界面 package CallName; import java.awt.Toolkit; import java.awt.event.*; import javax.swing.*; //点名程序 public class CallNamePage extends JFrame { JTextField txt11 = new JTextField(); JButton btn = new JButton( "随机点名" ); JButton btn2 = new JButton( "姓名维护" ); public CallNamePage() { super ( "标题" ); final JLabel lab = new JLabel( "点名中...." ); lab.setBounds( 120 , 40 , 120 , 40 ); txt11.setBounds( 120 , 100 , 120 , 40 ); btn.setBounds( 120 , 160 , 120 , 40 ); btn2.setBounds( 120 , 210 , 120 , 40 ); //事件 btn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ee) { lab.setText( "新的幸运儿已经产生" ); CallNameTool tool = new CallNameTool(); try { txt11.setText(tool.DoCallName()); } catch (Exception ex) { JOptionPane.showMessageDialog( null , "点名失败" ); } } }); btn2.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ee) { CallNameSetting st= new CallNameSetting(); st.CenterPanel(); } }); // 将控件添加到容器 JPanel p = new JPanel(); p.setLayout( null ); // 布局标题 p.add(lab); p.add(txt11); p.add(btn); p.add(btn2); getContentPane().add(p); setSize( 400 , 400 ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible( true ); } // 将界面开始位置显示到屏幕中间 public void CenterPanel() { int width = Toolkit.getDefaultToolkit().getScreenSize().width; int height = Toolkit.getDefaultToolkit().getScreenSize().height; this .setLocation(width / 2 , height / 4 ); } } |
CallNameSetting.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | //游戏主界面 package CallName; import java.awt.Toolkit; import java.awt.event.*; import javax.swing.*; //点名设置界面 public class CallNameSetting extends JFrame { // 输入用户列表 JLabel tip= new JLabel( "提示:按每个姓名为一行进行维护。" ); JTextArea txt11= new JTextArea(); // 功能按钮 JButton btnSave = new JButton( "保存设置" ); JButton btnBegin = new JButton( "开始点名" ); public CallNameSetting() { super ( "点名设置" ); tip.setBounds( 20 , 10 , 220 , 20 ); txt11.setBounds( 20 , 40 , 220 , 200 ); btnSave.setBounds( 20 , 270 , 100 , 20 ); btnBegin.setBounds( 150 , 270 , 100 , 20 ); btnSave.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ee) { CallNameTool tool= new CallNameTool(); try { tool.SaveCallName(txt11.getText()); JOptionPane.showMessageDialog( null , "已保存!" ); } catch (Exception ex) { // JOptionPane.showMessageDialog( null , "保存失败" ); } } }); btnBegin.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ee) { CallNamePage cp= new CallNamePage(); cp.CenterPanel(); } }); // 将控件添加到容器 JPanel p = new JPanel(); p.setLayout( null ); p.add(tip); p.add(txt11); p.add(btnSave); p.add(btnBegin); getContentPane().add(p); setSize( 300 , 400 ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible( true ); //初始化姓名列表 CallNameTool tool= new CallNameTool(); try { String result=tool.GetCallName(); txt11.setText(result); } catch (Exception ex) { // JOptionPane.showMessageDialog( null , "读取用户配置文件失败" ); } } // 程序入口 public static void main(String[] args) { CallNameSetting s = new CallNameSetting(); s.CenterPanel(); } // 将界面开始位置显示到屏幕中间 public void CenterPanel() { int width = Toolkit.getDefaultToolkit().getScreenSize().width; int height = Toolkit.getDefaultToolkit().getScreenSize().height; this .setLocation(width / 2 , height / 4 ); } } |
CallNameTool.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package CallName; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Random; //实现点名功能 public class CallNameTool { //随机读取一个姓名 public static String DoCallName() throws IOException { BufferedReader br= new BufferedReader( new FileReader( "name.txt" )); ArrayList<String> array= new ArrayList<>(); String line; //读取文件内容到ArrayList while ((line= br.readLine())!= null ){ array.add(line); } br.close(); //随机返回一个姓名 Random r= new Random(); int index = r.nextInt(array.size()); String name = array.get(index); return name; } //读取所有姓名 public static String GetCallName() throws IOException { BufferedReader br= new BufferedReader( new FileReader( "name.txt" )); String result= "" ; String line; //读取文件内容到ArrayList while ((line= br.readLine())!= null ){ result+=line+ "\n" ; } br.close(); return result; } //保存姓名 public static void SaveCallName(String content) throws IOException { String word = content; FileOutputStream fileOutputStream = null ; File file = new File( "name.txt" ); if (!file.exists()){ file.createNewFile(); } fileOutputStream = new FileOutputStream(file); fileOutputStream.write(word.getBytes( "gbk" )); fileOutputStream.flush(); fileOutputStream.close(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2017-11-04 jQuery学习之------html()、text()和val()
2017-11-04 jQuery学习之------对标签属性的操作
2017-11-04 jQuery学习之------选择器