编写一个Applet程序,使其在窗口中以红,蓝,绿各颜色循环显示;Applet program

编写一个Applet程序,使其在窗口中以红,蓝,绿各颜色循环显示任意内容??... 编写一个Applet程序,使其在窗口中以红,蓝,绿各颜色循环显示任意内容?? 展开
 我来答
hardneedl
2009-10-03 · TA获得超过922个赞
知道小有建树答主
回答量:1353
采纳率:57%
帮助的人:629万
展开全部

贴主认为可行的话,请您赏分为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;

    }

}

冬日恋曲_bwin
2009-10-02 · TA获得超过192个赞
知道答主
回答量:81
采纳率:0%
帮助的人:69.3万
展开全部
是不是三种颜色出现在不同时刻,还有你想显示的内容是什么?不太具体。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式