Swing进度条样例
2013-09-18 13:19
260 查看
package com.jadyer.demo;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JRootPane;
import javax.swing.SwingConstants;
import javax.swing.Timer;
/**
* Swing进度条样例
* @create Apr 24, 2013 1:52:44 PM
* @author 玄玉<http://blog.csdn.net/jadyer>
*/
public class JProgressBarDemo {
Timer timer;
JProgressBar jpbFileLoading;
public JProgressBarDemo() {
JFrame jf = new JFrame("进度条测试");
/**
* 创建一个常规模式的进度条,其默认为水平方向,最小值为0,最大值为100,初始值为0
*/
jpbFileLoading = new JProgressBar();
jpbFileLoading.setStringPainted(true); //设置进度条呈现进度字符串,默认为false
jpbFileLoading.setBorderPainted(false); //不绘制边框,默认为true
jpbFileLoading.setPreferredSize(new Dimension(100, 40)); //设置首选大小
timer = new Timer(50, new ActionListener(){
public void actionPerformed(ActionEvent e) {
int loadingValue = jpbFileLoading.getValue();
if (loadingValue < 100){
jpbFileLoading.setValue(++loadingValue);
}else {
timer.stop();
}
}
});
timer.start();
/**
* 创建一个不确定模式的进度条
*/
JProgressBar jpbFileLoadingIndeterminate = new JProgressBar();
jpbFileLoadingIndeterminate.setIndeterminate(true); //设置进度条为不确定模式,默认为确定模式
jpbFileLoadingIndeterminate.setStringPainted(true);
jpbFileLoadingIndeterminate.setString("文件加载中......");
/**
* 将两种进度条放到主面板里
*/
jf.add(jpbFileLoading, BorderLayout.NORTH);
jf.add(new JLabel("上面为常规进度条,下面为不确定模式进度条", SwingConstants.CENTER), BorderLayout.CENTER);
jf.add(jpbFileLoadingIndeterminate, BorderLayout.SOUTH);
jf.setSize(300, 150);
jf.setLocationRelativeTo(null); //居中显示
jf.setUndecorated(true); //禁用此窗体的装饰
jf.getRootPane().setWindowDecorationStyle(JRootPane.NONE); //采用指定的窗体装饰风格
jf.setVisible(true);
/**
* 不确定模式的进度条处理
*/
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
jpbFileLoadingIndeterminate.setIndeterminate(false); //设置进度条为确定模式,即常规模式,否则那个条还会走来走去
jpbFileLoadingIndeterminate.setString("文件加载完毕..");
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
/**
* 关闭窗体
*/
jf.setVisible(false); //隐藏窗体
jf.dispose(); //释放资源,关闭窗体
jf = null; //若不再使用了就这样
}
public static void main(String[] args) {
new JProgressBarDemo();
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JRootPane;
import javax.swing.SwingConstants;
import javax.swing.Timer;
/**
* Swing进度条样例
* @create Apr 24, 2013 1:52:44 PM
* @author 玄玉<http://blog.csdn.net/jadyer>
*/
public class JProgressBarDemo {
Timer timer;
JProgressBar jpbFileLoading;
public JProgressBarDemo() {
JFrame jf = new JFrame("进度条测试");
/**
* 创建一个常规模式的进度条,其默认为水平方向,最小值为0,最大值为100,初始值为0
*/
jpbFileLoading = new JProgressBar();
jpbFileLoading.setStringPainted(true); //设置进度条呈现进度字符串,默认为false
jpbFileLoading.setBorderPainted(false); //不绘制边框,默认为true
jpbFileLoading.setPreferredSize(new Dimension(100, 40)); //设置首选大小
timer = new Timer(50, new ActionListener(){
public void actionPerformed(ActionEvent e) {
int loadingValue = jpbFileLoading.getValue();
if (loadingValue < 100){
jpbFileLoading.setValue(++loadingValue);
}else {
timer.stop();
}
}
});
timer.start();
/**
* 创建一个不确定模式的进度条
*/
JProgressBar jpbFileLoadingIndeterminate = new JProgressBar();
jpbFileLoadingIndeterminate.setIndeterminate(true); //设置进度条为不确定模式,默认为确定模式
jpbFileLoadingIndeterminate.setStringPainted(true);
jpbFileLoadingIndeterminate.setString("文件加载中......");
/**
* 将两种进度条放到主面板里
*/
jf.add(jpbFileLoading, BorderLayout.NORTH);
jf.add(new JLabel("上面为常规进度条,下面为不确定模式进度条", SwingConstants.CENTER), BorderLayout.CENTER);
jf.add(jpbFileLoadingIndeterminate, BorderLayout.SOUTH);
jf.setSize(300, 150);
jf.setLocationRelativeTo(null); //居中显示
jf.setUndecorated(true); //禁用此窗体的装饰
jf.getRootPane().setWindowDecorationStyle(JRootPane.NONE); //采用指定的窗体装饰风格
jf.setVisible(true);
/**
* 不确定模式的进度条处理
*/
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
jpbFileLoadingIndeterminate.setIndeterminate(false); //设置进度条为确定模式,即常规模式,否则那个条还会走来走去
jpbFileLoadingIndeterminate.setString("文件加载完毕..");
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
/**
* 关闭窗体
*/
jf.setVisible(false); //隐藏窗体
jf.dispose(); //释放资源,关闭窗体
jf = null; //若不再使用了就这样
}
public static void main(String[] args) {
new JProgressBarDemo();
}
}
相关文章推荐
- Swing进度条样例
- 《Java 2 图形设计卷Ⅱ- SWING》第11章 进度条、滑杆和分隔条
- swing进度条
- android一个上传图片的样例,包含怎样终止上传过程,假设在上传的时候更新进度条(二)
- Swing高级JProgressBarTest进度条
- Swift - 歌曲播放样例(附带播放时间和播放进度)
- android一个上传图片的样例,包含怎样终止上传过程,假设在上传的时候更新进度条(一)
- Java核心技术(高级Swing)四进度指示器
- 使用Java的Swing操作进度条高级操作
- java Swing 进度条 时间进度条
- Swing进度条
- Swing 中进度指示器的使用(一)
- java swing jtable 中加进度条
- Swing 中进度指示器的使用(二)
- Java Swing 创建转圈的进度提示框
- java swing 制作进度条-- 模拟卸载,附效果图
- JAVA简单Swing图形界面应用演示样例
- Java Swing组件实现进度监视功能示例
- Java Swing复习(11)进度条(涉及多线程)
- Swing圆形进度条