-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEventByDerived.java
More file actions
49 lines (42 loc) · 1.18 KB
/
EventByDerived.java
File metadata and controls
49 lines (42 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.awt.event.*;
import javax.swing.*;
public class EventByDerived extends JFrame
{
public EventByDerived()
{
// create basic panel
JPanel panel = new JPanel();
panel.setLayout( null );
// create example button
// events are handled via a derived inner class
// MyButton class will specify the event listener, so no need for that here
MyButton closeButton = new MyButton( "Close" );
closeButton.setBounds( 40, 50, 80, 25 );
// add components to frame
panel.add( closeButton );
add( panel );
// set frame attributes
setTitle( "Example" );
setSize( 300, 200 );
setLocationRelativeTo( null );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
// this is the inner derived class
class MyButton extends JButton implements ActionListener
{
public MyButton( String text )
{
super.setText( text ); // asks JButton to put text as its text
addActionListener( this );
}
public void actionPerformed( ActionEvent e )
{
System.exit( 0 );
}
}
public static void main( String[] args )
{
EventByDerived ex = new EventByDerived();
ex.setVisible( true );
}
}