JAVA applet application

来源:百度知道 编辑:UC知道 时间:2024/06/24 14:32:44
public class Watch extends javax.swing.JApplet{
private Color butterscotch = new Color(255,204,102);
private String lastTime = "";

public void init(){
setBackground(Color.black);
}

public void paint(Graphics screen){//paint is a method of Graphics2D, stands for component color
Graphics2D screen2D = (Graphics2D)screen; //构建一个新的Graphics2D对象
Font type = new Font("Monospaced", Font.BOLD,20);
screen2D.setFont(type);
GregorianCalendar day = new GregorianCalendar();
String time = day.getTime().toString();
screen2D.setColor(Color.black);
screen2D.drawString(lastTime,5,25);//使用grahpics2D上下文中的当前文本属性状态呈现制定的string的文本
screen2D.setColor(butterscotch);
screen2D.drawString(time,5,25);
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
lastTime = time;
repaint();

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.GregorianCalendar;

//整个思路就是每过1秒时间,获取当前时间并把当前时间绘制到容器上
public class Watch extends javax.swing.JApplet {
private Color butterscotch = new Color(255, 204, 102);
private String lastTime = "";

public void init() {
setBackground(Color.black);
}

//重写方法
//绘制容器
public void paint(Graphics screen) {
Graphics2D screen2D = (Graphics2D) screen; // 构建一个新的Graphics2D对象
Font type = new Font("Monospaced", Font.BOLD, 20);//设置字体
screen2D.setFont(type);
GregorianCalendar day = new GregorianCalendar();//获取日历对象
String time = day.getTime().toString();//获取日期字符串
screen2D.setColor(Color.black);
screen2D.drawString(lastTime, 5, 25);// 使用grahpics2D上下文中的当前文本属性状态呈现制定的string的文本
screen2D.setColor(butterscotch);
screen2D.drawStr