发布网友 发布时间:2022-04-19 20:07
共1个回答
热心网友 时间:2023-07-18 15:17
import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.util.*;
/**
* @author Hardneedl
*/
public class TimerApplet extends JApplet {
private final Dimension SIZE = new Dimension(100,100);
public Dimension getPreferredSize() {return SIZE;}
public Dimension getMinimumSize() {return SIZE;}
public Dimension getSize() {return SIZE;}
private boolean enabled = false;
private Date datetime;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年M月d日 HH:mm:ss");
private JComponent canvas = new JComponent(){
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.YELLOW);
g.setFont(g.getFont().deriveFont(22f));
g.drawString(dateFormat.format(datetime),40,50);
}
};
public void init() {
super.init();
Thread thread = new Thread(new _Runnable());
enabled = true;
thread.start();
setLayout(new BorderLayout());
add(canvas,BorderLayout.CENTER);
}
public void destroy() {
super.destroy();
enabled = false;
}
private class _Runnable implements Runnable {
public void run() {
if (enabled)
while (true) {
datetime = Calendar.getInstance().getTime();
canvas.paintImmediately(canvas.getBounds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}