Swings and Inner Classes
Hsuan-Tien Lin
Department of CSIE, NTU
OOP Class, June 14-15, 2010
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 0 / 12
NEZE\ W[MRK
NEZE E[X
Getting Started with GUI
1 i m p o r t j a v a x . swing . ∗ ;
2 p u b l i c c l a s s GUIDemo {
3 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {
4 JFrame frm = new JFrame ( ) ;
5 frm . s e t V i s i b l e (t r u e) ;
6 }
7 }
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 1 / 12
Setting Size of Frame
1 i m p o r t j a v a x . swing . ∗ ;
2 p u b l i c c l a s s GUIDemo {
3 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {
4 JFrame frm = new JFrame ( ) ;
5 frm . s e t S i z e ( 6 4 0 , 480) ;
6 frm . s e t V i s i b l e (t r u e) ;
7 }
8 }
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 2 / 12
Inheriting from JFrame
1 i m p o r t j a v a x . swing . ∗ ;
2 p u b l i c c l a s s GUIDemo {
3 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {
4 JFrame frm = new MyFrame ( " l a l a l a " ) ;
5 }
6 }
7 c l a s s MyFrame extends JFrame {
8 MyFrame ( S t r i n g t i t l e ) {
9 super( t i t l e ) ;
10 s e t S i z e ( 6 4 0 , 480) ;
11 s e t V i s i b l e (t r u e) ;
12 }
13 }
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 3 / 12
Adding Components to JFrame
1 c l a s s MyFrame extends JFrame {
2 J L a b e l l b l ;
3 MyFrame ( S t r i n g t i t l e ) {
4 super( t i t l e ) ;
5 s e t S i z e ( 6 4 0 , 480) ;
6 l b l = new J L a b e l ( " u n d e f i n e d " ) ;
7 add ( l b l ) ;
8 s e t V i s i b l e (t r u e) ;
9 }
10 }
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 4 / 12
Using CounterLabel as both JLabel and Runnable
1 i m p o r t j a v a . awt . ∗ ;
2 c l a s s MyFrame extends JFrame {
3 MyFrame ( S t r i n g t i t l e ) {
4 super( t i t l e ) ;
5 s e t S i z e ( 6 4 0 , 480) ; s e t V i s i b l e (t r u e) ;
6 f o r(i n t i = 0 ; i <10; i ++) new Counter Label (t h i s) ;
7 s e t L a y o u t (new FlowLayout ( ) ) ;
8 }
9 }
10 c l a s s CounterLa bel extends J L a b e l implements Runnable {
11 i n t c oun t = 0 ;
12 Counte rLabel ( C o n t a i n e r c o n t a i n e r ) {
13 c o n t a i n e r . add (t h i s) ;
14 (new Thread (t h i s) ) . s t a r t ( ) ;
15 }
16 p u b l i c v o i d run ( ) {
17 w h i l e(t r u e) {
18 t r y{ Thread . s l e e p ( 1 0 0 0 ) ; }
19 c a t c h( I n t e r r u p t e d E x c e p t i o n e ) { }
20 co unt ++;
21 t h i s. s e t T e x t ( " " + cou nt ) ;
22 }
23 }
24 }
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 5 / 12
Writing CounterLabel by Inner Thread Class
1 c l a s s CounterLa bel extends J L a b e l {
2 i n t c oun t = 0 ;
3 Counte rLabel ( C o n t a i n e r c o n t a i n e r ) {
4 c o n t a i n e r . add (t h i s) ;
5 (new CounterThread ( ) ) . s t a r t ( ) ;
6 }
7 c l a s s CounterThread extends Thread {
8 p u b l i c v o i d run ( ) {
9 w h i l e(t r u e) {
10 t r y{
11 Thread . s l e e p ( 1 0 0 0 ) ;
12 }
13 c a t c h( I n t e r r u p t e d E x c e p t i o n e ) { }
14 co unt ++;
15 s e t T e x t ( " " + c oun t ) ;
16 }
17 }
18 }
19 }
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 6 / 12
Writing CounterLabel by Anonymous Thread Class
1 c l a s s CounterLa bel extends J L a b e l {
2 i n t c oun t = 0 ;
3 Counte rLabel ( C o n t a i n e r c o n t a i n e r ) {
4 c o n t a i n e r . add (t h i s) ;
5 (new Thread ( ) {
6 p u b l i c v o i d run ( ) {
7 w h i l e(t r u e) {
8 t r y{
9 Thread . s l e e p ( 1 0 0 0 ) ;
10 }
11 c a t c h( I n t e r r u p t e d E x c e p t i o n e ) { }
12 co unt ++;
13 s e t T e x t ( " " + c oun t ) ;
14 }
15 }
16 } ) . s t a r t ( ) ;
17 }
18 }
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 7 / 12
Inner Class
easily share (private) data of outer-class instances
—usually as instance-specific, private utility class
often also do-able with interfaces, but clearer if used appropriately anonymous class: a extreme if the specific class is only needed
“temporarily”
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 8 / 12
A CounterLabel that Stops
1 c l a s s CounterLa bel extends J L a b e l {
2 i n t c oun t = 0 ;
3 boolean s t o p p i n g = t r u e;
4 Counte rLabel ( C o n t a i n e r c o n t a i n e r ) {
5 c o n t a i n e r . add (t h i s) ;
6 (new CounterThread ( ) ) . s t a r t ( ) ;
7 }
8 v o i d s t a r t ( ) { s t o p p i n g = f a l s e; }
9 v o i d s t o p ( ) { s t o p p i n g = t r u e; }
10 c l a s s CounterThread extends Thread {
11 p u b l i c v o i d run ( ) {
12 w h i l e(t r u e) {
13 t r y{
14 Thread . s l e e p ( 1 0 0 0 ) ;
15 }
16 c a t c h( I n t e r r u p t e d E x c e p t i o n e ) { }
17 i f ( ! s t o p p i n g ) {
18 co unt ++;
19 s e t T e x t ( " " + c oun t ) ;
20 }
21 }
22 }
23 }
24 }
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 9 / 12
A CounterLabel that Comes with a Control Button
1 c l a s s CounterLa bel extends J L a b e l {
2 C o n t r o l B u t t o n b u t = new C o n t r o l B u t t o n ( ) ;
3 Counte rLabel ( C o n t a i n e r c o n t a i n e r ) {
4 c o n t a i n e r . add (t h i s) ;
5 c o n t a i n e r . add ( b u t ) ;
6 (new CounterThread ( ) ) . s t a r t ( ) ;
7 }
8 v o i d s t a r t ( ) { s t o p p i n g = f a l s e; b u t . r e s e t T e x t ( ) ; }
9 v o i d s t o p ( ) { s t o p p i n g = t r u e; b u t . r e s e t T e x t ( ) ; }
10 c l a s s C o n t r o l B u t t o n extends J B u t t o n {
11 C o n t r o l B u t t o n ( ) { r e s e t T e x t ( ) ; }
12 v o i d r e s e t T e x t ( ) { s e t T e x t ( s t o p p i n g ? " S t a r t " : " Stop " ) ; }
13 }
14 }
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 10 / 12
A Control Button that Can Be Pressed
1 i m p o r t j a v a . awt . ev ent . ∗ ;
2 c l a s s CounterLa bel extends J L a b e l {
3 c l a s s C o n t r o l B u t t o n extends J B u t t o n {
4 C o n t r o l B u t t o n ( ) {
5 r e s e t T e x t ( ) ;
6 a d d A c t i o n L i s t e n e r (new A c t i o n L i s t e n e r ( ) {
7 p u b l i c v o i d a c t i o n P e r f o r m e d ( A c t i o n E v e n t e ) {
8 s t o p p i n g = ! s t o p p i n g ;
9 r e s e t T e x t ( ) ;
10 }
11 } ) ;
12 }
13 v o i d r e s e t T e x t ( ) { s e t T e x t ( s t o p p i n g ? " S t a r t " : " Stop " ) ; }
14 }
15 }
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 11 / 12
Event Driven Programming
throw-exception-catch event: any Throwable event generator: throw event handler: catch
handler registration: write after try component-action-listener
event: ActionEvent
event generator: some component event handler: ActionListener
handler registration: addActionListener component-mouse-listener
event: MouseEvent
event generator: some component event handler: MouseListener
handler registration: addMouseListener ...
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 12 / 12
Event Driven Programming
throw-exception-catch event: any Throwable event generator: throw event handler: catch
handler registration: write after try component-action-listener
event: ActionEvent
event generator: some component event handler: ActionListener
handler registration: addActionListener component-mouse-listener
event: MouseEvent
event generator: some component event handler: MouseListener
handler registration: addMouseListener ...
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 12 / 12
Event Driven Programming
throw-exception-catch event: any Throwable event generator: throw event handler: catch
handler registration: write after try component-action-listener
event: ActionEvent
event generator: some component event handler: ActionListener
handler registration: addActionListener component-mouse-listener
event: MouseEvent
event generator: some component event handler: MouseListener
handler registration: addMouseListener ...
H.-T. Lin (NTU CSIE) Swings and Inner Classes OOP 06/14-06/15/2010 12 / 12