Skip to content

Commit 899f474

Browse files
authored
Add files via upload
1 parent fcc5d71 commit 899f474

File tree

93 files changed

+7485
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+7485
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Chapter 10: Example 1
2+
// illustrate event handling using listener interfaces
3+
4+
import java.awt.*;
5+
import java.awt.event.*;
6+
7+
public class AWTFrameWithAButton extends Frame implements ActionListener, WindowListener
8+
{
9+
public static void main(String args[])
10+
{ // create instance of Frame
11+
AWTFrameWithAButton frameWithButton = new AWTFrameWithAButton();
12+
}
13+
14+
// constructor
15+
public AWTFrameWithAButton()
16+
{ // create instance of Button
17+
Button closeButton = new Button("Close");
18+
// set FlowLayout as the layout manager
19+
this.setLayout(new FlowLayout());
20+
// place Button on Frame instance
21+
this.add(closeButton);
22+
// establish size, create title & make it visible
23+
this.setSize(300,150);
24+
this.setTitle("AWT Frame With A Button");
25+
this.setVisible(true);
26+
// register frame as listener for button event
27+
closeButton.addActionListener(this);
28+
// register frame as listener for frame event
29+
this.addWindowListener(this);
30+
31+
}
32+
// actionPerformed is invoked when closeButton is clicked
33+
public void actionPerformed(ActionEvent e)
34+
{ shutDown(); }
35+
36+
// The following 7 methods are required because we implemented WindowListener
37+
// windowClosing invoked when window closed
38+
public void windowClosing(WindowEvent event)
39+
{ shutDown(); }
40+
41+
public void windowClosed(WindowEvent event){}
42+
public void windowDeiconified(WindowEvent event){}
43+
public void windowIconified(WindowEvent event){}
44+
public void windowActivated(WindowEvent event){}
45+
public void windowDeactivated(WindowEvent event){}
46+
public void windowOpened(WindowEvent event){}
47+
48+
// terminate
49+
public void shutDown()
50+
{
51+
this.dispose();
52+
System.exit(0);
53+
}
54+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Chapter 10: Example 2
2+
// illustrate event handling using an adapter class
3+
4+
import java.awt.*;
5+
import java.awt.event.*;
6+
public class AWTFrameWithAButton extends Frame implements ActionListener
7+
{
8+
public static void main(String args[])
9+
{ // create instance of Frame
10+
AWTFrameWithAButton frameWithButton = new AWTFrameWithAButton();
11+
}
12+
13+
// constructor
14+
public AWTFrameWithAButton()
15+
{ // create instance of Button
16+
Button closeButton = new Button("Close");
17+
// set FlowLayout as the layout manager
18+
this.setLayout(new FlowLayout());
19+
// place Button on Frame instance
20+
this.add(closeButton);
21+
// establish size, create title & make it visible
22+
this.setSize(300,150);
23+
this.setTitle("AWT Frame With A Button");
24+
this.setVisible(true);
25+
// register frame as listener for button event
26+
closeButton.addActionListener(this);
27+
28+
// create instance of WindowCloser
29+
WindowCloser eventHandler = new WindowCloser(this);
30+
31+
// register WindowCloser as listener for frame event
32+
this.addWindowListener(eventHandler);
33+
}
34+
35+
// actionPerformed is invoked when closeButton is clicked
36+
public void actionPerformed(ActionEvent e)
37+
{ shutDown(); }
38+
39+
public void shutDown()
40+
{
41+
this.dispose();
42+
System.exit(0); // terminate
43+
}
44+
}
45+
46+
// WindowCloser is a subclass of WindowAdapter
47+
class WindowCloser extends WindowAdapter
48+
{
49+
AWTFrameWithAButton frameToClose;
50+
// constructor populates reference to frame instance
51+
public WindowCloser(AWTFrameWithAButton frame)
52+
{ frameToClose = frame;}
53+
54+
// windowClosing is invoked when window closed
55+
// this method overrides windowClosing in WindowAdapter
56+
public void windowClosing(WindowEvent event)
57+
{ frameToClose.shutDown(); }
58+
}
59+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Chapter 10: Example 3
2+
// illustrate event handling using an anonymous inner class
3+
4+
import java.awt.*;
5+
import java.awt.event.*;
6+
public class AWTFrameWithAButton extends Frame implements ActionListener
7+
{
8+
public static void main(String args[])
9+
{ // create instance of Frame
10+
AWTFrameWithAButton frameWithButton = new AWTFrameWithAButton();
11+
}
12+
13+
// constructor
14+
public AWTFrameWithAButton()
15+
{ // create instance of Button
16+
Button closeButton = new Button("Close");
17+
// set FlowLayout as the layout manager
18+
this.setLayout(new FlowLayout());
19+
// place Button on Frame instance
20+
this.add(closeButton);
21+
// establish size, create title & make it visible
22+
this.setSize(300,150);
23+
this.setTitle("AWT Frame With A Button");
24+
this.setVisible(true);
25+
// register frame as listener for button event
26+
closeButton.addActionListener(this);
27+
28+
// create anonymous inner class to handle window closing event
29+
// register the inner class as a listener with the frame
30+
this.addWindowListener
31+
( // begin inner class definition
32+
new WindowAdapter() // superclass of inner class is WindowAdapter
33+
{
34+
public void windowClosing(WindowEvent event)
35+
{shutDown();} // invoke shutDown in outer class
36+
}// end of inner class definition
37+
); // end of argument sent to addWindowListener method
38+
}
39+
40+
// actionPerformed is invoked when closeButton is clicked
41+
public void actionPerformed(ActionEvent e)
42+
{ shutDown(); }
43+
44+
public void shutDown()
45+
{
46+
this.dispose();
47+
System.exit(0); // terminate
48+
}
49+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Chapter 10: Example 4
2+
// illustrate multiple AWT components
3+
4+
import java.awt.*;
5+
import java.awt.event.*;
6+
7+
public class AWTFrameAndComponents extends Frame implements ActionListener
8+
{
9+
// Button & TextField reference variables need class scope
10+
Button displayButton, clearButton, closeButton;
11+
TextField messageTextField;
12+
13+
public static void main(String args[])
14+
{ // create instance of AWTFrameAndComponents
15+
AWTFrameAndComponents frameAndCompnents = new AWTFrameAndComponents();
16+
}
17+
18+
// constructor
19+
public AWTFrameAndComponents()
20+
{ // create Button, Label and TextField instances
21+
displayButton = new Button("Display");
22+
clearButton = new Button("Clear");
23+
closeButton = new Button("Close");
24+
Label messageLabel = new Label("Message:");
25+
messageTextField = new TextField(15);
26+
// create two Panels - default FlowLayout manager
27+
Panel upperPanel = new Panel();
28+
Panel lowerPanel = new Panel();
29+
// add label & textfield to upper panel
30+
upperPanel.add(messageLabel);
31+
upperPanel.add(messageTextField);
32+
// add buttons to lower panel
33+
lowerPanel.add(displayButton);
34+
lowerPanel.add(clearButton);
35+
lowerPanel.add(closeButton);
36+
// add panels to frame - frame default is BorderLayout
37+
this.add("North",upperPanel);
38+
this.add("South",lowerPanel);
39+
// register frame as event listener with all buttons
40+
displayButton.addActionListener(this);
41+
clearButton.addActionListener(this);
42+
closeButton.addActionListener(this);
43+
this.setSize(320,150);
44+
this.setTitle("AWT Frame and Components");
45+
this.setVisible(true);
46+
47+
// create anonymous inner class to handle window closing event
48+
// register the inner class as a listener with the frame
49+
this.addWindowListener
50+
( // begin inner class definition
51+
new WindowAdapter() // superclass of inner class is WindowAdapter
52+
{
53+
public void windowClosing(WindowEvent event)
54+
{shutDown();} // invoke shutDown in outer class
55+
}// end of inner class definition
56+
); // end of argument sent to addWindowListener method
57+
}
58+
59+
// actionPerformed is invoked when a Button is clicked
60+
public void actionPerformed(ActionEvent e)
61+
{ // see which button was clicked
62+
if(e.getSource() == displayButton)
63+
displayMessage();
64+
if(e.getSource() == clearButton)
65+
clearMessage();
66+
if(e.getSource() == closeButton)
67+
shutDown();
68+
}
69+
public void displayMessage()
70+
{ messageTextField.setText("Hello World"); }
71+
public void clearMessage()
72+
{ messageTextField.setText(" "); }
73+
public void shutDown()
74+
{
75+
this.dispose();
76+
System.exit(0); // terminate
77+
}
78+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Chapter 10: Figure 10-3
2+
// illustrate Frame with a button
3+
4+
import java.awt.*;
5+
import java.awt.event.*;
6+
7+
public class AWTFrameWithAButton extends Frame implements ActionListener, WindowListener
8+
{
9+
public static void main(String args[])
10+
{ // create instance of Frame
11+
AWTFrameWithAButton frameWithButton = new AWTFrameWithAButton();
12+
}
13+
14+
// constructor
15+
public AWTFrameWithAButton()
16+
{ // create instance of Button
17+
Button closeButton = new Button("Close");
18+
// BorderLayout is the default layout manager
19+
// place Button on Frame instance
20+
this.add(closeButton);
21+
// establish size, create title & make it visible
22+
this.setSize(300,150);
23+
this.setTitle("AWT Frame With A Button");
24+
this.setVisible(true);
25+
// register frame as listener for button event
26+
closeButton.addActionListener(this);
27+
// register frame as listener for frame event
28+
this.addWindowListener(this);
29+
30+
}
31+
// actionPerformed is invoked when closeButton is clicked
32+
public void actionPerformed(ActionEvent e)
33+
{ shutDown(); }
34+
35+
// The following 7 methods are required because we implemented WindowListener
36+
// windowClosing invoked when window closed
37+
public void windowClosing(WindowEvent event)
38+
{ shutDown(); }
39+
40+
public void windowClosed(WindowEvent event){}
41+
public void windowDeiconified(WindowEvent event){}
42+
public void windowIconified(WindowEvent event){}
43+
public void windowActivated(WindowEvent event){}
44+
public void windowDeactivated(WindowEvent event){}
45+
public void windowOpened(WindowEvent event){}
46+
47+
// terminate
48+
public void shutDown()
49+
{
50+
this.dispose();
51+
System.exit(0);
52+
}
53+
}

Chapter10/Example5/AWTApplet.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<APPLET
2+
CODE="AWTApplet.class"
3+
WIDTH="300" HEIGHT="100"
4+
>
5+
</APPLET>

Chapter10/Example5/AWTApplet.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Chapter 10: Example 5
2+
// illustrate AWT Applet
3+
4+
import java.awt.*;
5+
import java.awt.event.*;
6+
import java.applet.*;
7+
8+
public class AWTApplet extends Applet implements ActionListener
9+
{
10+
// Button & TextField reference variables need class scope
11+
Button displayButton, clearButton, closeButton;
12+
TextField messageTextField;
13+
14+
// constructor
15+
public void init()
16+
{
17+
// create Button, Label and TextField instances
18+
displayButton = new Button("Display");
19+
clearButton = new Button("Clear");
20+
Label messageLabel = new Label("Message:");
21+
messageTextField = new TextField(15);
22+
// create two Panels - default FlowLayout manager
23+
Panel upperPanel = new Panel();
24+
Panel lowerPanel = new Panel();
25+
// add label & textfield to upper panel
26+
upperPanel.add(messageLabel);
27+
upperPanel.add(messageTextField);
28+
// add buttons to lower panel
29+
lowerPanel.add(displayButton);
30+
lowerPanel.add(clearButton);
31+
// add panels to frame - frame default is BorderLayout
32+
this.add("North",upperPanel);
33+
this.add("South",lowerPanel);
34+
// register frame as listener for button events
35+
displayButton.addActionListener(this);
36+
clearButton.addActionListener(this);
37+
}
38+
39+
// actionPerformed is invoked when a Button is clicked
40+
public void actionPerformed(ActionEvent e)
41+
{ // see which button was clicked
42+
if(e.getSource() == displayButton)
43+
displayMessage();
44+
if(e.getSource() == clearButton)
45+
clearMessage();
46+
}
47+
public void displayMessage()
48+
{ messageTextField.setText("Hello World"); }
49+
public void clearMessage()
50+
{ messageTextField.setText(" "); }
51+
}

0 commit comments

Comments
 (0)