这是我的代码public class myapplet extends Applet { public void paint(Graphics g) {
Date date=new Date();
g.drawString(date.toString(),20,50);
repaint(1000);
}
public void update(Graphics g){ paint(g); }
}重写update解决了闪烁问题 但是画面没有刷新 新的画面和原先的都重叠在一起了 请问怎么解决啊?
Date date=new Date();
g.drawString(date.toString(),20,50);
repaint(1000);
}
public void update(Graphics g){ paint(g); }
}重写update解决了闪烁问题 但是画面没有刷新 新的画面和原先的都重叠在一起了 请问怎么解决啊?
import java.util.*;
import java.awt.*;public class myapplet extends Applet
{
private Image bgImage;
private Graphics bg;public void paint(Graphics g)
{
Date date=new Date();
g.drawString(date.toString(),20,50);
repaint(1000);
}
public void update (Graphics g)
{
if (bgImage == null)
{
bgImage = createImage (this.getSize().width, this.getSize().height);
bg = bgImage.getGraphics ();
}
bg.setColor (getBackground ());
bg.fillRect (0, 0, this.getSize().width, this.getSize().height);
bg.setColor (getForeground());
paint (bg);
g.drawImage (bgImage, 0, 0, this);
}}
没repaint根本不刷新时间啊。。
我定义了这两个东西
private Image bgImage;
private Graphics bg;
bg由bgImage得到
我把要画的东西先都画到bg上,也就是改变bgImage
然后把这个bgImage画到myapplet的Graphics g上
bgImage的大小和你的myapplet一样大
就相当于我把一张和你的myapplet一样大的纸直接贴到你的myapplete上
原来myapplet上的东西都被压到这张纸下面了,你看不见了
import java.util.Date;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;/**
* TimerFrame - 倒计时窗口
* @author SageZk
* @version 1.0
*/
@SuppressWarnings("serial")
public class TimerFrame extends JFrame { private long time = (long) (1.2 * 60 * 1000L); //倒计时时间(单位毫秒)
private JLabel lblTime;
private Thread runner; public TimerFrame() {
super("TimerFrame");
this.lblTime = new JLabel("单击开始");
this.lblTime.setFont(new Font("Monospaced", Font.BOLD, 60));
this.lblTime.setHorizontalAlignment(SwingConstants.CENTER);
this.lblTime.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (runner != null) runner.start();
}
});
this.runner = new Thread(new Runnable() {
public void run() {
Date t = new Date();
StringWriter sw = new StringWriter();
StringBuffer sb = sw.getBuffer();
PrintWriter pw = new PrintWriter(sw);
long cur = 0L, end = System.currentTimeMillis() + time;
while ((cur = end - System.currentTimeMillis()) > 0) {
t.setTime(cur);
pw.format("%1$tM:%1$tS.%tL", t);
pw.flush();
lblTime.setText(sb.toString());
sb.setLength(0);
try {
Thread.sleep(6L);
} catch (InterruptedException e) {
}
}
lblTime.setText("00:00.000");
try {
Thread.sleep(1200L);
} catch (InterruptedException e) {
}
lblTime.setText("Bomb!!!");
}
});
this.runner.setDaemon(true);
getContentPane().add(this.lblTime, BorderLayout.CENTER);
setResizable(false);
setBounds(0, 0, 460, 330);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new TimerFrame().setVisible(true);
}}