
编写一个Applet程序,使其在窗口中以红,蓝,绿各颜色循环显示;Applet program
贴主认为可行的话,请您赏分为20分
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
/**
* @author Hardneedl
*/
final public class ColorApplet extends JApplet {
private static Color[] colors = new Color[]{Color.RED, Color.BLUE,Color.MAGENTA};
private static String[] msgs = new String[]{
"你好",
"你是鼹鼠吗?",
"你知道有个动画片叫做“鼹鼠的故事吗”"
};
private static Color currentColor = colors[0];
private static String currentMsg = msgs[0];
private ActionListener _action = new ActionListener(){
private int index = 0;
public void actionPerformed(ActionEvent e) {
index++;
if (index > colors.length -1) index = 0;
currentColor=colors[index];
currentMsg = msgs[index];
repaint();
}
};
private Timer timer = new Timer(1500,_action);
public void paint(Graphics g) {
super.paint(g);
Graphics gg = g.create();
gg.setColor(currentColor);
if (!timer.isRunning()) timer.start();
Rectangle2D rct = gg.getFontMetrics().getStringBounds(currentMsg,gg);
Rectangle selfRct = this.getBounds();
int x = (int)((selfRct.width - rct.getWidth() )/2);
int y = (int)((selfRct.height - rct.getHeight() )/2);
gg.drawString(currentMsg,x,y);
gg.dispose();
}
public void destroy() {
super.destroy();
timer.stop();
_action = null;
timer = null;
colors = null;
msgs = null;
}
}