14. 110. 1.ProgressMonitor Class進捗監視クラス


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SampleProgress{
	static ProgressMonitor monitor;
	static int progress;
	static Timer timer;
	
	public static void main(String[] args){
		JFrame frame = new JFrame("ProgressMonitor Sample");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new GridLayout(0, 1));	
		//----------------------------------------------------------------------------
		JButton startButton = new JButton(" ");
		ActionListener startActionListener = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Component parent = (Component)e.getSource();
				monitor = new ProgressMonitor(parent, "  ","Getting started...",0,200);
				progress = 0;
			}
		};
		startButton.addActionListener(startActionListener);
		frame.add(startButton);
		//----------------------------------------------------------------------------
		JButton increaseButton = new JButton(" ");
		ActionListener increaseActionListener = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(monitor == null){
					return ;
				}
				if(monitor.isCanceled()){
					System.out.println(" ");
				}else{
					progress += 5;
					monitor.setProgress(progress);
					monitor.setNote("   :" + progress + " ");
				}
			}
		};
		increaseButton.addActionListener(increaseActionListener);
		frame.add(increaseButton);
		//----------------------------------------------------------------------------
		JButton autoIncreaseButton = new JButton(" ");
		ActionListener autoIncreaseActionListener = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(monitor != null){
					if(timer == null){
						timer = new Timer(250, new ActionListener(){
							public void actionPerformed(ActionEvent ax){
								if(monitor == null){
								return;
								}
								if(monitor.isCanceled()){
									System.out.println(" ");
									timer.stop();
								}else{
									progress += 3;
									monitor.setProgress(progress);
									monitor.setNote("   :" + progress + " ");
								}
							}
						});
					}
					timer.start();
				}
			}
		};
		autoIncreaseButton.addActionListener(autoIncreaseActionListener);
		frame.add(autoIncreaseButton);
		
		frame.setSize(300, 200);
		frame.setVisible(true);
	}
}