您的位置:首页 > 产品设计 > UI/UE

GUI实现显示当前时间

2016-04-19 19:32 507 查看
“`

package com.westos.javase.lesson08;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.io.File;

import javax.swing.JLabel;

import javax.swing.JWindow;

import javax.swing.Timer;

public class MyClock extends JWindow implements ActionListener {

JLabel lc = new JLabel();

Timer timer = new Timer(1000, this); // 定时器

public MyClock() {
lc.setHorizontalAlignment(JLabel.CENTER);
lc.setVerticalAlignment(JLabel.CENTER);
lc.setText(String.format("%tT", System.currentTimeMillis()));
// 液晶字体
Font f = null;
try {
f = Font.createFont(Font.TRUETYPE_FONT, new File("FX-LED002.TTF"));
f = f.deriveFont(74f);
} catch (Exception e) {
e.printStackTrace();
f = new Font("黑体", Font.BOLD, 60);
}

lc.setFont(f);

lc.setOpaque(true);
lc.setForeground(Color.RED);
lc.setBackground(Color.BLACK);
add(lc);
setSize(300, 150);
//  setDefaultCloseOperation(EXIT_ON_CLOSE);
// 启动定时器
timer.start();

//双击关闭
this.addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {
//System.out.println(e.getButton());
if(e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) { //左键双击
dispose();
System.exit(0);
}
}

});

}

public static void main(String[] args) {
MyClock clock = new MyClock();
// 窗口居中
Dimension di = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕大小
System.out.println(di.width + "*" + di.height);
int x = (di.width - clock.getWidth()) / 2;
int y = (di.height - clock.getHeight()) / 2;
clock.setLocation(x, y);

clock.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("aaaaaaaaaa");
lc.setText(String.format("%tT", System.currentTimeMillis()));
}


}

“`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: