惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
V
V2EX
S
Security Affairs
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
J
Java Code Geeks
The Register - Security
The Register - Security
U
Unit 42
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Project Zero
Project Zero
S
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
博客园 - 司徒正美
V
Vulnerabilities – Threatpost
T
Tor Project blog
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
小众软件
小众软件
L
LangChain Blog
Attack and Defense Labs
Attack and Defense Labs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Palo Alto Networks Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 最新话题
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta

博客园 - soulsjie

一种在winfrom窗体中显示计算公式的解决方案 winform窗体DataGridView合并单元格处理 C#GDI+阴影笔刷样式HatchStyle探讨 C#代码混淆工具ConfuserEx的使用 Aspose.Words在指定位置插入图片、调整图片大小 C#获取对象实体的键值对信息 C#将Winform上的TextBox和ComBox的值导入和导出 C#使用Aspose.Words将Spread表格插入到Word中 C# Aspose.Words将word中自定义的标签进行替换 Python文件操作基础方法 C# 将项目资源文件保存到磁盘上 SqlSugar数据库辅助类 使用存储过程备份MS SQLServer数据库 案例2:JAVA GUI 简易计算器 ArcGIS JS API 添加要素图层 点击时获取图层属性 ArcGIS JS API 将天地图设置为底图 HTML5PLUS实现类似右侧弹出菜单 SqlSugar各数据库连接串 案例1:JAVAGUI用户管理
案例3:JAVA GUI 随机点名程序
soulsjie · 2022-11-04 · via 博客园 - soulsjie

先开发一个姓名维护的界面,输入学生的姓名,每行录入一个学生姓名,点击保存的时候将学生的姓名保存到一个txt文件中。

再开发一个点名的程序,从维护好的txt文件中,随机读取一个学生的姓名显示出来,表示该学生已被点名。

1.维护名称

2.随机点名

3.源码 

CallNamePage.java

//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

//游戏主界面
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

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