⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 progressbartest.java

📁 "多线程的例子"包括了多线程的各个方面
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;

/**
 * 
 *  版本信息:
 *  类    名:
 *  功能描述:
 *  作者:Jiang Wu     
 *  日期:2005-10-10
 *  模块标识:
 */
/**
   This program demonstrates the use of a progress bar
   to monitor the progress of a thread.
*/
public class ProgressBarTest {
	public static void main(String[] args) {
		ProgressBarFrame frame = new ProgressBarFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.show();
	}
}

/**
   A frame that contains a button to launch a simulated activity,
   a progress bar, and a text area for the activity output.
*/
class ProgressBarFrame extends JFrame {
	public ProgressBarFrame() {
		setTitle("ProgressBarTest");
		setSize(WIDTH, HEIGHT);

		Container contentPane = getContentPane();

		// this text area holds the activity output
		textArea = new JTextArea();

		// set up panel with button and progress bar

		JPanel panel = new JPanel();
		startButton = new JButton("Start");
		progressBar = new JProgressBar();
		progressBar.setStringPainted(true);
		panel.add(startButton);
		panel.add(progressBar);
		contentPane.add(new JScrollPane(textArea), BorderLayout.CENTER);
		contentPane.add(panel, BorderLayout.SOUTH);

		// set up the button action

		startButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				progressBar.setMaximum(1000);
				activity = new SimulatedActivity(1000);
				activity.start();
				activityMonitor.start();
				startButton.setEnabled(false);
			}
		});

		// set up the timer action

		activityMonitor = new Timer(500, new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				int current = activity.getCurrent();

				// show progress
				textArea.append(current + "\n");
				progressBar.setValue(current);

				// check if task is completed
				if (current == activity.getTarget()) {
					activityMonitor.stop();
					startButton.setEnabled(true);
				}
			}
		});
	}

	private Timer activityMonitor;
	private JButton startButton;
	private JProgressBar progressBar;
	private JTextArea textArea;
	private SimulatedActivity activity;

	public static final int WIDTH = 300;
	public static final int HEIGHT = 200;
}

/**
   A similated activity thread.
*/
class SimulatedActivity extends Thread {
	/**
	   Constructs the simulated activity thread object. The
	   thread increments a counter from 0 to a given target.
	   @param t the target value of the counter.
	*/
	public SimulatedActivity(int t) {
		current = 0;
		target = t;
	}

	public int getTarget() {
		return target;
	}

	public int getCurrent() {
		return current;
	}

	public void run() {
		try {
			while (current < target && !interrupted()) {
				sleep(100);
				current++;
			}
		} catch (InterruptedException e) {
		}
	}

	private int current;
	private int target;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -