Java数字时钟(简单的桌面应用)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

import java.util.*;
/*****该程序是一个简单的数字时钟,每变化一秒,颜色随机变色,可以系统托盘,最大的特点是可以和桌面形成一体,
也就是容纳这个数字时钟的窗体可以看成是透明的***********/
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
public class app509 extends JFrame{
static int i=-1,geli=-1;
/*这一部分是系统托盘图标的文件路径,可以自己设置,只要路径正确即可,可以是gif,jpg,png格式*/
static Image image=Toolkit.getDefaultToolkit().getImage("D:1\\3.png");
static SystemTray systemTray=SystemTray.getSystemTray();
static PopupMenu pop01=new PopupMenu();
static MenuItem MI01=new MenuItem("打开主程序");
static MenuItem MI02=new MenuItem("退出程序");
static MenuItem MI03=new MenuItem("隐藏");
static TrayIcon trayIcon=new TrayIcon(image,"这是程序图标",pop01);
static JLabel beijingtu=new JLabel();
static JLabel xingqi=new JLabel(" ",JLabel.CENTER);
static JLabel jlabel02=new JLabel("年",JLabel.CENTER);
static JLabel jlabel03=new JLabel("月",JLabel.CENTER);
static JLabel jlabel04=new JLabel("日",JLabel.CENTER);
static JLabel jlabel05=new JLabel("分",JLabel.CENTER);
static JLabel jlabel10=new JLabel("时",JLabel.CENTER);
static JLabel jlabel12=new JLabel("分",JLabel.CENTER);
static JLabel jlabel13=new JLabel("秒",JLabel.CENTER);
static JLabel jlabel06=new JLabel(" ",JLabel.CENTER);
static JLabel jlabel07=new JLabel(" ",JLabel.CENTER);
static JLabel jlabel08=new JLabel(" ",JLabel.CENTER);
static JLabel jlabel09=new JLabel(" ",JLabel.CENTER);
static JLabel jlabel11=new JLabel(" ",JLabel.CENTER);
static JLabel jlabel=new JLabel(" ",JLabel.CENTER);
static JLabel jbData[]=
{jlabel13,jlabel02,jlabel03,jlabel04,jlabel10,jlabel12};
static JLabel jbData02[]=
{jlabel,jlabel07,jlabel08,jlabel09,jlabel11,jlabel06};
static int mill=0;
static int minute=0;
static int hour=0;
static int day=0;
static int month=0;
static int year=0;
static int week;static int zuobiaoX,zuobiaoY;
static JFrame JF01=new JFrame();
static JDialog JF=new JDialog(JF01," ");
static Robot robot;
static BufferedImage image1;
static Rectangle rec;
static class mouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent a){
if(a.getSource()==trayIcon){
if(a.getClickCount()==2){
i++;
if(i%2==1){geli++;if(geli%2==1){
image1=robot.createScreenCapture(rec);
beijingtu.setIcon(new ImageIcon(image1));
JF.setBounds(0,0,120,560);JF.setVisible(true);}}
else{JF.setBounds(0,0,400,1);}
}}}
public void mouseEntered(MouseEvent a){
if(a.getSource()==JF){
image1=robot.createScreenCapture(rec);
beijingtu.setIcon(new ImageIcon(image1));
JF.setBounds(0,0,120,560);JF.setVisible(true);}}
public void mouseExited(MouseEvent a){
if(a.getSource()==JF){JF.setBounds(0,0,400,1);}}
}
public static void main(String args[]) throws Exception
{
trayIcon.addMouseListener(new mouseListener());
rec=new Rectangle(0,0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),
(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
try{robot=new Robot(); }catch(Exception b){}
image1=robot.createScreenCapture(rec);
beijingtu.setIcon(new ImageIcon(image1));
MI01.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent a)
{
Image1=robot.createScreenCapture(rec);beijingtu.setIcon(new ImageIcon(image1));
JF.setBounds(0,0,120,560);JF.setVisible(true);}});
MI03.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){JF.setBounds(0,0,400,1);}});
MI02.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){System.exit(0);}});
try{
pop01.add(MI01);pop01.add(MI03);pop01.add(MI02);
systemTray.add(trayIcon);
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new mouseListener());}catch(Exception a){} JF.setResizable(false) ;
JF.addMouseListener(new mouseListener());
JF.setUndecorated(true);
beijingtu.setBounds(0,0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),
(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
JF.setLayout(null);
JF.setBounds(0,0,120,560);
JF.setVisible(true);
jlabel02.setBounds(91,94,24,25);
jlabel06.setBounds(15,94,64,28);
jlabel03.setBounds(91,175,24,25);
jlabel07.setBounds(2,125,86,75);
jlabel04.setBounds(91,261,24,25);
jlabel08.setBounds(2,210,86,75);
jlabel10.setBounds(91,346,24,25);
jlabel09.setBounds(2,296,86,75);
jlabel11.setBounds(2,382,86,75);
jlabel12.setBounds(91,433,24,25);
jlabel13.setBounds(91,520,24,25);
jlabel.setBounds(2,468,86,75);
xingqi.setBounds(2,30,118,62);JF.add(xingqi);xingqi.setHorizontalTextPosition(JLabel.CENTER);
xingqi.setFont(new Font("微软雅黑",Font.BOLD,20));
for(int i=0;i<jbData.length;i++){JF.add(jbData[i]);JF.add(jbData02[i]);}
for(int i=0;i<jbData.length;i++){jbData[i].setFont(new Font("微软雅黑",Font.BOLD,15));
jbData02[i].setFont(new Font("微软雅黑",Font.BOLD,30));}
jlabel06.setFont(new Font("微软雅黑",Font.BOLD,15));
for(int i=0;i<jbData.length;i++){jbData[i].setForeground(Color.blue);
jbData02[i].setForeground(Color.red);}
for(int i=0;i<jbData.length;i++){jbData[i].setHorizontalTextPosition(JLabel.CENTER);
jbData02[i].setHorizontalTextPosition(JLabel.CENTER);}
jlabel02.setHorizontalTextPosition(JLabel.RIGHT);
JF.add(beijingtu);
xiancheng xiancheng01=new xiancheng();
xiancheng01.start();
}
}
class xiancheng extends Thread
{
static GregorianCalendar date=new GregorianCalendar();
app509 app=new app509();
public void run()
{
for(int i=0;i<60;){
try{sleep(1000);}catch(Exception a){}
app.year=(date=new GregorianCalendar()).get(date.YEAR);
app.jlabel06.setText(Integer.toString(app.year));
app.month=((date=new GregorianCalendar()).get(date.MONTH)+1);
app.jlabel07.setText(Integer.toString(app.month));
app.day=(date=new GregorianCalendar()).get(date.DAY_OF_MONTH);
app.jlabel08.setText(Integer.toString(app.day));
app.week=(date=new GregorianCalendar()).get(date.DAY_OF_WEEK);
app.hour=(date=new GregorianCalendar()).get(date.HOUR_OF_DAY);
app.jlabel09.setText(Integer.toString(app.hour));
app.minute=(date=new GregorianCalendar()).get(date.MINUTE);
app.jlabel11.setText(Integer.toString(app.minute));
l=(date=new GregorianCalendar()).get(date.SECOND);
app.jlabel.setText(Integer.toString(l));
if(app.jlabel.getText()!=" "){
app.xingqi.setForeground(
new Color((int)(255*Math.random()),(int)(255*Math.random()),(int)(255*Math.random())));
for(int j=0;j<app.jbData.length;j++){
app.jbData[j].setForeground(
new Color((int)(255*Math.random()),(int)(255*Math.random()),(int)(255*Math.random())));
app.jbData02[j].setForeground(
new Color((int)(255*Math.random()),(int)(255*Math.random()),(int)(255*Math.random())));}} switch(app.week)
{
case 1 : app.xingqi.setText("星期日");break;
case 2 : app.xingqi.setText("星期一");break;
case 3 : app.xingqi.setText("星期二");break;
case 4 : app.xingqi.setText("星期三");break;
case 5 : app.xingqi.setText("星期四");break;
case 6 : app.xingqi.setText("星期五");break;
case 7 : app.xingqi.setText("星期六");break;
}
System.gc();
}
}
}
/****复制以上代码进行编译即可*****/
程序效果图:。

相关文档
最新文档