📄 automshutdown.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.TimeZone;
import java.util.GregorianCalendar;
import java.awt.Font;
import com.ibm.iwt.IFrame;
import com.ibm.iwt.window.*;
public class AutomShutDown extends TestApp4 implements ActionListener,MouseMotionListener,MouseListener
{
private int hh=10000,mm,ss;
private JLabel jlb1=new JLabel(" 当 前 时 间:");
private JLabel jlb2=new JLabel(" 关 机 时 间:");
private JTextField jtfh=new JTextField();
private JTextField jtfm=new JTextField();
private JTextField jtfs=new JTextField();
private JTextField jtfhh=new JTextField();
private JTextField jtfmm=new JTextField();
private JTextField jtfss=new JTextField();
private JButton jbStart=new JButton("启用");
private JButton jbCancel=new JButton("取消");
private JButton jbExit=new JButton("退出");
private TimeThread thread=new TimeThread();
public AutomShutDown()
{
//setTitle("欢迎使用!");
jlb1.setFont(new Font("Serif",Font.BOLD,18));
jlb2.setFont(new Font("Serif",Font.BOLD,18));
jtfh.setFont(new Font("Serif",Font.BOLD,18));
jtfm.setFont(new Font("Serif",Font.BOLD,18));
jtfs.setFont(new Font("Serif",Font.BOLD,18));
jtfhh.setFont(new Font("Serif",Font.BOLD,18));
jtfmm.setFont(new Font("Serif",Font.BOLD,18));
jtfss.setFont(new Font("Serif",Font.BOLD,18));
JPanel leftPanel1=new JPanel();
leftPanel1.setBackground(Color.yellow);
leftPanel1.setPreferredSize(new Dimension(5,10));
JPanel leftPanel2=new JPanel();
leftPanel2.setBackground(Color.yellow);
leftPanel2.setPreferredSize(new Dimension(5,10));
JPanel p1=new JPanel();
p1.setBackground(Color.yellow);
p1.add(jlb1);
JPanel p2=new JPanel();
p2.setBackground(Color.yellow);
p2.setLayout(new GridLayout(1,7));
jtfh.setEditable(false);
jtfh.setForeground(Color.red);
p2.add(leftPanel1);
p2.add(jtfh);
p2.add(new JLabel(" 点"));
jtfm.setEditable(false);
jtfm.setForeground(Color.red);
p2.add(jtfm);
p2.add(new JLabel(" 分"));
jtfs.setEditable(false);
jtfs.setForeground(Color.red);
p2.add(jtfs);
p2.add(new JLabel(" 秒"));
JPanel p3=new JPanel();
p3.setBackground(Color.yellow);
p3.add(jlb2);
JPanel p4=new JPanel();
p4.setBackground(Color.yellow);
p4.setLayout(new GridLayout(1,7));
p4.add(leftPanel2);
p4.add(jtfhh);
p4.add(new JLabel(" 点"));
p4.add(jtfmm);
p4.add(new JLabel(" 分"));
p4.add(jtfss);
p4.add(new JLabel(" 秒"));
JPanel p5=new JPanel();
p5.setBackground(Color.yellow);
p5.add(jbStart);
p5.add(jbCancel);
p5.add(jbExit);
getIContentPane().setLayout(new GridLayout(5,1));
getIContentPane().setBackground(Color.yellow);
getIContentPane().add(p1);
getIContentPane().add(p2);
getIContentPane().add(p3);
getIContentPane().add(p4);
getIContentPane().add(p5);
jbStart.addActionListener(this);
jbCancel.addActionListener(this);
jbExit.addActionListener(this);
this.addMouseMotionListener(this);
this.addMouseListener(this);
thread.Start();
}
public void actionPerformed(ActionEvent ex)
{
if(ex.getSource()==jbStart)
{
if(jtfhh.getText().equals("")||jtfmm.getText().equals("")||jtfss.getText().equals(""))
{
hh=10000;//破坏关机条件
mm=0;
ss=0;
JOptionPane.showMessageDialog(this,"时间不能为空","For Your Information",JOptionPane.INFORMATION_MESSAGE);
}
else
{
hh=Integer.parseInt(jtfhh.getText());
mm=Integer.parseInt(jtfmm.getText());
ss=Integer.parseInt(jtfss.getText());
JOptionPane.showMessageDialog(this,"您可以安息了","For Your Information",JOptionPane.INFORMATION_MESSAGE);
}
}
else if(ex.getSource()==jbCancel)
{
jtfhh.setText("");
jtfmm.setText("");
jtfss.setText("");
JOptionPane.showMessageDialog(this,"已取消设定","For Your Information",JOptionPane.PLAIN_MESSAGE);
}
else if(ex.getSource()==jbExit)
{
System.exit(0);
}
}
//重写相关方法让user不能移动窗体
public void mouseMoved(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public static void main(String[] args)
{
AutomShutDown frame=new AutomShutDown();
frame.setSize(400,200);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=frame.getSize();
int x=(screenSize.width-frameSize.width)/2;
int y=(screenSize.height-frameSize.height)/2;
frame.setLocation(x,y-100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class TimeThread implements ActionListener
{
private int h,m,s;
private Timer timer;
private TimeZone timeZone=TimeZone.getDefault();
private Runtime rt=Runtime.getRuntime();
public TimeThread()
{
timer=new Timer(1000,this);
timer.start();
}
public void Start()
{
timer.start();
}
public void Stop()
{
timer.stop();
}
public void actionPerformed(ActionEvent e)
{
Show();
Check();
}
public void Show()
{
GregorianCalendar calendar=new GregorianCalendar(timeZone);
h=(int)calendar.get(GregorianCalendar.HOUR_OF_DAY);
m=(int)calendar.get(GregorianCalendar.MINUTE);
s=(int)calendar.get(GregorianCalendar.SECOND);
jtfh.setText(String.valueOf(h));
jtfm.setText(String.valueOf(m));
jtfs.setText(String.valueOf(s));
}
//检查是否满足关机条件
public void Check()
{
if(h==hh&&m==mm&&s==ss)
try
{
rt.exec("ShutDown -s -t 0");
}catch(Exception e)
{}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -