📄 task.java
字号:
package part1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
public class task extends JFrame implements ActionListener{
static int cH=0;
static int ch=0;
static int cM=0;
static int cm=0;
static int cS=0;
static int cs=0;
public static String taskmsg=null;
FlowLayout fl0=new FlowLayout();
JLabel tjl0=new JLabel("请设置关机时间:");
JLabel tjl1=new JLabel(":");
JLabel tjl2=new JLabel(":");
JTextField jt0=new JTextField(2);
JTextField jt1=new JTextField(2);
JTextField jt2=new JTextField(2);
JButton tjb0=new JButton("确定");
JButton tjb1=new JButton("取消");
task(){
super("关机任务");
setSize(250,200);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(fl0);
this.getContentPane().add(tjl0);
this.getContentPane().add(jt0);
this.getContentPane().add(tjl1);
this.getContentPane().add(jt1);
this.getContentPane().add(tjl2);
this.getContentPane().add(jt2);
this.getContentPane().add(tjb0);
this.getContentPane().add(tjb1);
tjb0.addActionListener(this);
tjb1.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);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==tjb1)
setVisible(false);
if(e.getSource()==tjb0){
if(Integer.parseInt(jt0.getText().trim())<=24&&Integer.parseInt(jt1.getText().trim())<=59&&Integer.parseInt(jt2.getText().trim())<=59){
taskmsg=jt0.getText().trim()+":"+jt1.getText().trim()+":"+jt2.getText().trim();
//setVisible(false);
}
else
{
JLabel warm=new JLabel("时间设置有误,请重新设置!");
JOptionPane warmi=new JOptionPane();
warmi.showMessageDialog(null, warm,"出错了",JOptionPane.ERROR_MESSAGE);
jt0.setText("");
jt1.setText("");
jt2.setText("");
}
if(taskmsg!=null){
cH=Integer.parseInt((jt0.getText().trim()).substring(0,2))-(10*timer.H+timer.h);
cM=Integer.parseInt((jt1.getText().trim()).substring(0,1))-timer.M;
cm=Integer.parseInt((jt1.getText().trim()).substring(1,2))-timer.m;
cS=Integer.parseInt((jt2.getText().trim()).substring(0,1))-timer.S;
cs=Integer.parseInt((jt2.getText().trim()).substring(1,2))-timer.s;
if(cs<0){
cs+=10;
cS-=1;
}
if(cS<0){
cS+=6;
cm-=1;
}
if(cm<0){
cm+=10;
cM-=1;
}
if(cM<0){
cM+=6;
cH-=1;
}
if(cH<0){
cH+=24;
}
/*if(cH>=20&&cH<24){
ch=cH-20;
cH=2;
}
if(cH>=10&&cH<20){
ch=cH-10;
cH=1;
}
if(cH>=0&&cH<10){
ch=cH;
cH=0;
}*/
timer.jlt.setText("系统将在预设的时间关闭:"+task.taskmsg);
timer.jlt0.setText("距离设定的关机任务时间还有:");
timer.jltc0.setText(cH+":"+cM+""+cm+":"+cS+""+cs);
dcounter inc=new dcounter();
inc.start();
setVisible(false);
}
}
}
class dcounter extends Thread{
public void run(){
//boolean stopc=false;
while(true){
timer.jltc0.setText(cH+":"+cM+""+cm+":"+cS+""+cs);
try {
this.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cs--;
if(cH==0&&cM==0&&cm==0&&cS==0&&cs<0){
new closewin();
this.stop();
}
if(cs<0){
cS-=1;
cs=9;
}
if(cS<0){
cS=5;
cm-=1;
}
if(cm<0){
cm=9;
cM-=1;
}
if(cM<0){
cM=5;
cH-=1;
}
}
}
}
class closewin{
closewin(){
String command = "shutdown -s -t 00";
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -