当前位置:网站首页>Enlist soldiers and generals, draw small programs, multi-threaded display time
Enlist soldiers and generals, draw small programs, multi-threaded display time
2022-06-30 04:30:00 【A fat man】
package pointSoldierPoint;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.TreeSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.xml.crypto.Data;
/**
* @author : htf
* Java The program for ordering soldiers and generals , Luck draw , Time stamp
*/
public class Soldier extends JFrame implements Runnable{
/**
* Declare five components :Frame Panel Button Label TextArea
*/
private JPanel panel;
private JButton button;
private JLabel label;
private JTextArea text;
/**
* data source
*/
private static Map<Integer, String> map=null;
static {
map=new HashMap<Integer, String>();
String name=null;
int id=1;
// Get files from outside
File file=new File("students.txt");
try(BufferedReader bf=new BufferedReader(new FileReader(file));){
while((name=bf.readLine())!=null) {
map.put(id, name);
id++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Call other methods by constructing methods , And set the relevant properties
public Soldier() {
init();
buttonAt();
this.setSize(600, 500);// Form size
this.setDefaultCloseOperation(EXIT_ON_CLOSE);// close
this.setTitle(" Luck draw ");// title
this.setVisible(true);// Set display
}
/**
* Instantiate objects init, Assembly components
*/
public void init() {
panel=new JPanel(new GridLayout(3, 1));// Grid layout
button=new JButton(" Am I ");
label=new JLabel(" The winners ");
text=new JTextArea(3,30);
panel.add(button);
panel.add(label);
panel.add(text);
this.getContentPane().add(panel);
}
/**
* Register listener
* Generate random numbers , obtain map,key Value corresponds to information , Show it to the program
*/
public void buttonAt() {
button.addActionListener((e)->{
TreeSet<Integer> set=new TreeSet<>();// Sorting is not repeated
while(set.size()<5) {
Random random=new Random();
int num=random.nextInt(29)+1;
set.add(num);
}
StringBuffer sb =new StringBuffer();// Store the name of the winner
for(Integer s:set) {
String name=map.get(s);
sb.append(name+",");
}
text.setText(sb.substring(0, sb.toString().length()-1));
});
}
public static void main(String[] args) {
System.out.println(map.size());
Soldier s1=new Soldier();// Construction method
new Thread(s1).start();// Start time thread
}
@Override
public void run() {
// TODO Auto-generated method stub
// Setup time
while(true) {
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time =sdf.format(date);
label.setText(" The winners ( current time "+time+")");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
边栏推荐
- Directory operations and virtual file systems
- 基于SSM框架茶叶商城系统【项目源码+数据库脚本+报告】
- Errno and PERROR
- Five methods to clear floating and their advantages and disadvantages
- Indefinite parameters of JS function
- win10系统使用浏览器下载后,内容无故移动或删除
- With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming
- FortiGate firewall filters the specified session and cleans it up
- Threejs realizes the simulation of river, surface flow, pipe flow and sea surface
- El upload Upload file (Manual upload, Automatic upload, upload progress)
猜你喜欢

el-upload上傳文件(手動上傳,自動上傳,上傳進度)

I spent three years in a big factory outsourcing, which subverted my understanding!

Myrpc version 3

With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming

Learning about signals

Myrpc version 6

Share an example of a simple MapReduce method using a virtual machine

Redis sentry, persistence, master-slave, hand tear LRU

What is an optocoupler circuit and what should be paid attention to in actual use?

lego_ Reading and summary of loam code
随机推荐
7-3 single source shortest circuit for strange play upgrade
Mongodb learning
节点CODE相同会导致数据重复
SQL append field
Daily summary of code knowledge
Salary management system based on servlet+jsp+mysql [source code + database]
Modifier of JS regular expression
Splicing strings with custom functions
2021-07-14
FortiGate performs DNAT mapping, and intranet users cannot normally access the mapping
After the win10 system uses the browser to download, the content is moved or deleted without reason
Find the interface and add parameters to the form
Day 10 data saving and loading
JS generator
JS import and export
FortiGate creates multiple corresponding DDNS dynamic domain names for multiple ADSL interfaces
Troubleshooting of abnormal communication between FortiGate and fortiguard cloud
Tea mall system based on SSM framework [project source code + database script + report]
找到接口在表单里加参数
Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions