模拟展示动态按钮

动态展示按钮    没有运行出效果来

package com.lsw.server;

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HelloApplet extends Applet implements Runnable{
    private int fontSize = 8;
    private Thread changer;
    private boolean stopFlag = true;
    private Button contrlButton = new Button(" 开始动态显示!");
    
    public void init(){
        ActionListener actionListener = new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(stopFlag)
                    start();
                else
                    stop();
                
            }
        };
        
        contrlButton.addActionListener(actionListener);
        
        setBackground(Color.WHITE);
        add(contrlButton);
        setSize(100,100);
    }
    
    public void start(){
        changer = new Thread(this);
        stopFlag = false;
        fontSize = 8;
        contrlButton.setLabel("停止动态显示");
        changer.start();
    }
    
    public void paint(Graphics g){
        super.paint(g);
        g.setFont(new Font("newFont",Font.BOLD,fontSize));
        g.drawString("Hello", 30, 80);
    }
    
    public void stop(){
        stopFlag = true;
        contrlButton.setLabel("开始动态显示!");
    }

    @Override
    public void run() {
        while(!stopFlag){
            repaint();
            try{
                Thread.sleep(1000);
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
            if(fontSize++>40)
                fontSize = 8;
        }
        
    }

}

 

posted @ 2017-09-23 17:21  露水上的青蛙  阅读(209)  评论(0编辑  收藏  举报