swing之进度监视
2015-11-13 14:18
169 查看
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ProgressMonitor;
import javax.swing.Timer;
public class TestProgressMonitor
{
Timer timer;
public void init()
{
final SimulatedTargetMi target = new SimulatedTargetMi(1000);
//以启动一条线程的方式来执行一个耗时的任务
final Thread targetThread = new Thread(target);
targetThread.start();
//创建进度对话框
final ProgressMonitor dialog = new ProgressMonitor(null ,
"等待任务完成,任务完成之前请不要关闭窗口,否则将取消当前操作..." , "已完成:0.00%" , 0 , target.getAmount());
//创建一个计时器
timer = new Timer(300 , new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//以任务的当前完成量设置进度对话框的完成比例
dialog.setProgress(target.getCurrent());
dialog.setNote("已完成:" + target.getPercent());
//如果用户单击了进度对话框的”取消“按钮
if (dialog.isCanceled())
{
//停止计时器
timer.stop();
//中断任务的执行线程
targetThread.interrupt();
//系统退出
System.exit(0);
}
}
});
timer.start();
}
public static void main(String[] args)
{
new TestProgressMonitor().init();
}
}
//模拟一个耗时的任务
class SimulatedTargetMi implements Runnable
{
//任务的当前完成量
private volatile int current;
//总任务量
private int amount;
public SimulatedTargetMi(int amount)
{
current = 0;
this.amount = amount;
}
public int getAmount()
{
return amount;
}
public int getCurrent()
{
return current;
}
//run方法代表不断完成任务的过程
public void run()
{
while (current < amount)
{
try
{
Thread.sleep(50);
}
catch(InterruptedException e)
{
}
current++;
}
}
public String getPercent(){
return String.format("%.2f", 100.0 * current/amount ) + "%";
}
}
////////////////////////////////////////////////
实例二:
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
public class TestJProgressBar
{
JFrame frame = new JFrame("当前进度指示...");
//创建一条垂直进度条
JProgressBar bar = new JProgressBar(JProgressBar.HORIZONTAL);
JLabel tipLabel = new JLabel("提示:", JLabel.LEFT);
JLabel contentLabel = new JLabel("任务完成之前请不要关闭窗口,否则将取消当前操作...", JLabel.LEFT);
JLabel statusLabel = new JLabel(" ", JLabel.CENTER);
public void init()
{
frame.setLayout(new FlowLayout());
frame.setResizable(false);
tipLabel.setFont(new Font("Serif",Font.PLAIN,14));
contentLabel.setFont(new Font("Serif",Font.PLAIN,14));
statusLabel.setFont(new Font("Serif",Font.PLAIN,14));
JPanel panel = new JPanel();
//fr5.setBorder(new TitledBorder("BoxLayout - Y"));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(tipLabel);
panel.add(Box.createVerticalStrut(2));
panel.add(contentLabel);
panel.add(Box.createVerticalStrut(7));
panel.add(bar);
//panel.add(Box.createVerticalGlue());
panel.add(Box.createVerticalStrut(2));
panel.add(statusLabel);
frame.add(panel, 0);
final SimulatedTarget target = new SimulatedTarget(1000);
//以启动一条线程的方式来执行一个耗时的任务
final Thread thread = new Thread(target);
thread.start();
//设置在进度条中绘制完成百分比
bar.setStringPainted(true);
//bar.setPreferredSize(new Dimension(100, 18));
//设置进度条的最大值和最小值,
bar.setMinimum(0);
//以总任务量作为进度条的最大值
bar.setMaximum(target.getAmount());
final Timer timer = new Timer(300 , new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//以任务的当前完成量设置进度条的value
bar.setValue(target.getCurrent());
if(target.getAmount() <= target.getCurrent()){
statusLabel.setText("处理完成,oh yes!");
}
}
});
timer.start();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e) {
thread.interrupt();
timer.stop();
//系统退出
System.exit(0);
}
});
//该代码依据放置的组件设定窗口的大小使之正好能容纳你放置的所有组件
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
new TestJProgressBar().init();
}
}
//模拟一个耗时的任务
class SimulatedTarget implements Runnable
{
//任务的当前完成量
private volatile int current;
//总任务量
private int amount;
public SimulatedTarget(int amount)
{
current = 0;
this.amount = amount;
}
public int getAmount()
{
return amount;
}
public int getCurrent()
{
return current;
}
//run方法代表不断完成任务的过程
public void run()
{
while (current < amount)
{
try
{
Thread.sleep(20);
}
catch(InterruptedException e)
{
}
current++;
}
}
public String getPercent(){
return String.format("%.1f", 100.0 * current/amount ) + "%";
}
}
import java.awt.event.ActionListener;
import javax.swing.ProgressMonitor;
import javax.swing.Timer;
public class TestProgressMonitor
{
Timer timer;
public void init()
{
final SimulatedTargetMi target = new SimulatedTargetMi(1000);
//以启动一条线程的方式来执行一个耗时的任务
final Thread targetThread = new Thread(target);
targetThread.start();
//创建进度对话框
final ProgressMonitor dialog = new ProgressMonitor(null ,
"等待任务完成,任务完成之前请不要关闭窗口,否则将取消当前操作..." , "已完成:0.00%" , 0 , target.getAmount());
//创建一个计时器
timer = new Timer(300 , new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//以任务的当前完成量设置进度对话框的完成比例
dialog.setProgress(target.getCurrent());
dialog.setNote("已完成:" + target.getPercent());
//如果用户单击了进度对话框的”取消“按钮
if (dialog.isCanceled())
{
//停止计时器
timer.stop();
//中断任务的执行线程
targetThread.interrupt();
//系统退出
System.exit(0);
}
}
});
timer.start();
}
public static void main(String[] args)
{
new TestProgressMonitor().init();
}
}
//模拟一个耗时的任务
class SimulatedTargetMi implements Runnable
{
//任务的当前完成量
private volatile int current;
//总任务量
private int amount;
public SimulatedTargetMi(int amount)
{
current = 0;
this.amount = amount;
}
public int getAmount()
{
return amount;
}
public int getCurrent()
{
return current;
}
//run方法代表不断完成任务的过程
public void run()
{
while (current < amount)
{
try
{
Thread.sleep(50);
}
catch(InterruptedException e)
{
}
current++;
}
}
public String getPercent(){
return String.format("%.2f", 100.0 * current/amount ) + "%";
}
}
////////////////////////////////////////////////
实例二:
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
public class TestJProgressBar
{
JFrame frame = new JFrame("当前进度指示...");
//创建一条垂直进度条
JProgressBar bar = new JProgressBar(JProgressBar.HORIZONTAL);
JLabel tipLabel = new JLabel("提示:", JLabel.LEFT);
JLabel contentLabel = new JLabel("任务完成之前请不要关闭窗口,否则将取消当前操作...", JLabel.LEFT);
JLabel statusLabel = new JLabel(" ", JLabel.CENTER);
public void init()
{
frame.setLayout(new FlowLayout());
frame.setResizable(false);
tipLabel.setFont(new Font("Serif",Font.PLAIN,14));
contentLabel.setFont(new Font("Serif",Font.PLAIN,14));
statusLabel.setFont(new Font("Serif",Font.PLAIN,14));
JPanel panel = new JPanel();
//fr5.setBorder(new TitledBorder("BoxLayout - Y"));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(tipLabel);
panel.add(Box.createVerticalStrut(2));
panel.add(contentLabel);
panel.add(Box.createVerticalStrut(7));
panel.add(bar);
//panel.add(Box.createVerticalGlue());
panel.add(Box.createVerticalStrut(2));
panel.add(statusLabel);
frame.add(panel, 0);
final SimulatedTarget target = new SimulatedTarget(1000);
//以启动一条线程的方式来执行一个耗时的任务
final Thread thread = new Thread(target);
thread.start();
//设置在进度条中绘制完成百分比
bar.setStringPainted(true);
//bar.setPreferredSize(new Dimension(100, 18));
//设置进度条的最大值和最小值,
bar.setMinimum(0);
//以总任务量作为进度条的最大值
bar.setMaximum(target.getAmount());
final Timer timer = new Timer(300 , new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//以任务的当前完成量设置进度条的value
bar.setValue(target.getCurrent());
if(target.getAmount() <= target.getCurrent()){
statusLabel.setText("处理完成,oh yes!");
}
}
});
timer.start();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e) {
thread.interrupt();
timer.stop();
//系统退出
System.exit(0);
}
});
//该代码依据放置的组件设定窗口的大小使之正好能容纳你放置的所有组件
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
new TestJProgressBar().init();
}
}
//模拟一个耗时的任务
class SimulatedTarget implements Runnable
{
//任务的当前完成量
private volatile int current;
//总任务量
private int amount;
public SimulatedTarget(int amount)
{
current = 0;
this.amount = amount;
}
public int getAmount()
{
return amount;
}
public int getCurrent()
{
return current;
}
//run方法代表不断完成任务的过程
public void run()
{
while (current < amount)
{
try
{
Thread.sleep(20);
}
catch(InterruptedException e)
{
}
current++;
}
}
public String getPercent(){
return String.format("%.1f", 100.0 * current/amount ) + "%";
}
}
相关文章推荐
- soj 1679 Gangsters(dp)
- ubuntu中安装和卸载apache2
- Cocos2D旋转炮塔到指定角度(一)
- Cocos2D旋转炮塔到指定角度(一)
- Linux驱动环境配置之内核树的建立
- laravel 5 用户管理
- html头部
- 关于消息队列
- 【转帖】安装Intel HAXM为Android 模拟器加速,30秒内启动完成
- PHP程序对象、数组串行化(序列化)
- C语言 memcpy memmove
- Orlace 数据库连接的那些事儿:服务器端(四)
- 使用mailto在网页中链接Email地址
- 跨设备自适应代码
- 定时任务调度
- Egit Patch
- 杭电5533 Dancing Stars on Me
- Python——获取进程信息
- 安卓开发之组件
- Bugtags奉命解救宝贵的双手,务必将此文章转给你身边的程序猿