====WinApp
package taskShow;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class WinApp extends Frameimplements ActionListener{
public static final int MAP_WIDTH = 800;
public static final int MAP_HEIGHT = 600;
private Image offScreenImage = null;
private TaskHandler taskhandler = null;
private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public static void main(String[] args) {
WinApp app = new WinApp();
app.lauchFrame();
app.initViewObj();
}
/** 初始化窗体信息 */
private void lauchFrame() {
this.setTitle("任务列表Demo");
this.setSize(MAP_WIDTH, MAP_HEIGHT);
this.setResizable(false); // 是否可以调整大小
this.setBackground(new Color(187, 201, 229));
this.setVisible(true);
this.setLocation(800, 500);
this.setLayout(new FlowLayout());
new Thread(new ThreadPaint()).start();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/**
* 初始化界面内容
*/
private void initViewObj(){
TaskProgressBar tpbar = new TaskProgressBar(); //进度条实体
Task task = new Task(1, "任务还未开始"); //任务实体
taskhandler = new TaskHandler(task, tpbar);
taskhandler.getTask().setTaskHandler(taskhandler);
taskhandler.addActionListener(this);
this.add(taskhandler);
}
@Override
public void actionPerformed(ActionEvent e) {
TaskHandler th = (TaskHandler)e.getSource();
Task tk = th.getTask();
if(tk.isBegin()){
return;
}
tk.setBegin(true);
tk.setName("任务正在进行中...");
final ScheduledFuture<?> readerHandle = scheduler.scheduleAtFixedRate(th.getPbar(), 1000, 1000, TimeUnit.MILLISECONDS); //一秒执行一次
TimeSchedule timeS = new TimeSchedule(tk, readerHandle);
scheduler.schedule(timeS, 5, TimeUnit.SECONDS); //5秒的持续时间
}
/** 重绘屏幕 */
class ThreadPaint implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(41);
repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void paint(Graphics g) {
if(taskhandler.getPbar() != null){
taskhandler.getPbar().draw(g);
}
}
/** 加入缓冲区 */
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(MAP_WIDTH,MAP_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(new Color(187, 201, 229));
gOffScreen.fillRect(0, 0, MAP_WIDTH, MAP_HEIGHT);
gOffScreen.setColor(c);
this.paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
}
========TaskHandler
package taskShow;
import java.awt.Button;
/**
* 任务手柄
* 与用户进行事件触发,显示任务信息
*/
public class TaskHandler extends Button{
private static final long serialVersionUID = -1331222163890726042L;
private Task task;
private TaskProgressBar taskProgress;
public TaskHandler(Task task, TaskProgressBar taskProgress){
super();
this.task = task;
this.taskProgress = taskProgress;
setBounds(100, 100, 80, 40);
setLabel(task.getName());
setSize(80, 40);
setVisible(true);
}
public Task getTask() {
return task;
}
public TaskProgressBar getPbar(){
return taskProgress;
}
}
============TaskProgressBar
package taskShow;
import java.awt.Color;
import java.awt.Graphics;
/**
* 任务进度条
*/
public class TaskProgressBar implements Runnable{
private int x; //X坐标
private int y; //Y坐标
private int height; //进度条的高度
private int totalBarLen = 200; //进度条总长度
private int runingLen = 0; //正在行走的进度长度
int percent = 0; //换算之后的百分比
public TaskProgressBar(){
x = 100;
y = 100;
height = 20;
}
public void draw(Graphics g){
//总条数
g.setColor(Color.RED);
g.fillRect(x, y, totalBarLen, height);
//进度条
g.setColor(Color.GREEN);
g.fillRect(x, y, runingLen, height);
//进度条文字显示
g.setColor(Color.BLACK);
percent = runingLen * 100 / 200;
g.drawString(percent + "%", x + totalBarLen / 2, y + 10);
}
@Override
public void run() {
runingLen += 40; //一秒绘制一次
}
/**清空进度条信息*/
public void clearInfo(){
this.percent = 0;
this.runingLen = 0;
}
}
==============Task
package taskShow;
/**
* 任务实体类
*/
public class Task {
private int id;
private String name;
private double beginTime;//任务开始时间
private double useTime; //任务所需时间
private boolean isBegin; //任务是否已经开始
private TaskHandler taskhandler = null;
public Task(){}
public Task(int id, String name){
this.id = id;
this.name = name;
}
/**添加按钮手柄引用*/
public void setTaskHandler(TaskHandler taskhandler2) {
this.taskhandler = taskhandler2;
}
/**获取按钮手柄*/
public TaskHandler getTaskHandler(){
return taskhandler;
}
public final int getId() {
return id;
}
public final void setId(int id) {
this.id = id;
}
public final String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
taskhandler.setLabel(name);
}
public final double getBeginTime() {
return beginTime;
}
public final void setBeginTime(double beginTime) {
this.beginTime = beginTime;
}
public final double getUseTime() {
return useTime;
}
public final void setUseTime(double useTime) {
this.useTime = useTime;
}
public final boolean isBegin() {
return isBegin;
}
public final void setBegin(boolean isBegin) {
this.isBegin = isBegin;
}
}
==============TimeSchedule
package taskShow;
import java.util.concurrent.ScheduledFuture;
public class TimeSchedule implements Runnable{
private Task task;
private ScheduledFuture<?> everytimeSched; //控制每秒运行的时钟
public TimeSchedule(Task task, ScheduledFuture<?> everytimeSched){
this.task = task;
this.everytimeSched = everytimeSched;
}
@Override
public void run() {
task.setName("任务时间到");
task.setBegin(false);
TaskHandler tk = task.getTaskHandler();
TaskProgressBar tpbar = tk.getPbar();
tpbar.clearInfo();
everytimeSched.cancel(true);
}
}
图示: