简易定时关机
试试了 shutdown实现关机 重启就写了swing界面的定时关机 以后自己用
对输入的字符做判断时 temp总为0 老是给我关机 我就在那不停地试
电脑就不停的被关 郁闷着呢 帮改改给点建议
里面有一个博客地址和一个Q空间的链接
顺便进去留言那我就感激不尽了
就在Q空间中留下:"感谢GR50的支持与鼓励 "
那我就很感谢各位初学JAVA同胞了 呵呵...
程序代码:
package com.lyz.study;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax. javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.WindowConstants;
/**
* This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
* Builder, which is free for non-commercial use. If Jigloo is being used
* commercially (ie, by a corporation, company or business for any purpose
* whatever) then you should purchase a license for each developer using Jigloo.
* Please visit www. for details. Use of Jigloo implies
* acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
* PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
* ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ShutDownFrame extends javax.swing.JFrame {
private JTabbedPane mainTabbedPane;
private JButton space50Button;
private JLabel niLabel;
private JButton cancelButton;
private JTextField enterTimeTextField;
private JLabel minLabel;
private JButton exitButton;
private JLabel flagLabel;
private JLabel timeLabel;
private JPanel calendarPanel;
private JLabel textLabel;
private JButton blogButton;
private JButton commitButton;
private static JLabel viewTimeLabel;
private JLabel sysTimeLabel;
private JComboBox chooseComboBox;
private JPanel adPanel;
private JPanel shutPanel;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
ShutDownFrame inst = new ShutDownFrame();
inst.setVisible(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = screenSize.width;
int y = screenSize.height;
Dimension frameSize = inst.getSize();
int xx = (x - frameSize.width) / 2;
int yy = (y - frameSize.height) / 2;
inst.setLocation(xx, yy);
inst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getTimeView();
}
public ShutDownFrame() {
super("简易定时关机");
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
rootWindowClosing(evt);
}
});
{
mainTabbedPane = new JTabbedPane();
getContentPane().add(mainTabbedPane);
mainTabbedPane.setBounds(0, 0, 392, 266);
{
shutPanel = new JPanel();
mainTabbedPane.addTab("关机", null, shutPanel, null);
shutPanel.setBackground(new java.awt.Color(205, 242, 250));
shutPanel.setLayout(null);
{
ComboBoxModel chooseComboBoxModel = new DefaultComboBoxModel(
new String[] { "关机", "重启", "注销" });
chooseComboBox = new JComboBox();
shutPanel.add(chooseComboBox);
chooseComboBox.setModel(chooseComboBoxModel);
chooseComboBox.setBounds(217, 126, 70, 28);
chooseComboBox.setForeground(new java.awt.Color(0, 128,
64));
}
{
sysTimeLabel = new JLabel();
shutPanel.add(sysTimeLabel);
sysTimeLabel
.setText("\u7cfb\u7edf\u5f53\u524d\u65f6\u95f4");
sysTimeLabel.setBounds(14, 28, 112, 28);
sysTimeLabel.setForeground(new java.awt.Color(255, 0,
255));
}
{
viewTimeLabel = new JLabel();
shutPanel.add(viewTimeLabel);
viewTimeLabel.setBounds(133, 28, 140, 28);
viewTimeLabel.setForeground(new java.awt.Color(255, 0,
255));
}
{
commitButton = new JButton();
shutPanel.add(commitButton);
commitButton.setText("\u6267\u884c");
commitButton.setBounds(147, 196, 63, 28);
commitButton.setForeground(new java.awt.Color(0, 128,
64));
commitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
commitButtonActionPerformed(evt);
}
});
}
{
exitButton = new JButton();
shutPanel.add(exitButton);
exitButton.setText("\u9000\u51fa");
exitButton.setBounds(308, 196, 63, 28);
exitButton
.setForeground(new java.awt.Color(0, 128, 64));
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
}
{
minLabel = new JLabel();
shutPanel.add(minLabel);
minLabel.setText("\u5206\u949f\u540e");
minLabel.setBounds(147, 126, 63, 28);
}
{
enterTimeTextField = new JTextField();
shutPanel.add(enterTimeTextField);
enterTimeTextField.setBounds(21, 126, 112, 28);
}
{
cancelButton = new JButton();
shutPanel.add(cancelButton);
cancelButton.setText("\u53d6\u6d88");
cancelButton.setBounds(224, 196, 63, 28);
cancelButton.setForeground(new java.awt.Color(0, 128,
64));
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
}
{
niLabel = new JLabel();
shutPanel.add(niLabel);
niLabel.setText("\u4f60\u60f3\u5728:");
niLabel.setBounds(21, 77, 70, 28);
}
}
{
adPanel = new JPanel();
mainTabbedPane.addTab("非广告", null, adPanel, null);
adPanel.setBackground(new java.awt.Color(23, 196, 174));
adPanel.setLayout(null);
{
space50Button = new JButton();
adPanel.add(space50Button);
space50Button.setText("Q\u7a7a\u95f4");
space50Button.setBounds(105, 63, 126, 28);
space50Button.setForeground(new java.awt.Color(192,
207, 14));
space50Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
space50ButtonActionPerformed(evt);
}
});
}
{
blogButton = new JButton();
adPanel.add(blogButton);
blogButton.setText("\u535a\u5ba2");
blogButton.setBounds(126, 126, 77, 28);
blogButton.setForeground(new java.awt.Color(209, 215,
13));
blogButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
blogButtonActionPerformed(evt);
}
});
}
{
flagLabel = new JLabel();
adPanel.add(flagLabel);
flagLabel.setText("\u6807\u7b7e");
flagLabel.setBounds(147, 14, 63, 28);
flagLabel
.setForeground(new java.awt.Color(207, 218, 14));
}
{
textLabel = new JLabel();
adPanel.add(textLabel);
textLabel
.setText("\u4e09\u5c11\u7ee7\u7eed\u52aa\u529b \u52a0\u6cb9~~~~~~~");
textLabel.setBounds(70, 189, 245, 28);
textLabel
.setForeground(new java.awt.Color(45, 30, 225));
}
}
{
calendarPanel = new JPanel();
mainTabbedPane.addTab("\u65e5\u5386", null, calendarPanel,
null);
calendarPanel.setLayout(null);
}
{
timeLabel = new JLabel();
timeLabel.setLayout(null);
calendarPanel.add(timeLabel);
timeLabel.setBounds(7, 7, 133, 28);
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
private void exitButtonActionPerformed(ActionEvent evt) {
System.exit(0);
}
private void commitButtonActionPerformed(ActionEvent evt) {
long temp = 0;
try {
temp = Long.parseLong(this.enterTimeTextField.getText().toString()
.trim());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "输入整数吖 白痴");
try {
Runtime.getRuntime().getRuntime().exec("shutdown -a");
} catch (IOException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "未知错误 呵呵 ^_^ ~~");
e1.printStackTrace();
}
}
temp = (long) temp * 60;
String str = null;
String choose = this.chooseComboBox.getSelectedItem().toString()
.toString().trim();
if (choose.equals("关机")) {
str = "shutdown -s -t " + temp;
System.out.println(choose);
}
if (choose.equals("重启")) {
str = "shutdown -r -t " + temp;
System.out.println(choose);
}
if (choose.equals("注销")) {
JOptionPane.showMessageDialog(this, "注销也定时,也忒变态了吧!!!浪费电 直接关机了");
str = "shutdown -l - t " + temp;
}
try {
Runtime.getRuntime().getRuntime().exec(str);
this.enterTimeTextField.setEditable(false);
(false);
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "未知错误 呵呵 ^_^ ~~");
e.printStackTrace();
}
this.exitButton.setEnabled(false);
}
private void cancelButtonActionPerformed(ActionEvent evt) {
try {
Runtime.getRuntime().exec("shutdown -a");
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "未知错误 呵呵 ^_^ ~~");
e.printStackTrace();
}
this.exitButton.setEnabled(true);
this.enterTimeTextField.setEditable(true);
(true);
}
private static void getTimeView() {
// TODO Auto-generated method stub
do {
Calendar calendar = GregorianCalendar.getInstance();
Date date = calendar.getTime();// 得到当前时间
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeNow = df.format(date);
viewTimeLabel.setText(timeNow);
} while (true);
}
private void space50ButtonActionPerformed(ActionEvent evt) {
String url = "http://306290032.qzone. d = Desktop.getDesktop();
try {
d.browse(new URI(url));
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "未知错误 呵呵 ^_^ ~~");
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "未知错误 呵呵 ^_^ ~~");
e.printStackTrace();
}
// try {
// Runtime.getRuntime().exec("http://www.baidu.com");
// } catch (IOException e) {
// // TODO Auto-generated catch block
// JOptionPane.showMessageDialog(this, "未知错误 呵呵 ^_^ ~~");
// e.printStackTrace();
// }
}
private void blogButtonActionPerformed(ActionEvent evt) {
Desktop d = Desktop.getDesktop();
try {
d.browse(new URI("http://home.));
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "未知错误 呵呵 ^_^ ~~");
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "未知错误 呵呵 ^_^ ~~");
e.printStackTrace();
}
}
private void rootWindowClosing(WindowEvent evt) {
try {
Runtime.getRuntime().exec("shutdown -a");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}






