📄 libraryclock.java
字号:
package lib_source;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;
import javax.swing.*;
public class LibraryClock extends JFrame implements Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
Thread clock;
public LibraryClock(){
super("系统时钟");
setResizable(false);
start();
setFont(new Font("Times New Roman",Font.BOLD,60)); //设置时钟的显示字体
setSize(280,100); //设置窗口尺寸
setVisible(true);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
Dimension screen = getToolkit().getScreenSize();
setLocation((screen.width - getSize().width) / 2,
(screen.height - getSize().height) / 2);
}
public void start(){
if (clock==null){
clock=new Thread(this);
clock.start();
}
}
public void run(){
while (clock!=null){
repaint();
try{
Thread.sleep(1000);
}
catch (InterruptedException ex){
JOptionPane.showMessageDialog(this,
"多线程错误!");
}
}
}
public void stop(){
clock=null;
}
public void paint(Graphics g){
// Graphics2D g2=(Graphics2D)g;
Calendar now=new GregorianCalendar();
String timeInfo="";
int hour=now.get(Calendar.HOUR_OF_DAY);
int minute=now.get(Calendar.MINUTE);
int second=now.get(Calendar.SECOND);
if (hour<=9)
timeInfo+="0"+hour+":";
else
timeInfo+=hour+":";
if (minute<=9)
timeInfo+="0"+minute+":";
else
timeInfo+=minute+":";
if (second<=9)
timeInfo+="0"+second;
else
timeInfo+=second;
g.setColor(Color.white); //设置当前颜色为白色
Dimension dim=getSize(); //得到窗口尺寸
g.fillRect(0,0,dim.width,dim.height); //填充背景色为白色
g.setColor(Color.orange); //设置当前颜色为橙色
g.drawString(timeInfo,20,80); //显示时间字符串
}
public static void main(String[] args){
new LibraryClock();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -