📄 jlabeltime1.java
字号:
package common.currenttime;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class JLabelTime1 extends JLabel{
private static final long serialVersionUID = 1L;
private final static int ONE_SECOND = 1000;
public String timeStyle = "";
public JLabelTime1(){}
public JLabelTime1(String text) {
this.setBounds(0, 0, 400, 400);
// this.setBackground(Color.red);
this.setFont(new Font(null,Font.BOLD,15));
timeStyle = "yyyy-MM-dd HH:mm:ss";
timer.start();
}
Timer timer = new Timer(ONE_SECOND, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
setCurrTimeToText();
}
});
public void setCurrTimeToText() {
this.setText(getCurrTime(timeStyle));
}
public String getCurrTime(String timeStyle) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat fromatter = new SimpleDateFormat(timeStyle);
String strCurrTime = fromatter.format(cal.getTime());
return strCurrTime;
}
public static void main(String[] args) {
JLabelTime time = new JLabelTime("显示时间");
JFrame mainFrame = new JFrame();
mainFrame.setContentPane(time);
mainFrame.setSize(500, 400);
mainFrame.setVisible(true);
System.out.println(new JLabelTime1().getCurrTime("yyyy-MM-dd HH:mm:ss"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -