1 21 22 public class DebuggerTestApplication extends javax.swing.JFrame { 23 24 25 public DebuggerTestApplication() { 26 this.setTitle("Debugger Test Application"); 27 initComponents(); 28 29 counterThread = new Thread (new Runnable () { 30 public void run() { 31 Thread thisThread = Thread.currentThread(); 32 while (counterThread == thisThread) { 33 updateCounter(); 34 try { 35 Thread.currentThread().sleep(1000); 36 if (counterThreadSuspended) { 37 synchronized(counterThread) { 38 while (counterThreadSuspended) { 39 counterThread.wait(); 40 } 41 } 42 } 43 44 } catch (InterruptedException ie) { 45 ie.printStackTrace(); 46 } 47 } 48 } 49 }); 50 counterThread.setName("counterThread"); 51 counterThread.start(); 52 53 } 54 55 60 private void initComponents() { jPanel1 = new javax.swing.JPanel (); 62 jButton1 = new javax.swing.JButton (); 63 jButton2 = new javax.swing.JButton (); 64 jLabel1 = new javax.swing.JLabel (); 65 jProgressBar1 = new javax.swing.JProgressBar (); 66 jProgressBar1.setMaximum(MAX_COUNT); 67 68 addWindowListener(new java.awt.event.WindowAdapter () { 69 public void windowClosing(java.awt.event.WindowEvent evt) { 70 exitForm(evt); 71 } 72 }); 73 74 jButton1.setText("Stop"); 75 jButton1.addActionListener(new java.awt.event.ActionListener () { 76 public void actionPerformed(java.awt.event.ActionEvent evt) { 77 jButton1ActionPerformed(evt); 78 } 79 }); 80 81 jPanel1.add(jButton1); 82 83 jButton2.setText("Clear"); 84 jButton2.addActionListener(new java.awt.event.ActionListener () { 85 public void actionPerformed(java.awt.event.ActionEvent evt) { 86 jButton2ActionPerformed(evt); 87 } 88 }); 89 90 jPanel1.add(jButton2); 91 92 getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH); 93 94 jLabel1.setFont(new java.awt.Font ("Dialog", 1, 36)); 95 jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 96 jLabel1.setText("jLabel1"); 97 getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER); 98 99 getContentPane().add(jProgressBar1, java.awt.BorderLayout.NORTH); 100 101 pack(); 102 } 104 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if (!counterThreadSuspended) { 106 counterThreadSuspended = !counterThreadSuspended; 107 jButton1.setText("Start"); 108 } else { 109 synchronized(counterThread) { 110 counterThreadSuspended = !counterThreadSuspended; 111 counterThread.notify(); 112 jButton1.setText("Stop"); 113 } 114 115 } 116 } 118 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { counter = 0; 120 jLabel1.setText(String.valueOf(counter)); 121 } 123 private void updateCounter() { 124 counter = (counter < MAX_COUNT) ? ++counter : 0 ; 125 jLabel1.setText(String.valueOf(counter)); 126 jProgressBar1.setValue(counter); 127 } 128 129 130 private void exitForm(java.awt.event.WindowEvent evt) { System.exit(0); 132 } 134 135 138 public static void main(String args[]) { 139 new DebuggerTestApplication().show(); 140 } 141 142 private javax.swing.JButton jButton1; 144 private javax.swing.JButton jButton2; 145 private javax.swing.JLabel jLabel1; 146 private javax.swing.JPanel jPanel1; 147 private javax.swing.JProgressBar jProgressBar1; 148 private Thread counterThread; 150 public volatile boolean counterThreadSuspended = false; 151 private int counter = 0; 152 153 private static final int MAX_COUNT = 13; 154 155 } 156 | Popular Tags |