package com.jb.arklis.d2.demo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
/**
* 功能:书写一个进度条,用来表示应用正在装载之中
* 作者:Arklis zeng
* 时间:2009-06-04
* 地点:成都公兴镇(Home)
* 版本:ver 1.0.0
* 备注:
* 控制进度条的关键是通过Action来实现。只要把createLoadButton和createStopButton中的事件处理代码放到自己想要的位置
* 就可以实现类似Eclipse启动时的装载动作!!!
* @author az
*
*/
public class ProgressBarDemo extends JFrame {
//设置一个计时器
private Timer timer = new javax.swing.Timer(18, createApplicationLoadAction());
private Action loadAction;
private Action stopAction;
private JProgressBar progressBar;
private int length = 200; //进度的条的最大长度
public ProgressBarDemo() {
setTitle("演示应用加载时的动画");
setSize(350, 150);
init();
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
private void init() {
JPanel panel = new JPanel();
// 初始化进度条
progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, length) {
//重写父类的getPreferredSize方法
public Dimension getPreferredSize() {
return new Dimension(300, super.getPreferredSize().height);
}
};
panel.add(BorderLayout.CENTER,progressBar);
//放按钮
panel.add(BorderLayout.SOUTH,createLoadButton());
panel.add(BorderLayout.SOUTH,createStopButton());
Container container = getContentPane();
container.add(panel);
}
public Action createApplicationLoadAction() {
return new AbstractAction("正在装载中...") {
public void actionPerformed(ActionEvent e) {
if (progressBar.getValue() < progressBar.getMaximum()) {
progressBar.setValue(progressBar.getValue() + 1);
} else {
timer.stop();
loadAction.setEnabled(true);
stopAction.setEnabled(false);
}
}
};
}
private JButton createLoadButton() {
loadAction = new AbstractAction("开始") {
public void actionPerformed(ActionEvent e) {
loadAction.setEnabled(false);
stopAction.setEnabled(true);
if (progressBar.getValue() == progressBar.getMaximum()) {
progressBar.setValue(0);
}
timer.start();
}
};
return createButton(loadAction);
}
private JButton createStopButton() {
stopAction = new AbstractAction("停止") {
public void actionPerformed(ActionEvent e) {
timer.stop();
loadAction.setEnabled(true);
stopAction.setEnabled(false);
}
};
return createButton(stopAction);
}
private JButton createButton(Action action) {
JButton button = new JButton();
button.setAction(action);//给按钮添加行为控制权限
return button;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
// TODO Auto-generated method stub
new ProgressBarDemo();
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Swing应用程序的加载动画示例
共9个文件
class:6个
classpath:1个
project:1个
4星 · 超过85%的资源 需积分: 50 37 下载量 167 浏览量
2009-06-04
12:52:38
上传
评论
收藏 8KB RAR 举报
温馨提示
环境Windows XP professional, JDK 1.6, Eclipse 3.3 该应用是演示怎样使用多线程来显示一个Swing应用的加载动画。主要是使用JProgressBar和Timer来实现。目的是让比较复杂的Swing应用启动时解决延迟的状态,以提供友好的使用感。 阅读对象:使用Swing开发应用的学员。
资源推荐
资源详情
资源评论
收起资源包目录
ApplicationLoadingAnimation.rar (9个子文件)
ApplicationLoadingAnimation
.project 403B
bin
com
jb
arklis
d2
demo
ProgressBarDemo$5.class 565B
ProgressBarDemo$4.class 1KB
ProgressBarDemo$2.class 1KB
ProgressBarDemo.class 3KB
ProgressBarDemo$1.class 844B
ProgressBarDemo$3.class 1KB
src
com
jb
arklis
d2
demo
ProgressBarDemo.java 3KB
.classpath 232B
共 9 条
- 1
资源评论
- u0103718762014-09-07效果比较简单,没达到心理预期。
- 十点数据2012-11-14不怎么样啊
技术大黍-曾老师
- 粉丝: 109
- 资源: 108
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功