Java Reference
In-Depth Information
57
// Define components for panel 3
58
JLabel label3 =
new
JLabel
(
"User name: "
+user.getName
())
;
59
jbtLogout =
new
JButton
(
"Log out"
)
;
60
61
JPanel p3=
new
JPanel
()
;
62
p3.setLayout
(
new
GridLayout
(
1,2
))
;
63
p3.add
(
label3
)
;
64
p3.add
(
jbtLogout
)
;
65
p3.setBorder
(
new
TitledBorder
(
"Current user"
))
;
66
67
// Use nested panels for positioning
68
JPanel p4=
new
JPanel
()
;
69
p4.setLayout
(
new
GridLayout
(
3,1,10,5
))
;
// rows, cols, hgap, vgap
70
p4.add
(
p1
)
;
71
p4.add
(
p2
)
;
72
p4.add
(
p3
)
;
73
74
JPanel p5 =
new
JPanel
(
new
BorderLayout
(
10,10
))
;
75
p5.add
(
p4,
BorderLayout
.WEST
)
;
76
JPanel p6 =
new
JPanel
(
new
BorderLayout
(
10,10
))
;
77
p6.add
(
p5,
BorderLayout
.EAST
)
;
78
79
setContentPane
(
p6
)
;
80
81
// Register listeners
82
addWindowListener
(
this
)
;
83
jbtAddStock.addActionListener
(
this
)
;
84
jbtLogout.addActionListener
(
this
)
;
85
jcbStockList.addActionListener
(
this
)
;
86
87
// Prepare for display
88
pack
()
;
89
if
(
width < getWidth
())
// prevent setting width too small
90
width = getWidth
()
;
91
if
(
height < getHeight
())
// prevent setting height too small
92
height = getHeight
()
;
93
centerOnScreen
(
width, height
)
;
94
jtfStockSymbol.setText
(
""
)
;
95
jtfStockSymbol.requestFocus
()
;
96
}
97
98
public void
centerOnScreen
(
int
width,
int
height
)
99
{
100
int
top, left, x, y;
101
102
// Get the screen dimension
103
Dimension
screenSize =
Toolkit
.getDefaultToolkit
()
.getScreenSize
()
;
104
105
// Determine the location for the top left corner of the frame
106
x =
(
screenSize.width - width
)
/2;
107
y =
(
screenSize.height - height
)
/2;
108
left =
(
x<0
)
?0:x;
109
top =
(
y<0
)
?0:y;
110
111
this
.setBounds
(
left, top, width, height
)
;
112
}
113
114
public boolean
stockInList
(
String
stock
)
115
{
116
boolean
inList =
false
;
117
int
numItems;
118
119
numItems = jcbStockList.getItemCount
()
;
120
121
stock.trim
()
;
// remove any leading, trailing white space
122
FIGURE 10-41
(continued)
Search WWH ::
Custom Search