Oops.java
Download Oops.java
1: // This code show an "Oops!" moment---sleeping on the Event Dispatch
2: // Thread. Clicking the "Click me!" button will freeze the GUI for
3: // 10 seconds. The events are still queued by the JVM but the EDT is
4: // stuck waiting before it can process them.
5: //
6: // Written 3/2011 by Wayne Pollock
7:
8: import java.awt.*;
9: import java.awt.event.*;
10:
11: public class Oops {
12: public static void main ( String [] args ) {
13: Frame f = new Frame("Opps!");
14: f.setSize(250, 150);
15: Panel p;
16:
17: Button badBtn = new Button("Click me!");
18: p = new Panel();
19: p.add( badBtn );
20: f.add( p, "North" );
21:
22: badBtn.addActionListener( new ActionListener(){
23: public void actionPerformed( ActionEvent evt ) {
24: try { Thread.sleep( 10000 ); }
25: catch ( InterruptedException ie ) {}
26: }
27: });
28:
29: Button exitBtn = new Button( "Exit" );
30: p = new Panel();
31: p.add( exitBtn );
32: f.add( p, "South" );
33:
34: exitBtn.addActionListener( new ActionListener(){
35: @Override public void actionPerformed( ActionEvent evt ) {
36: System.exit(0);
37: }
38: });
39:
40: f.addWindowListener( new WindowAdapter() {
41: @Override public void windowClosing ( WindowEvent evt ) {
42: System.exit(0);
43: }
44: });
45: f.setVisible( true );
46: }
47: }