为什么背景色设置后没有改变
import java.awt.*;
import java.util.*;
public class Clock 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;
Font type=new Font( "Monospaced ",Font.BOLD,20);
screen2D.setFont(type);
GregorianCalendar day=new GregorianCalendar();
String nowTime=day.getTime().toString();
screen2D.setColor(Color.black);
screen2D.drawString(lastTime,5,25);
screen2D.setColor(butterscotch);
screen2D.drawString(nowTime,5,25);
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
}
lastTime=nowTime;
repaint();
}
}
------解决方案--------------------不知道是不是这个意思
import java.awt.*;
import java.util.*;
public class Clock extends javax.swing.JApplet{
private Color butterscotch=new Color(111,111,111);
private String lastTime= " ";
public void init(){
setBackground(Color.BLACK);
}
public void changeColor(Color color){
setBackground(color);
}
public void paint(Graphics screen){
Graphics2D screen2D=(Graphics2D)screen;
Font type=new Font( "Monospaced ",Font.BOLD,10);
screen2D.setFont(type);
GregorianCalendar day=new GregorianCalendar();
String nowTime=day.getTime().toString();
screen2D.drawString(lastTime,5,25);
screen2D.setColor(butterscotch);
screen2D.drawString(nowTime,5,25);
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
}
this.changeColor(Color.blue);
this.setVisible(false);
this.setVisible(true);
lastTime=nowTime;
repaint();
}
}