JAVA桌面提示,定时学习

本文介绍了一个简单的桌面提示应用开发过程,该应用从指定路径下的文本文件中读取内容,并按计划任务定时展示这些内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package content;

public class Content {
 private int index;
 private String content;
 private int width;
 private int height;
 
 public int getIndex() {
  return index;
 }
 public void setIndex(int index) {
  this.index = index;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
 public int getWidth() {
  return width;
 }
 public void setWidth(int width) {
  this.width = width;
 }
 public int getHeight() {
  return height;
 }
 public void setHeight(int height) {
  this.height = height;
 }
}

 

 

package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import content.Content;

import mytask.ShowDeskTipTask;
import myutil.Constant;
import myutil.FileUtil;

public class FrameDemo {

 static JDialog myFrame;
 
 static List<Content> contentList;

 private static int index;
 
 public static int getIndex() {
  return index;
 }

 public static void setIndex(int index) {
  FrameDemo.index = index;
 }
 
 public static JDialog getMyFrame() {
  return myFrame;
 }
 
 public static List<Content> getContentList() {
  return contentList;
 }

 public static void setContentList(List<Content> contentList) {
  FrameDemo.contentList = contentList;
 }

 public JDialog initMyFrame()
 {
  myFrame = new JDialog();
  myFrame.setUndecorated(true);// 不显示窗口边框和标题
  myFrame.setSize(300, 100);
  myFrame.setLayout(null);
  final JLabel jLabel = new JLabel("X");// 放在右上角做关闭按钮
  jLabel.setFont(new Font("宋体", 0, 14));
  myFrame.getContentPane().setBackground(new Color(255, 255, 255));
  JPanel p = ((JPanel) myFrame.getContentPane());
  p.setBorder(new LineBorder(new java.awt.Color(10, 110, 10), 1, false));
//  myFrame.setBounds(
//    Toolkit.getDefaultToolkit().getScreenSize().width - 305,
//    Toolkit.getDefaultToolkit().getScreenSize().height - 135, 300,
//    100);
  myFrame.setBounds(
    Toolkit.getDefaultToolkit().getScreenSize().width/2 - 200,
    Toolkit.getDefaultToolkit().getScreenSize().height/2, 300,
    100);
  myFrame.getContentPane().add(jLabel);
  jLabel.setBounds(280, 0, 20, 20);
  myFrame.setVisible(true);
  myFrame.setAlwaysOnTop(true);//保持在桌面最顶层
  myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

  jLabel.addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent e) {
    myFrame.dispose();
   }

   public void mouseEntered(MouseEvent e) {
    super.mouseEntered(e);
    jLabel.setForeground(Color.red);
   }

   public void mouseExited(MouseEvent e) {
    super.mouseExited(e);
    jLabel.setForeground(Color.BLACK);
   }

  });
  return myFrame;
 }
 
 public void setLocation(int x,int y)
 {
  myFrame.setLocation(x, y);
 }
 
 public void setSize(int width, int height)
 {
  myFrame.setSize(width, height);
 }
 
 public void setSize(Dimension d)
 {
  myFrame.setSize(d);
 }
 
 public Dimension getDynamicSize()
 {
  
  Dimension d = new Dimension(10, 10);
  
  return d;
 }

 public static void main(String[] args) {
  FrameDemo f = new FrameDemo();
  f.initMyFrame();
  List<Content> l = FileUtil.getContentList(Constant.getPath(), Constant.getSuffix());
  FrameDemo.setContentList(l);
  
  Timer timer = new Timer(true);
  TimerTask task = new ShowDeskTipTask();
  timer.scheduleAtFixedRate(task, 2000, 10 * 60 * 1000);

 }
}

 

 

package mytask;

import java.util.TimerTask;

import javax.swing.JDialog;

public class HideDeskTipTask extends TimerTask {

 private JDialog myFrame;
 public HideDeskTipTask(JDialog myFrame)
 {
  this.myFrame = myFrame;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  myFrame.setVisible(false);
 }

}

 

 

package mytask;

import java.awt.Font;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JDialog;
import javax.swing.JTextArea;

import main.FrameDemo;
import content.Content;

public class ShowDeskTipTask extends TimerTask{

 
 @Override
 public void run() {
  JDialog myFrame = FrameDemo.getMyFrame();
  int index = FrameDemo.getIndex();
  List<Content> l = FrameDemo.getContentList();
  if(l != null && l.size() > 0)
  {
   Content content = l.get(index);
   FrameDemo.setIndex(index + 1);
   
   JTextArea jLabelText = new JTextArea(content.getContent());
   jLabelText.setFont(new Font("宋体", 0, 14));
   if(myFrame.getContentPane().getComponentCount() > 1)
   {
    myFrame.getContentPane().remove(1);
   }
   
   myFrame.getContentPane().add(jLabelText);
   jLabelText.setBounds(10, 20, content.getWidth() * 15, content.getHeight());
   myFrame.setVisible(true);
   
   Timer t = new Timer();
   HideDeskTipTask hdttask = new HideDeskTipTask(myFrame);
   t.schedule(hdttask, 5000);
  }
 }

}

 

 

