时间不重绘的Applet [英] Time doesn't repaint in Applet
问题描述
我有一个小问题,但我不知道要修复它。我创建简单的小程序应该是简单的数字时钟。我创建了所有的方法正确,但重绘方法不重绘我的小程序。你可以检查我的code和说得清是错误的。谢谢你。
公共类DigitalClock扩展JApplet的实现Runnable {私人螺纹timeThread;
日期日期=新的日期();公共无效的start(){
timeThread =新主题(这一点,时钟);
timeThread.start();
}@覆盖
公共无效停止(){
如果(timeThread == NULL){
返回;
}
timeThread = NULL;
}@覆盖
公共无效的run(){
而(timeThread!= NULL){
重绘();
尝试{
timeThread.sleep(1000);
}赶上(InterruptedException的E){
}
}
}@覆盖
公共无效漆(图形G){
date.setTime(System.currentTimeMillis的());
g.drawString(与Date.toString(),50,95);
}
}
只需使用 javax.swing.Timer中的
。 了解如何使用Swing定时器
下面是一个例子。但是,而不是在顶层容器油画般 JApplet的
你是干什么的,我画上的JPanel
这是比较推荐
进口java.awt中的*。
java.awt.event中导入*。
进口java.text.SimpleDateFormat的;
进口java.util.Date;
进口的javax.swing *。公共类TimerPanel继承JPanel { 私人字符串dateString;
私人最终的SimpleDateFormat格式;
私人最终字体字库;
私人最终日期日期; 公共TimerPanel(){
格式=新的SimpleDateFormat(HH:MM:SS);
字体=新的字体(影响,Font.PLAIN,30);
日期=新的日期(); date.setTime(System.currentTimeMillis的());
dateString = format.format(日期); 定时器定时器=新定时器(1000,新的ActionListener(){
@覆盖
公共无效的actionPerformed(ActionEvent的五){
date.setTime(System.currentTimeMillis的());
dateString = format.format(日期);
重绘();
}
});
timer.start();
} @覆盖
保护无效paintComponent(图形G){
super.paintComponent方法(G); g.setFont(字体);
FontMetrics对象FM = g.getFontMetrics(字体);
INT宽度= fm.stringWidth(dateString);
INT高度= fm.getAscent();
INT X =的getWidth()/ 2 - 宽/ 2;
INT Y =的getHeight()/ 2 +高/ 2; g.drawString(dateString,X,Y);
} @覆盖
公共尺寸的get preferredSize(){
返回新尺寸(300,100);
} 公共静态无效的主要(字串[] args){
SwingUtilities.invokeLater(Runnable的新(){
@覆盖
公共无效的run(){
TimerPanel定时器=新TimerPanel();
JOptionPane.showMessageDialog(
空,定时器,数字时钟,JOptionPane.PLAIN_MESSAGE);
}
});
}
}
更直接地回答你的问题,你的code工作时,我测试了一下,你忘了这样做的唯一的事情就是调用 super.paint()
。按预期这样做之后,
@覆盖
公共无效漆(图形G){
super.paint(G);
您会希望避免视频下载
虽然。您可能会注意到这会导致闪烁。这是pferred使用 javax.swing.Timer中的
$ P $。这就是为什么我用我的示例首先回答。
I have a small problem but I don't know to fix it. I create simple Applet in which should be simple digital clock. I created all methods correctly but repaint methods doesn't repaint my applet. Can you check my code and say where is mistake. Thanks.
public class DigitalClock extends JApplet implements Runnable {
private Thread timeThread;
Date date = new Date();
public void start() {
timeThread = new Thread(this, "Clock");
timeThread.start();
}
@Override
public void stop() {
if (timeThread == null) {
return;
}
timeThread = null;
}
@Override
public void run() {
while (timeThread != null) {
repaint();
try {
timeThread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
@Override
public void paint(Graphics g) {
date.setTime(System.currentTimeMillis());
g.drawString(date.toString(), 50, 95);
}
}
Just use a javax.swing.Timer
. See how to use Swing Timer
Here's an example. But rather than painting on top-level container like JApplet
which you are doing, I'm painting on JPanel
which is more recommended
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class TimerPanel extends JPanel {
private String dateString;
private final SimpleDateFormat format;
private final Font font;
private final Date date;
public TimerPanel() {
format = new SimpleDateFormat("hh:mm:ss a");
font = new Font("impact", Font.PLAIN, 30);
date = new Date();
date.setTime(System.currentTimeMillis());
dateString = format.format(date);
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
date.setTime(System.currentTimeMillis());
dateString = format.format(date);
repaint();
}
});
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(font);
FontMetrics fm = g.getFontMetrics(font);
int width = fm.stringWidth(dateString);
int height = fm.getAscent();
int x = getWidth() / 2 - width / 2;
int y = getHeight() / 2 + height / 2;
g.drawString(dateString, x, y);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 100);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TimerPanel timer = new TimerPanel();
JOptionPane.showMessageDialog(
null, timer, "Digital Clock", JOptionPane.PLAIN_MESSAGE);
}
});
}
}
More directly answering your question, your code works when I test it, the only thing you're forgetting to do is call super.paint()
. Works as expected after doing so
@Override
public void paint(Graphics g) {
super.paint(g);
You'll want to avoid Thread.sleep
though. You may notice this causes flickering. It's preferred to use javax.swing.Timer
. That's why I answered with my example first.
这篇关于时间不重绘的Applet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!