⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listenerthread.java

📁 老师布置的一个作业,是关于记时的,当上网时间快结束时提醒用户
💻 JAVA
字号:
/*
 * 创建日期 2006-3-25
 */
import java.awt.*;
import java.io.*;
import java.util.*;
/**
 * @author 肖亮庆
 * 
 * 上网计时器
 */

public class ListenerThread extends Thread {
	public static String address = "ping localhost";
	//总时间、已用时间、计时
    public static int allTime , lastTime , add;
    public static boolean reply = true;
	public static MainWindow mw = new MainWindow("警告窗口");
	public static 	MessageBox message = new MessageBox(mw);
	
	public void run() {
		//获得网络联接的时间点
		lastTime = this.getTime();
		//显示已用时间
		int hour = lastTime/3600;
		mw.setVisible(false);
		message.show("本月您的上网时间已达" + String.valueOf(hour) + "小时");
		//每一秒钟ping一次指定地址
		while(true) {
			reply = this.ping();
			try{
				this.sleep(1000);
			}catch(InterruptedException e){
				System.out.println(e.getMessage());
			}
		}
		
	}
	/*
	 * ping指定的地址,获得返回值
	 */
	public boolean ping() {
		try{
			Process process = Runtime.getRuntime().exec(address);
			InputStream inputStream = process.getInputStream();
			InputStreamReader streamReader = new InputStreamReader(inputStream);
			BufferedReader reader = new BufferedReader(streamReader);
			String result = reader.readLine();
			if(result.endsWith("out.")) {
				reply = false;
			}else if(result.startsWith("Reply")){
				reply = true;
			}
			//关闭流
			reader.close();
			streamReader.close();
			inputStream.close();
		}catch(Exception e){
			return reply = false;
		}
		return reply;
	}
	/*
	 * 计时器
	 */
	public static void timer() {
		add++;//时间加一钞
		allTime = lastTime + add;
		//判断是否到月,到月计时清零
		Date now = new Date();
		if(now.getDay() == 1 && now.getHours() == 0 && now.getMinutes() == 0 && now.getSeconds() == 0)
			allTime = 0;
		saveTime(allTime);
	}
	/*
	 * 保存时间
	 */
	public static void saveTime(int minute) {
        try{
        	File file = new File("save.txt");
            FileOutputStream outputFile = new FileOutputStream(file);
            DataOutputStream out = new DataOutputStream(outputFile);
            out.writeInt(minute);
            //关闭输入流
            out.close();
            outputFile.close();
        }catch(IOException e) {
            System.out.println(e.getMessage());
        }
    }
	/*
	 * 获取时间
	 */
	public static int getTime() {
		File file = new File("save.txt");
        if(file.exists()) {
        	try{
            	
                FileInputStream inputFile = new FileInputStream(file);
                DataInputStream dataStream = new DataInputStream(inputFile);
                lastTime = inputFile.read();
                inputFile.close();
            }catch(IOException e) {
                System.out.println(e.getMessage());
            }
        }
		
        return lastTime;
    }
	/*
	 * 提示用户本月总上网时间
	 */
	public static void warmer() {
		int time = getTime();
		if(time == 100800){
			message.show("警告,您本月的上网时间已超过28小时!");
		}else if(time == 104400) {
			message.show("警告,您本月的上网时间已超过29小时!");
		}else if(time == 106200) {
			message.show("警告,您本月的上网时间只剩下半小时!");
		}else if(time == 107940) {
			message.show("警告,您本月的上网时间只剩下一分钟!");
		}else if(time == 108000) {
			message.show("警告,您本月的上网时是已到!");
		}
	}
	
	public static void main(String [] args) {
		ListenerThread timeThread = new ListenerThread();
		timeThread.start();
		//设置窗体属性
		mw.setVisible(false);
		message.setBackground(Color.DARK_GRAY);
		message.setFont(new Font("宋体",Font.BOLD,30));
		message.setTitle("警告窗口");
		message.setForeground(Color.RED);
		
		while(true) {
			try{
				Thread.sleep(1000);
				if(reply == true)
					timer();//调用计时器
			}catch(InterruptedException e) {
				System.out.println(e.getMessage());
			}
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -