📄 aa.java
字号:
/**
*自动关机程序,可以采用倒计时或定时方式关机
*新加入的专家模式可以调用命令行
*但是命令行中的某些命令可能导致程序失去响应
*author:skyang
*version:1.0
*last modification:2007-3-24
**/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Runtime;
import java.util.*;
import java.io.*;
public class TimeMachine extends JFrame implements ActionListener,ItemListener,Runnable,KeyListener{
JRadioButton r1=null;
JRadioButton r2=null;
JButton b1=null;
JLabel lb1=null;
JLabel lb2=null;
JLabel lb3=null;
JLabel lb4=null;
JLabel lb5_pic=null;//放图片的标签
JTextField txt1=null;
JTextField txt2=null;
JComboBox c1=null;
JComboBox c2=null;
JComboBox c3=null;
Thread th;
int deadSecond=0;//倒计时多少秒后关机
int count=1;//记录已过去的秒数
TimeControl TC=null;
public TimeMachine(){
super("定时关机");
this.setBounds(200,200,400,300);
Container cp = this.getContentPane();
cp.setLayout(null);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.setBounds(0,0,100,300);
p2.setBounds(100,0,300,300);//容器设为一行两列
p2.setLayout(null);
p2.setBorder(BorderFactory.createTitledBorder("请选择一种计时模式:"));
cp.add(p1);
cp.add(p2);
r1 = new JRadioButton("倒计时:");
r2 = new JRadioButton("定时:");
r1.setBounds(0,80,100,30);
r2.setBounds(0,130,100,30);
r1.addItemListener(this);
r2.addItemListener(this);
r2.setSelected(true);
p2.add(r1);
p2.add(r2);
ButtonGroup bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
String s1[]={"10","20","30","40","50","60","90","120","150","180"
};
String s2[]={"0","1","2","3","4","5","6","7","8","9","10","11",
"12","13","14","15","16","17","18","19","20","21","22","23"
};//hour
String s3[]={"0","5","10","15","20","25","30","35","40","45","50","55"
};//minute
c1 = new JComboBox(s1);
c2 = new JComboBox(s2);
c3 = new JComboBox(s3);
c1.setBounds(100,80,100,30);
c2.setBounds(100,130,50,30);
c3.setBounds(180,130,50,30);
c1.setSelectedIndex(0);
c2.setSelectedIndex(23);
c3.setSelectedIndex(0);
p2.add(c1);
p2.add(c2);
p2.add(c3);
lb1 = new JLabel("分");
lb2 = new JLabel("时");
lb3 = new JLabel("分");
lb4 = new JLabel("当前系统时间:");
lb5_pic = new JLabel(new ImageIcon(".\\kenan.jpg"));
lb1.setBounds(210,80,30,30);
lb2.setBounds(160,130,30,30);
lb3.setBounds(250,130,30,30);
lb4.setBounds(0,35,100,40);
p1.add(lb5_pic);
p2.add(lb1);
p2.add(lb2);
p2.add(lb3);
p2.add(lb4);
txt1 = new JTextField(10);
txt2 = new JTextField(10);
txt1.setBounds(100,40,100,30);
txt2.setBounds(100,80,100,30);
txt1.setEditable(false);
txt2.setEditable(false);
txt2.setVisible(false);//txt2暂时不可见
p2.add(txt1);
p2.add(txt2);
b1 = new JButton("确定");
b1.setBounds(100,190,100,40);
b1.addActionListener(this);
p2.add(b1);
th = new Thread(this);//别忘了在构造函数中新建一个线程并启动
th.start();
TC = new TimeControl();//启动时间控制类
this.setVisible(true);
this.setResizable(false);
this.requestFocus(true);
c2.addKeyListener(this);
// this.setIconImage(Toolkit.getDefaultToolkit().createImage(
// this.getClass().getResource("Clock.jpg")));//设置标题栏的小图标
this.getRootPane().setDefaultButton(b1);//将确定设为默认按钮
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1 && b1.getText()=="确定"){
b1.setText("停止");
if(r1.isSelected()){//倒计时模式
count=1;
deadSecond=60*String_To_Int(c1.getSelectedItem().toString());
c1.setVisible(false);
lb1.setVisible(false);
txt2.setVisible(true);
}
else if(r2.isSelected()){//定时模式
deadSecond=0;
TC.setTime(String_To_Int(c2.getSelectedItem().toString()),
String_To_Int(c3.getSelectedItem().toString()));
}
}
else if(e.getSource()==b1 && b1.getText()=="停止"){
//这里应该弹出一个对话框确认是否真的取消
if(!ShutDownDialog()) return;
b1.setText("确定");
deadSecond=0;
txt2.setVisible(false);
c1.setVisible(true);
lb1.setVisible(true);
TC.setTime(-1,-1);//取消定时
}
}
public boolean ShutDownDialog(){//弹出确认对话框
int result;
result=JOptionPane.showConfirmDialog(null,"确定取消关机设置?","确认取消",
JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(result==JOptionPane.YES_OPTION) return true;
else return false;
}
public void keyTyped(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_P){
ProfessorMode pm = new ProfessorMode();
}
}
public void itemStateChanged(ItemEvent e){
}
public int String_To_Int(String str){//如果输入的字符串不能转换为整型,返回0
int sum=0;
for(int i=0;i<STR.LENGTH();I++){ str.charAt(i) || if(str.charAt(i)<48 sum="sum*10+(str.charAt(i)-48);">57) return 0;
}
return sum;
}
public void run(){//设定倒计时,刷新系统时间显示
String time;
TimeControl TC = new TimeControl();
while(th!=null){
count++;
time = TC.getTime();
txt1.setText(time);
//倒计时关机
if(count==deadSecond) TC.ShutDown();
//显示倒计时
if(deadSecond!=0){
txt2.setText(deadSecond-count+" 秒");
}
try{
th.sleep(1000);
}
catch(Exception e){
}
}
}
public static void main(String args[]){
TimeMachine myOne = new TimeMachine();
myOne.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
/**
*定时关机在这里实现,该类中的ShutDown()方法调用系统的关机命令
*
**/
class TimeControl implements Runnable{//获取系统时间,并控制关机
GregorianCalendar time;
int hour,minute,second;
int deadHour=-1,deadMinute=-1;
Thread th;
public void ShutDown(){
String cmd="cmd /C shutdown -s -t 0";
Runtime rt = Runtime.getRuntime();
try{
Process proc = rt.exec(cmd);
while (true){
if(proc.waitFor() == 0) break;
}
}
catch(Exception e){
System.out.println("something wrong!");
}
}
public TimeControl(){
th = new Thread(this);
th.start();
}
public void run(){
while(th!=null){//构建死循环的方式实现计时线程
time = new GregorianCalendar();
hour=time.get(Calendar.HOUR_OF_DAY);
minute=time.get(Calendar.MINUTE);
second=time.get(Calendar.SECOND);
if(hour==deadHour && minute==deadMinute)
ShutDown();//到点关机
try{
th.sleep(1000);
}
catch(Exception e){
}
}
}
public String getTime(){//返回系统时间的字符串
String str="";
str=hour+":"+minute+":"+second;
return str;
}
public void setTime(int hour,int minute){//设定时间,到点关机
this.deadHour=hour;
this.deadMinute=minute;
}
}
/**
*专家模式,将命令行程序搬过来,但不能完全取代命令行,有些功能无法实现
*
**/
class ProfessorMode extends JFrame implements ActionListener{
JTextArea txt1=null;
JTextField txt2=null;
JScrollPane sp=null;
JPanel p1=null;
JPanel p2=null;
public ProfessorMode(){
this.setTitle("专家模式");
this.setBounds(200,200,600,410);
Container cp = this.getContentPane();
cp.setLayout(null);
txt1 = new JTextArea();
txt2 = new JTextField(100);
txt1.setBounds(0,0,600,350);
txt2.setBounds(0,350,600,30);
txt1.setBackground(Color.BLACK);
txt1.setEditable(false);
txt1.setLineWrap(true);
txt1.setForeground(Color.white);
txt1.setFocusable(false);
txt2.requestFocus(true);
txt2.addActionListener(this);
sp = new JScrollPane(txt1);
sp.setBounds(0,0,595,350);
cp.add(sp);
cp.add(txt2);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(2);
}
public void actionPerformed(ActionEvent e){
txt1.append(">>"+txt2.getText()+"\n");
if(txt2.getText().compareToIgnoreCase("cls")==0){//输入cls将清空txt2中的内容
txt1.setText("");
txt2.setText("");
return;
}
CMD(txt2.getText());//将txt2中的命令送入命令行
txt2.setText("");
}
public void CMD(String cmd){
String inline;
Runtime r = Runtime.getRuntime();
try{
Process p = r.exec("cmd /C "+cmd);
BufferedReader br=new BufferedReader(
new InputStreamReader(p.getInputStream()));
while((inline=br.readLine())!=null){
txt1.append(inline+"\n");
}
}
catch(Exception e){
txt1.append("someting wrong!\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -