当前位置:网站首页>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();
}
}
}
}
边栏推荐
- Detailed explanation of network layer
- Myrpc version 6
- Network layer protocol hardware
- Troubleshoot abnormal video playback problems in public network deployment based on Haikang ehomedemo tool
- Es2019 key summary
- Day 12 advanced programming techniques
- win10系统使用浏览器下载后,内容无故移动或删除
- My idea configuration
- Redis sentry, persistence, master-slave, hand tear LRU
- Learn about threads
猜你喜欢
MySQL DDL change
el-upload上傳文件(手動上傳,自動上傳,上傳進度)
Interview topic of MySQL
Educoder group purchase suspension box page production
深度融合云平台,对象存储界的“学霸”ObjectScale来了
Intern method of string
lego_ Reading and summary of loam code
Myrpc version 1
Process architecture and process management
FortiGate firewall quick initialization administrator password
随机推荐
Clients accessing the daytime service (TCP)
JS inheritance
JS reflect
File system and directory operations
How the FortiGate firewall rejects a port by using the local in policy policy
Troubleshoot abnormal video playback problems in public network deployment based on Haikang ehomedemo tool
Myrpc version 0
487-3279(POJ1002)
Technology sharing | broadcast function design in integrated dispatching
MySQL updates JSON string in array form
Named pipes for interprocess communication
How to write a conditional statement to obtain the value of the maximum time in a table using a MySQL statement
Internship: interface case implementation
SQLyog导入数据库时报错,求帮解决!
SQL追加字段
Day 10 data saving and loading
Salary management system based on servlet+jsp+mysql [source code + database]
Day 11 script and game AI
base64.c
Errno and PERROR