package myutil;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;


public class Constant {
 
  private static final Logger _LOG = Logger.getLogger(Constant.class);
  public static final String PROPERTIES_FILE_NAME = "config.properties";
  private static Properties properties = new Properties();
  private static String path = "";
  private static String suffix = "";
  public static String getSuffix() {
   return suffix;
  }

  private static final int HEIGHT = 25;
  private static final int WIDTH = 15;

  public static int getWidth() {
   return WIDTH;
  }

  public static int getHeight() {
   return HEIGHT;
  }

  public static String getPath() {
   return path;
  }

  static {
   InputStream in = Constant.class.getClassLoader().getResourceAsStream(
     PROPERTIES_FILE_NAME);
   if (in == null) {
    _LOG.error("in is null");
   }
   try {
    properties.load(in);
    
    path = properties.getProperty("path");
    suffix = properties.getProperty("suffix");

   } catch (FileNotFoundException ex) {
    _LOG.error("没有找到接口参数文件:" + PROPERTIES_FILE_NAME);
   } catch (IOException ex) {
    _LOG.error("加载接口参数文件出错:" + PROPERTIES_FILE_NAME, ex);
   } finally {
    try {
     in.close();
    } catch (IOException ex) {
     _LOG.error("关闭接口参数文件失败。", ex);
    }
   }
  }
}

 

 

package myutil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import content.Content;

public class FileUtil {
 private static final Logger _LOG = Logger.getLogger(FileUtil.class);
 public static List<Content> getContentList(String path, String suffix)
 {
  List<Content> l = new ArrayList<Content>();
  ContentFilenameFilter filenameFilter = new ContentFilenameFilter();

  filenameFilter.setFilter(suffix);
  File file = new File(path);

  if (!file.exists()) {
   _LOG.error("文件 " + path + " 不存在!");
  }
  
  if(!file.isDirectory())
  {
   BufferedReader reader = null;
         try {
             System.out.println("以行为单位读取文件内容,一次读一整行:");
             reader = new BufferedReader(new FileReader(file));
             String tempString = null;
             int line = 1;
             // 一次读入一行,直到读入null为文件结束
             while ((tempString = reader.readLine()) != null) {
              if(tempString.trim().length() > 0)
              {
               // 显示行号
                  System.out.println("line " + line + ": " + tempString);
                  Content content = new Content();
                  content.setContent(getContentByWidth(tempString));
                  content.setHeight(getContentHeight(tempString));
                  content.setWidth(Constant.getWidth());
                  l.add(content);
              }
                 line++;
             }
             reader.close();
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             if (reader != null) {
                 try {
                     reader.close();
                 } catch (IOException e1) {
                 }
             }
         }
  }

  return l;
 }
 
 static class ContentFilenameFilter implements FilenameFilter {
  private String suffix;

  public void setFilter(String filter) {
   this.suffix = filter;
  }

  public boolean accept(File dir, String name) {
   return name.toUpperCase().endsWith(this.suffix.toUpperCase());
  }
 }
 
 public static String getContentByWidth(String content)
 {
  int length = content.length();
  int width = Constant.getWidth();
  String temp = "";
  if(length > width)
  {
   int row = (length % width != 0) ? length/width +1 : length/width;
   
   for(int i = 0;i < row - 1;i++)
   {
    temp += content.substring(width * i, width * (i + 1)) + System.getProperty( "line.separator");
   }
   temp += content.substring(width * (row - 1));
  }
  else
  {
   temp = content;
  }
  return temp;
 }
 
 public static int getContentHeight(String content)
 {
  int length = content.length();
  int width = Constant.getWidth();
  int row = 1;
  if(length > width)
  {
   row = (length % width != 0) ? length/width +1 : length/width;
  }
  return row * Constant.getHeight();
 }
}

 

config.properties

path=D\:\\personal\\life\\\u65F6\u5C1A\u8BED\u5F55.txt
suffix=txt

 

log4j.properties

 

# For the general syntax of property based configuration files see the
# documenation of org.apache.log4j.PropertyConfigurator.

# The root category uses the appender called A1. Since no priority is
# specified, the root category assumes the default priority for root
# which is DEBUG in log4j. The root category is the only category that
# has a default priority. All other categories need not be assigned a
# priority in which case they inherit their priority from the
# hierarchy.

log4j.rootLogger=ALL,stdout,R

# A1 is set to be a LF5Appender which outputs to a swing
# logging console.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.threshold=DEBUG
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= [%d{ yyyy-MMM-dd HH:mm:ss }] <%-3p> :: %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=D\:\\WnmsNetCellLog.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%d{ yyyy-MMM-dd HH:mm:ss }] <%-3p> :: %m%n

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值