swing 跑马灯特效(不断动态前进的效果)

跑马灯特效(不断动态前进的效果)

1,跑马灯特效的障眼法实现:就是前面不断循环 添加空格 ,视觉效果就好像跑起来了哈哈哈

swing写的跑马灯 + 进度条更新效果小项目 代码:

 

  ❀class ProgressBar

package ProgressBar;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class ProgressBar extends JFrame {
    private JButton startButton;    //开始按钮
    private JButton pauseButton;    //暂停按钮
    private JPanel contentPane;        //内容面板
    private JProgressBar progressBar;//进度条 ,使用默认的呀呀呀呀,不用基础JProgressBar重写一个好看的
    private ProgressBarTask progressBarTask;//进度条任务
    private MarqueeTask marqueeTask;        //跑马灯特效任务
    public ProgressBar() {
        //设置标题
        setTitle("进度条");
        //设置退出模式
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置大小
        setSize(800,600);
        //居中
        setLocationRelativeTo(null);
        //初始化
        initCommponents();
        //添加事件
        initEvents();
    }

    private void initCommponents() {
        //内容面板
        contentPane = (JPanel) getContentPane();
        //添加开始、暂停按钮
        startButton = new JButton("开始");
        pauseButton = new JButton("暂停");
        //顶部布局面板
        JPanel topPane = new JPanel();
        //顶部布局面板添加开始、暂停按钮
        topPane.add(startButton);
        topPane.add(pauseButton);
        //进度条面板
        JPanel progressBarPane = new JPanel();
        //进度条面板添加进度条
        progressBar = new JProgressBar();
        //设置进度条大小
        progressBar.setPreferredSize(new Dimension(600,30));
        //设置进度条的数字
        progressBar.setStringPainted(true);
        progressBarPane.add(progressBar);
        //内容面板添加你进度条面板到中间
        contentPane.add(progressBarPane, BorderLayout.CENTER);
        //内容面板添加顶部面板到顶部
        contentPane.add(topPane, BorderLayout.NORTH);

    }

    private void initEvents() {
        // 为开始按钮添加事件
        startActionListenner();
        //为暂停按钮添加事件
        pauseActionListener();
        
        
    }

    private void startActionListenner() {
        startButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
                    //开始进度条任务
                    progressBarTask = new ProgressBarTask(progressBar);
                    //装有进度条任务的线程
                    Thread thread1 = new Thread(progressBarTask);
                    thread1.start();
                    marqueeTask = new MarqueeTask(ProgressBar.this);
                    new Thread(marqueeTask).start();;
                    
                }
            }
        });
    }

    private void pauseActionListener() {
        pauseButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                progressBarTask.stop();
                
            }
        });
        
    }
    
    public static void main(String[] args) {
        new ProgressBar().setVisible(true);
    }
}

  ❀class ProgressBarTask

package ProgressBar;

import java.util.concurrent.TimeUnit;

import javax.swing.JProgressBar;

/**
 * 进度条更新任务
 * @author Huangyujun
 *
 */

public class ProgressBarTask implements Runnable{
    //private ProgressBar progressBar;   因为咱自己写的Progress 没有继承JProgressBar重写它,
    //而是new 一个 JProgressBar默认样式
    //这里也要使用JProgressBar
    private JProgressBar progressBar; 
    private boolean isStopProgress = false;
    public ProgressBarTask(JProgressBar progressBar) {
        this.progressBar = progressBar;
    }
    @Override
    public void run() {
        int currValue = progressBar.getValue();
        // 开始进度条
        while (currValue < 100 && !isStopProgress) {
            currValue = progressBar.getValue();
            progressBar.setValue(currValue + 1);
            try {
                TimeUnit.MILLISECONDS.sleep(300);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    public void stop() {
        isStopProgress = true;
    }
}

  ❀class MarqueeTask

package ProgressBar;

import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;

/** 
 * 跑马灯特效任务
 * @author Huangyujun
 *
 */
public class MarqueeTask implements Runnable{
    private JFrame jFrame;
    String title;
    public MarqueeTask(JFrame jFrame) {
        this.jFrame = jFrame;
        title = jFrame.getTitle();
    }
    @Override
    public void run() {
        StringBuffer strTitle = new StringBuffer(title);
        while(true) {
            strTitle.insert(0, " ");
            if(strTitle.length() == 100) {
                strTitle = new StringBuffer(strTitle.toString().trim());
            }
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            jFrame.setTitle(strTitle.toString());
        }
        
    }
    
}

 转自:老九学堂线上java班《java多线程---跑马灯案例》和《java多线程----创建线程的方法--Runnable》

上一篇:matlab进度条显示


下一篇:android ProgressBar(进度条)