Java Reference
In-Depth Information
Using a Callback Mechanism in a Class
Because the StockFrame object is created by the LogonFrame, the
StockFrame must be coded first. Figure 10-41 displays the code for the
StockFrame class. Lines 9 through 14 import packages used by the class,
including those used to create the user interface.
1
/*
2
Chapter 10: The StockFrame Class
3
Programmer: Michael Mick
4
Date:
December 5, 2007
5
Filename:
StockFrame.java
6
Purpose:
Provides a user interface for a stock list to test the Password
class
7
*/
8
9
import
javax.swing.*;
10
import
java.awt.*;
11
import
java.awt.event.*;
12
import
java.util.*;
13
import
java.io.*;
14
import
javax.swing.border.TitledBorder;
15
16
class
StockFrame
extends
JFrame
implements
ActionListener
,
WindowListener
17
{
18
19
//define instance variables
20
String
stockSymbol;
21
22
JTextField jtfStockSymbol;
23
JButton jbtAddStock, jbtLogout;
24
JComboBox jcbStockList;
25
Activator caller;
26
27
public
StockFrame
(
User user, Activator callerObj
)
28
{
29
super
(
"Add Stock Symbols"
)
;
// call super (JFrame) constructor
30
31
int
width = 330;
32
int
height = 170;
33
caller = callerObj;
// save reference to caller object
34
35
// Define components for panel 1
36
JLabel label1 =
new
JLabel
(
"Symbol:"
)
;
37
jtfStockSymbol =
new
JTextField
(
4
)
;
38
jbtAddStock =
new
JButton
(
"Add Symbol"
)
;
39
40
JPanel p1=
new
JPanel
()
;
41
p1.setLayout
(
new
GridLayout
(
1,3
))
;
42
p1.add
(
label1
)
;
43
p1.add
(
jtfStockSymbol
)
;
44
p1.add
(
jbtAddStock
)
;
45
p1.setBorder
(
new
TitledBorder
(
"Add stock symbol"
))
;
46
47
// Define components for panel 2
48
JLabel label2 =
new
JLabel
(
"Click arrow to view list"
)
;
49
jcbStockList =
new
JComboBox
()
;
50
51
JPanel p2=
new
JPanel
()
;
52
p2.setLayout
(
new
GridLayout
(
1,2
))
;
53
p2.add
(
label2
)
;
54
p2.add
(
jcbStockList
)
;
55
p2.setBorder
(
new
TitledBorder
(
"View stock symbols"
))
;
56
57
// D fi
t f
l 3
FIGURE 10-41
Search WWH ::
Custom Search