当前位置:网站首页>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();
}
}
}
}
边栏推荐
- Interview topic of MySQL
- Threejs realizes the simulation of river, surface flow, pipe flow and sea surface
- 深度融合云平台,对象存储界的“学霸”ObjectScale来了
- An error occurs when sqlyog imports the database. Please help solve it!
- Troubleshooting of abnormal communication between FortiGate and fortiguard cloud
- What is an optocoupler circuit and what should be paid attention to in actual use?
- Sql语句遇到的错误,求解
- Indefinite parameters of JS function
- Clients accessing the daytime service (TCP)
- 2021-11-04
猜你喜欢

深度融合云平台,对象存储界的“学霸”ObjectScale来了

Blocking queue example

Process architecture and process management

Myrpc version 0

Day 11 script and game AI

Myrpc version 5
![Salary management system based on servlet+jsp+mysql [source code + database]](/img/4a/6015cf17f4297691e97b48a5592fc5.png)
Salary management system based on servlet+jsp+mysql [source code + database]

Learning about signals

Network layer protocol hardware

El upload Upload file (Manual upload, Automatic upload, upload progress)
随机推荐
Internship: interface case implementation
Basic knowledge of redis
JS import and export
Equity interest [non DP]
Myrpc version 2
El upload Upload file (Manual upload, Automatic upload, upload progress)
Unity when entering a string in the editor, escape the input of characters
With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming
Indefinite parameters of JS function
Day 11 script and game AI
File and IO
Memorize unfamiliar words at SSM stage and update them from time to time
Sql语句遇到的错误,求解
Mongodb learning
FortiGate firewall quick initialization administrator password
基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】
Five methods to clear floating and their advantages and disadvantages
FortiGate creates multiple corresponding DDNS dynamic domain names for multiple ADSL interfaces
Myrpc version 1
【WEBRTC】ADM: rtc_ include_ internal_ audio_ Device triggers RTC_ Dcheck (ADM) assertion