import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class Test1 { public static void main (String[] args) { Win win = new Win("Test Window"); } } /*******************/ /* クラスWinの定義 */ /*******************/ class Win extends JFrame implements ActionListener { JTextArea ta; JButton bt; JProgressBar pb; java.util.Timer timer; Test ts; int end_t = 500; /******************/ /* コンストラクタ */ /******************/ Win (String name) { // Frameクラスのコンストラクタ(Windowのタイトルを引き渡す) super(name); // テキストエリアの追加 Container cp = getContentPane(); Font f = new Font("MS 明朝", Font.PLAIN, 20); ta = new JTextArea(4, 25); ta.setFont(f); cp.add(new JScrollPane(ta), BorderLayout.CENTER); // パネルの追加 JPanel pn = new JPanel(); cp.add(pn, BorderLayout.SOUTH); // ボタン bt = new JButton("開始"); bt.addActionListener(this); pn.add(bt); // 進捗モニタ pb = new JProgressBar(); pb.setStringPainted(true); pb.setMaximum(end_t); pn.add(pb); // Windowの大きさ setSize(300, 200); // ウィンドウを表示 setVisible(true); // イベントアダプタ addWindowListener(new WinEnd()); } /******************************/ /* 上,左,下,右の余白の設定 */ /******************************/ public Insets getInsets() { return new Insets(35, 10, 10, 10); } /******************************/ /* ボタンが押されたときの処理 */ /******************************/ public void actionPerformed(ActionEvent e) { if (e.getSource() == bt) { // スレッド(Test)の開始 ts = new Test(end_t); ts.start(); // ボタンをイネーブルでなくす bt.setEnabled(false); // タイマーをスタート timer = new java.util.Timer(); timer.schedule(new Timer_test(), 0, 500); } } class Timer_test extends TimerTask { public void run() { int current = ts.getCurrent(); // 進捗状況の表示 ta.append(current + "\n"); pb.setValue(current); // 終了チェック if (current == ts.getEnd()) { timer.cancel(); bt.setEnabled(true); } } } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } } /**************/ /* クラスTest */ /**************/ class Test extends Thread { int current, end; // 現在時間と終了時間 // コンストラクタ Test(int end1) { current = 0; end = end1; } // 終了時間を返す public int getEnd() { return end; } // 現在時刻を返す public int getCurrent() { return current; } // 実行 public void run() { while (current < end) { try { sleep(100); } catch(InterruptedException e) {return;} current++; } } }