📄 timer.java
字号:
package part1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.*;
public class timer extends JFrame implements ActionListener {
static int h=0;
static int H=0;
static int m=0;
static int M=0;
static int s=0;
static int S=0;
static String hy=null;
JButton jb=new JButton("退出");
JButton jb0=new JButton("设置关机任务");
JLabel jl0=new JLabel(H+""+h);
JLabel jl1=new JLabel(M+""+m);
JLabel jl2=new JLabel(S+""+s);
JLabel jl3=new JLabel(":");
JLabel jl4=new JLabel(":");
JLabel jl5=new JLabel("您当前操作系统的时间是:");
static JLabel jl7=new JLabel("");
static JLabel jl6=new JLabel("");
static JLabel jlt=new JLabel("");
static JLabel jlt0=new JLabel("");
static JLabel jltc0=new JLabel("");
static JLabel jltc1=new JLabel("");
static JLabel jltc2=new JLabel("");
FlowLayout fl=new FlowLayout();
timer(){
super("XP定时关机系统V1.0");
setSize(300,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
this.getContentPane().setLayout(fl);
this.getContentPane().add(jl7);
this.getContentPane().add(jl5);
this.getContentPane().add(jl6);
this.getContentPane().add(jl0);
this.getContentPane().add(jl3);
this.getContentPane().add(jl1);
this.getContentPane().add(jl4);
this.getContentPane().add(jl2);
this.getContentPane().add(jlt);
this.getContentPane().add(jlt0);
this.getContentPane().add(jltc0);
this.getContentPane().add(jltc1);
this.getContentPane().add(jltc2);
this.getContentPane().add(jb0);
this.getContentPane().add(jb);
jb.addActionListener(this);
jb0.addActionListener(this);
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
Dimension frameSize=this.getSize();
if(frameSize.height>screenSize.height){
frameSize.height=screenSize.height;
}
if(frameSize.width>screenSize.width){
frameSize.width=screenSize.width;
}
this.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
countThread test=new countThread();
test.start();
setVisible(true);
}
public static void main(String[] args){
Date date=new Date();
String time=date.toString();
String hour=time.substring(11, 13);
String minute=time.substring(14, 16);
String second=time.substring(17, 19);
String h1=hour.substring(0,1);
String h0=hour.substring(1,2);
String m1=minute.substring(0,1);
String m0=minute.substring(1,2);
String s1=second.substring(0,1);
String s0=second.substring(1,2);
H=Integer.parseInt(h1);//呢句将String型噶数据转换成int型啊
h=Integer.parseInt(h0);
M=Integer.parseInt(m1);
m=Integer.parseInt(m0);
S=Integer.parseInt(s1);
s=Integer.parseInt(s0);
switch(H){
case 0:
hy="早上好!";
break;
case 1:
if(h<2)hy="早上好!";
else if(h==2) hy="中午好!";
else if(h<9)hy="下午好!";
else hy="晚上好!";
break;
case 2:
hy="晚上好!";
break;
default:
break;
}
jl7.setText(hy);
new timer();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source=e.getSource();
if(source==jb){
System.exit(0);
}
if(source==jb0){
new task();
}
}
class countThread extends Thread{
public void run(){
while(true){
jl2.setText(S+""+s);
try {
this.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s++;
if(s==10){
s=0;
S+=1;
jl2.setText(S+""+s);
}
if(S==6){
s=0;
S=0;
m+=1;
jl1.setText(M+""+m);
}
if(m==10){
m=0;
M+=1;
jl1.setText(M+""+m);
}
if(M==6){
M=0;
m=0;
h+=1;
jl1.setText(M+""+m);
jl0.setText(H+""+h);
}
if(h==10){
h=0;
H+=1;
jl0.setText(H+""+h);
}
if(H==2&&h==4){
s=0;
S=0;
m=0;
M=0;
h=0;
H=0;
jl2.setText(S+""+s);
jl1.setText(M+""+m);
jl0.setText(H+""+h);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -