自己写的最简单的关机打卡的代码,大神勿喷
package tellMeSign;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
//写个关机打卡的程序
/**
* @author Administrator
*
*/
public class SignDemo extends JFrame implements ActionListener{
//关机按钮
private JButton countDown;
//取消按钮
private JButton cancel;
//提示
private JLabel label;
private JPanel Npanel;
private JPanel Spanel;
public SignDemo(){
super("打卡提示工具-g.jinp");
label=new JLabel("是否已经打卡");
countDown=new JButton("是,我确定关机");
cancel=new JButton("不,我还没有打卡");
Npanel=new JPanel();
Npanel.add(label);
Spanel=new JPanel();
Spanel.add(countDown);
Spanel.add(cancel);
countDown.addActionListener(this);
cancel.addActionListener(this);
//基本属性设置
this.add(Npanel,BorderLayout.NORTH);
this.add(Spanel,BorderLayout.SOUTH);
this.setVisible(true);
this.setResizable(false);
this.setSize(325,125);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==countDown){
countDown();
}else if(e.getSource()==cancel){
cancel();
}
}
private void countDown() {//实现关机方法
// TODO Auto-generated method stub
try {
Runtime.getRuntime().exec("shutdown -s");
} catch (IOException e) {
e.printStackTrace();
}
}
private void cancel() {//实现取消关机
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(this, "您已经取消关机任务!");
try {
Runtime.getRuntime().exec("shutdown -a");
} catch (IOException e) {
e.printStackTrace();
}
}
}
<pre name="code" class="java">public class Test {
public static void main(String[] args) {
new SignDemo();
}
}