-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path14.3.java.java
72 lines (55 loc) · 1.67 KB
/
14.3.java.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Fig. 14.3: CustomPanelTest.java
// Using a customized Panel object.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
private JPanel buttonPanel;
private Main myPanel;
private JButton circleButton, squareButton;
// set up GUI
public Main()
{
super( "CustomPanel Test" );
// create custom drawing area
myPanel = new Main();
myPanel.setBackground( Color.GREEN );
// set up squareButton
squareButton = new JButton( "Square" );
squareButton.addActionListener(
new ActionListener() { // anonymous inner class
// draw a square
public void actionPerformed( ActionEvent event )
{
myPanel.draw( Main.SQUARE );
}
} // end anonymous inner class
); // end call to addActionListener
circleButton = new JButton( "Circle" );
circleButton.addActionListener(
new ActionListener() { // anonymous inner class
// draw a circle
public void actionPerformed( ActionEvent event )
{
myPanel.draw( CustomPanel.CIRCLE );
}
} // end anonymous inner class
); // end call to addActionListener
// set up panel containing buttons
buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout( 1, 2 ) );
buttonPanel.add( circleButton );
buttonPanel.add( squareButton );
// attach button panel & custom drawing area to content pane
Container container = getContentPane();
container.add( myPanel, BorderLayout.CENTER );
container.add( buttonPanel, BorderLayout.SOUTH );
setSize( 300, 150 );
setVisible( true );
} // end constructor CustomPanelTest
public static void main( String args[] )
{
Main application = new Main();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class CustomPanelTest