您的位置:首页 > 其它

swing学习笔记五(进度条ProgressMonitor )

2012-09-06 22:44 302 查看
/**

* 进度条

*

* @time 3:30:01 PM

* @author retacn yue

* @Email zhenhuayue@sina.com

*/

public class Test_ProgressMonitorExample extends JPanel {

private static final long serialVersionUID = 1L;

ProgressThread1 progressThread1;

static JFrame myFrame;

/**

* 构造器

*/

public Test_ProgressMonitorExample() {

setLayout(new BorderLayout());

JPanel Panel = new JPanel();

JButton btnStart = new JButton("Start");

Panel.add(btnStart);

// 开始按钮

btnStart.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// 开始加载

startRunning();

}

});

// 结束按钮

JButton btnStop = new JButton("Stop");

Panel.add(btnStop);

btnStop.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// 停止加载

stopRunning();

}

});

add(Panel, BorderLayout.SOUTH);

}

public static void main(String[] args) {

myFrame = new JFrame("");

Test_ProgressMonitorExample example = new Test_ProgressMonitorExample();

myFrame.getContentPane().add("Center", example);

myFrame.setSize(200, 100);

myFrame.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

myFrame.setVisible(true);

}

/**

* 开始加载

*/

public void startRunning() {

if (null == progressThread1 || progressThread1.isAlive()) {

progressThread1 = new ProgressThread1(this);

progressThread1.start();

}

}

/**

* 停止加载

*/

public void stopRunning() {

progressThread1.setStop(true);

}

}

/**

* 自定义进度线程

*/

class ProgressThread1 extends Thread {

// 操作进度的类

ProgressMonitor monitor;

boolean stopStatus = false;// 状态位

// 最大值 和最小值

int min = 0;

int max = 50;

/**

* 构造器

*/

ProgressThread1(Component parent) {

monitor = new ProgressMonitor(parent, "Progress of Thread", "Not Started", min, max);

}

/**

* 停止进度

*/

public void setStop(boolean value) {

stopStatus = value;

}

@Override

public void run() {

monitor.setNote("Started");

for (int x = min; x <= max; x++) {

if (stopStatus) {

monitor.close();

break;

} else {

monitor.setProgress(x);

monitor.setNote("" + (x * 2) + "%");

try {

sleep(100);

} catch (Exception e) {

}

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: