java自己做的软件---人员抽取小软件..有待改进啊...

mport java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class choose extends JFrame {
private static final int H = 0;
//这是定义部分
//JPanel jp;
JPanel jp1;
JMenuBar jmb;
JMenu jm;
JMenu jm1;
//JMenu jm2;
//JMenu jm3;
JMenuItem add;
JMenuItem del;
JMenuItem exit;
JMenuItem zs;
JMenuItem js;
JMenuItem aboutus;
JMenuItem ckbz;
JLabel jl;
JLabel jl1;
JButton ks;
JButton tz;
String[] reny={"王燕","王小红","李德伟","张伟","田晓伟","赵晓峰","王晓光","刘泽","林俊杰","丁芬祥","张文杰","张玉爽","李珊珊","赵潭谭","吴洋洋"};
Mythread my;
MyTime tim;
int speed=1000;
//这是构造方法用于,初始化窗体
public choose (){
//这里调用显示时间的组件
//new MyTime().start();
//这是构造方法用于,初始化窗体
this.setSize(300, 400);
this.setVisible(true);
this.setTitle("晓峰--人员随机抽取器");
this.setLocationRelativeTo(null);
//this.setBackground(Color.blue);
//this.setLayout(new GridLayout(3,1));
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
//下面创建菜单栏
jmb=new JMenuBar();
jm=new JMenu("人员管理 ");
jm1=new JMenu("状态设置 ");
aboutus=new JMenuItem("关于我们");
//为aboutus添加事件监听。
aboutus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(rootPane, "创梦网络--晓峰qq:1606841559");
}
});
//jm2=new JMenu("关于我们 ");
// jm3=new JMenu("查看帮助");
//jm3设置监听事件
// jm3.addActionListener(new ActionListener() {
//
// @Override
// public void actionPerformed(ActionEvent e) {
// // TODO Auto-generated method stub
// JOptionPane.showMessageDialog(jm3, "请按alt+z开始,请按alt+s结束");
// }
// });
add=new JMenuItem("增加");
del=new JMenuItem("减少");
exit=new JMenuItem("退出");
//给exit增加响应
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
zs=new JMenuItem("增速");
//给zs设置快捷键menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
zs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
//给增速设置,响应事件;
zs.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
speed-=100;
int c=speed-50;
if (c<0) {
speed=0;
JOptionPane.showMessageDialog(zs, "您的速度太快了,不能再加了");
} else{
speed-=100;
}
}
});
js=new JMenuItem("减速");
//为js添加快捷键 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
js.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK));
//为js添加响应事件
js.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
speed+=100;
}
});
//aboutus=new JMenuItem("联系我们");
ks=new JButton("开始");
//设置开始按钮的事件监听。
//设置按钮ks的快捷键
ks.setMnemonic(KeyEvent.VK_Z);
ks.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (my!=null) {
my.sto();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//else{
Mythread s=new Mythread();
s.stat();
//}
}
});
ckbz=new JMenuItem("查看帮助");
ckbz.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(rootPane, "请按alt+z开始 请按alt+s停止");
}
});
tz=new JButton("停止");
//为tz添加事件监听器
//为停止添加快捷键
tz.setMnemonic(KeyEvent.VK_S);
tz.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Mythread st=new Mythread();
st.sto();
}
});
new MyTime().start();
//jp=new JPanel();
jp1=new JPanel();
//下面创建及设置label
jl1=new JLabel();
jl=new JLabel();
jl.setText("开始抽取");
jl1.setFont(new Font("宋体", Font.PLAIN, 15));
jl1.setHorizontalAlignment(Label.LEFT);
jl.setFont(new Font("黑体", Font.PLAIN, 50));
//jl.setSize(298, 310);
jl.setHorizontalAlignment(JLabel.CENTER);
//组建菜单和窗体---组建菜单
jm.add(add);
jm.addSeparator();//分割线
jm.add(del);
jm.addSeparator();
jm.add(exit);
jm1.add(zs);
jm1.addSeparator();
jm1.add(js);
//jm2.add(aboutus);
jmb.add(jm);
jmb.add(jm1);
jmb.add(aboutus);
jmb.add(ckbz);
//jmb.add(jm2);
//jmb.add(jm3);
//jp.setSize(298, 310);
//jp.add(jl);
jp1.add(ks);
jp1.add(tz);
//把菜单栏放到窗体上
setJMenuBar(jmb);
add(jl, BorderLayout.CENTER);
add(jp1, BorderLayout.SOUTH);
add(jl1, BorderLayout.NORTH);

}
boolean flag;
class Mythread extends Thread{
@Override
public void run() {
while(flag){
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Random r=new Random();
int n=r.nextInt(reny.length);
jl.setText(reny[n]);
}
}
public void stat(){
Mythread b=new Mythread();
flag=true;
b.start();
}
public void sto(){
Mythread b=new Mythread();
flag=false;
b.stop();
}
}
//显示时间的事件监听
class MyTime extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while(true){
Date d=new Date();
int h=d.getHours();
int m=d.getMinutes();
int s=d.getSeconds();
String current="当前的时间是"+h+":"+m+":"+s;
jl1.setText(current);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}
//用于执行的main函数;
public static void main(String[] args) {
//调用choose类
new choose();
}
}

你可能感兴趣的:(java)