模擬展示動態按鈕

露水上的青蛙發表於2017-09-23

動態展示按鈕    沒有執行出效果來

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;
        }
        
    }

}

 

相關文章