可现实开机最小化托盘,可还原;并可设置定时关机。
1、MainFrame
package pretoct; import java.awt.AWTException; import java.awt.BorderLayout; import java.awt.Color; import java.awt.MenuItem; import java.awt.Panel; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.TextArea; import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; import javax.swing.Timer; import pretoct.SD; public class MainFrame extends JFrame { static TextArea ta = new TextArea(); private static TrayIcon trayIcon = null; static JFrame mf = new JFrame(); static SystemTray tray = SystemTray.getSystemTray(); public static void myFrame() { // 窗体 mf.setLocation(300, 100); mf.setSize(400, 150); mf.setTitle("定时休息系统"); mf.setLayout(new BorderLayout()); ta.setText("30"); ImageIcon ll = new ImageIcon("image/favicon.ico");//在JFrame中使用图片 JLabel i = new JLabel(ll); JLabel j = new JLabel("鼠标单击设置定时关机", SwingConstants.CENTER);//设置标签,显示文本居中 j.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ j.setForeground(Color.red); System.out.println("-------------"); SD win= new SD("定时关机程序"); Thread t=new Thread(win); t.start(); }//mouseClicked });//addMouseListener JLabel k = new JLabel("实现定时休息提醒功能,在下框中输入休息间隔时间(分)。", SwingConstants.CENTER);//设置标签,显示文本居中 j.setFont(new java.awt.Font("", 1, 18));//设置标签J显示字体 Panel p1 = new Panel();//实例化面板P1 p1.setLayout(new BorderLayout());//设置面板P1中控件排列方式 final Panel p11 = new Panel(); p11.setLayout(new BorderLayout());//设置P11控件排列方式 p11.add(j, BorderLayout.NORTH);//P11上方显示控件J p11.add(k, BorderLayout.SOUTH);//P11下方显示控件K final JLabel t = new JLabel("",SwingConstants.CENTER);//设置标签t用于显示时钟 t.setFont(new java.awt.Font("", 1, 26));//设置标签t字体 t.setForeground(Color.blue);//设置标签t前景色彩 p11.add(t,BorderLayout.CENTER);//标签t显示在中间位置 Timer timer_date = new Timer(1000,new ActionListener(){ //设置数字时钟 public void actionPerformed(ActionEvent evt) { t.setText( new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date())); }//actionPerformed }); //Timer timer_date.start(); p1.add(i, BorderLayout.WEST);//左侧图标 p1.add(p11, BorderLayout.CENTER);//p11在P1中间排列 mf.add(p1, BorderLayout.NORTH);//将p1显示在窗口上方 mf.add(ta, BorderLayout.CENTER);//将一个多行文本区域显示在文体中间 mf.setVisible(true);//使窗口可见 mf.addWindowListener(new WindowAdapter() { // 窗口关闭事件 public void windowClosing(WindowEvent e) { System.exit(0); };//windowClosing public void windowIconified(WindowEvent e) { // 窗口最小化事件 mf.setVisible(false); MainFrame.miniTray(); }//windowIconified });//addWindowListener MainFrame.setTimer(); //add 使最小化 MainFrame.miniTray(); mf.setVisible(false); //end add }//myFrame private static void miniTray() { //窗口最小化到任务栏托盘 ImageIcon trayImg = new ImageIcon("image/favicon.ico");//托盘图标 PopupMenu pop = new PopupMenu(); //增加托盘右击菜单 MenuItem show = new MenuItem("还原"); MenuItem exit = new MenuItem("退出"); show.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 按下还原键 tray.remove(trayIcon); mf.setVisible(true); mf.setExtendedState(JFrame.NORMAL); mf.toFront(); }//actionPerformed });//addActionListener exit.addActionListener(new ActionListener() { // 按下退出键 public void actionPerformed(ActionEvent e) { tray.remove(trayIcon); System.exit(0); }//actionPerformed });//addActionListener pop.add(show); pop.add(exit); trayIcon = new TrayIcon(trayImg.getImage(), "请注意休息哦!身体最重要。", pop); trayIcon.setImageAutoSize(true); trayIcon.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { // 鼠标器双击事件 if (e.getClickCount() == 2){ tray.remove(trayIcon); // 移去托盘图标 mf.setVisible(true); mf.setExtendedState(JFrame.NORMAL); // 还原窗口 mf.toFront(); }//if }//mouseClicked });//addMouseListener try { tray.add(trayIcon); } catch (AWTException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }//try }//miniTray //设置Timer private static void setTimer(){ int time=1000; //1000ms实现一次动作 实际是一个线程 long openTime=System.currentTimeMillis()/1000;//起初值 Timer timeAction = new Timer(time, new ActionListener() { public void actionPerformed(ActionEvent e) { int userRestMinute=Integer.parseInt(ta.getText())*60; long nowTime=System.currentTimeMillis()/1000; long cha=nowTime-openTime; long bi=cha%userRestMinute; if(bi==0){ new TopFrame(); //打开新界面 System.out.println("hava a reset"); }//if }//actionPer }); timeAction.start(); }//setTimer public static void main(String[] args) { new MainFrame(); MainFrame.myFrame(); }//main }//MainFrame
2、TopFrame.java
package pretoct; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRootPane; import javax.swing.JTextField; import javax.swing.Timer; public class TopFrame extends Frame{ JTextField jtf=null; int rest=100;//minutes public TopFrame(){ JFrame frame =new JFrame(); FlowLayout flow=new FlowLayout( ); frame.setLayout(flow); JLabel jlb=new JLabel("现在是休息时间,请好好休息!"); jtf=new JTextField(5); frame.add(jlb); frame.add(jtf); jtf.setText(rest+""); jtf.disable(); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setAlwaysOnTop(true); frame.setUndecorated(true); // 去掉窗口的装饰 frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);//采用指定的窗口装饰风格 frame.setVisible(true); Timer timeAction = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { rest--; jtf.setText(rest+""); if(jtf.getText().equals("0")){ System.out.println("-finished-"); frame.dispose(); }//if }//actionPerformed });//timeAction timeAction.start(); }//TopFrame }//TopFrame
3、FrameSD
package pretoct; import java.awt.*; import java.awt.event.*; import java.util.*; import java.text.SimpleDateFormat; import java.io.*; class SD extends Frame implements ActionListener,Runnable{ Label text1; TextField text2; Label text3; Label text4; Button button1 ; Button button2 ; Dialog dlg; Date nowtime; String st; boolean tf=false; SD(String s) {super(s); setLayout(new GridLayout(4,2)); text1=new Label("请输入关机时间(如 06:20): ",text1.CENTER); text2=new TextField(10); text3=new Label("现在的时间是:",text3.CENTER); nowtime=new Date(); SimpleDateFormat matter1=new SimpleDateFormat("HH 时 mm 分 ss秒 yyyy年 MM 月DD 日 E"); text4=new Label(matter1.format(nowtime)); button1=new Button("确定"); button2=new Button("取消"); add(text1); add(text2); add(text3); add(text4); add(button1); add(button2); setBounds(100,100,500,150); button1.addActionListener(this); button2.addActionListener(this); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){System.exit(0);} }); setVisible(true); validate(); } public void run(){ while(true){ nowtime=new Date(); SimpleDateFormat matter2=new SimpleDateFormat("HH 时 mm 分 ss秒 yyyy年 MM 月DD 日 E"); text4.setText((matter2.format(nowtime))); try{ Thread.sleep(1000);} catch(Exception ex){} }} public void actionPerformed(ActionEvent e) { String command=e.getActionCommand(); if(command.equals("确定")){ st=text2.getText(); int tj=st.length(); if(st.equals("")||tj<5){ final Dialog d=new Dialog(this,"错误",true); d.add(new Label("你输入的格式错误!请从新输入!例如08:30",Label.CENTER)); d.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){d.dispose();} }); d.setSize(300,100); d.setVisible(true); }else{ dlg=new Dialog(this,"关机确定",true); Panel p=new Panel(); p.setLayout(new GridLayout(1,2)); Button button3=new Button("是"); Button button4=new Button("否"); p.add(button3); p.add(button4); button3.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent s){ try{ Runtime rt=Runtime.getRuntime(); String cmd= " C:/Windows/System32/at "+st+" shutdown.exe -s -t 20"; rt.exec(cmd);}catch(IOException e){} dlg.dispose();} }); button4.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent s){ dlg.dispose();} }); dlg.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){dlg.dispose();} }); dlg.add(new Label("你确定在"+st+"时关机?在cmd中使用at id /delete可放弃任务。"),"Center"); dlg.add(p,"South"); dlg.setBounds(150,200,200,100); dlg.setVisible(true); }} else if(command.equals("取消")){ this.dispose(); } } } /* public class FrameSD{ public static void main(String arge[]){ SD win= new SD("定时关机程序"); Thread t=new Thread(win); t.start(); }//main }//FrameSD*/