Applet中没有重绘时间 [英] Time doesn't repaint in Applet

查看:170
本文介绍了Applet中没有重绘时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,但我不知道要解决它。我创建简单的Applet,其中应该是简单的数字时钟。我正确地创建了所有方法,但重绘方法不会重绘我的小程序。你可以检查我的代码,并说错误在哪里。谢谢。

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);
}
}


推荐答案

使用 javax.swing.Timer 查看如何使用Swing Timer

Just use a javax.swing.Timer. See how to use Swing Timer

这是一个例子。而不是像您正在做的 JApplet 在顶级容器上绘画,而是在 JPanel 上绘画更多推荐

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);
            }
        });
    }
}






更直接地回答你的问题,你的代码在我测试的时候工作,你唯一忘记的是调用 super.paint()。在这样做之后按预期工作


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);

您将要避免 Thread.sleep 虽然你可能会注意到这会导致闪烁。最好使用 javax.swing.Timer 。这就是为什么我先回答我的例子。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