码迷,mamicode.com
首页 > 编程语言 > 详细

java基础知识(三)

时间:2015-08-15 16:15:46      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

  • 你好
      • 代码
    package com.lky;
    
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.util.Calendar;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    public class closeComputer extends JFrame
    {
    	private JPanel panel_main = new JPanel(new BorderLayout(5, 10));
    	private JPanel panel_subnorth = new JPanel(new FlowLayout(3));
    	private JPanel panel_subcenter = new JPanel(new FlowLayout(1, 5, 5));
    	private JButton countdown = new JButton("倒计时关机");
    	private JButton time = new JButton("定时关机");
    	private JButton cancel = new JButton("取消关机");
    	private JLabel tag;
    	String key;
    
    	public closeComputer()
    	{
    		this.getContentPane().add(panel_main);
    
    		panel_main.add(panel_subnorth, BorderLayout.NORTH);
    		panel_main.add(panel_subcenter, BorderLayout.CENTER);
    		panel_subnorth.add(tag = new JLabel("请选择关机方式:"));
    		panel_subcenter.add(countdown);
    		panel_subcenter.add(time);
    		panel_subcenter.add(cancel);
    
    		countdown.addActionListener(new Button1Hander());
    		time.addActionListener(new Button2Hander());
    		cancel.addActionListener(new Button3Hander());
    	}
    
    	public void time()
    	{
    		Calendar calendar = Calendar.getInstance();
    		int h = calendar.get(Calendar.HOUR);
    		int m = calendar.get(Calendar.MINUTE);
    		int s = calendar.get(Calendar.SECOND);// 获取当前系统时间
    
    //		String a = String.valueOf(h);
    
    		int hour, minute, secord;
    		String hourtmp, minutetmp, secordtmp;
    
    		hourtmp = JOptionPane.showInputDialog(this, "请输入关机的小时(12小时制)", "输入", 1);
    		minutetmp = JOptionPane.showInputDialog(this, "请输入关机的分钟", "输入", 1);
    		secordtmp = JOptionPane.showInputDialog(this, "请输入关机的秒钟", "输入", 1);
    
    		hour = Integer.parseInt(hourtmp);
    		minute = Integer.parseInt(minutetmp);
    		secord = Integer.parseInt(secordtmp);
    
    		long set_time = timesum(hour, minute, secord);
    		long currently_time = timesum(h, m, s);
    
    		long discrepancy_time = set_time - currently_time;
    		if (discrepancy_time < 0)
    		{
    			try
    			{
    				Runtime.getRuntime().exec("shutdown -s");
    			} catch (IOException e)
    			{
    				e.printStackTrace();
    			}
    		} else
    		{
    			countTimeTool.deleyTime(discrepancy_time);
    			JOptionPane.showMessageDialog(this, "恭喜你,设置成功!", "确认", 2);
    		}
    	}
    
    	private class Button1Hander implements ActionListener
    	{
    
    		@Override
    		public void actionPerformed(ActionEvent arg0)
    		{
    			countdown();
    		}
    
    	}
    
    	private class Button2Hander implements ActionListener
    	{
    
    		@Override
    		public void actionPerformed(ActionEvent arg0)
    		{
    			time();
    		}
    
    	}
    
    	private class Button3Hander implements ActionListener
    	{
    
    		@Override
    		public void actionPerformed(ActionEvent arg0)
    		{
    			cancel();
    		}
    
    	}
    
    	public void countdown()
    	{
    		key = JOptionPane.showInputDialog(this, "请输入倒计时关机的时间(秒)", "输入框", 1);
    		countTimeTool.deleyTime(Long.parseLong(key));
    	}
    
    	public int timesum(int h, int m, int s)
    	{
    		int sum = h * 3600 + m * 60 + s;
    		return sum;
    	}// 计算出时间总和,并返回
    
    	public void cancel()
    	{
    		try
    		{
    			JOptionPane.showMessageDialog(this, "你已经成功取消了关机操作!", "消息", 2);
    			Runtime.getRuntime().exec("shutdown -a");
    		} catch (IOException e)
    		{
    		}
    	}
    
    	public static void main(String[] args)
    	{
    		closeComputer frame = new closeComputer();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.pack();
    		frame.setSize(320, 120);
    		frame.setTitle("关机工具");
    		frame.setLocation(350, 350);
    		frame.setVisible(true);
    		frame.setResizable(false);
    	}
    
    }
    

java基础知识(三)

标签:

原文地址:http://www.cnblogs.com/dmir/p/4732508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!