Java Reference
In-Depth Information
Naming Menu Components
Use the prefix, mnu, in the variable name for menu components.
Follow the prefix with the Menu's location and a keyword. For
example, the Copy command on the Edit menu might be named
mnuEditCopy.
Creating the Content Pane
You learned earlier that a content pane is a special-purpose container similar to
a Panel. Content panes hold individual components and are added to and,
therefore, contained by the JFrame. Many programmers and developers write a
method to create the content pane to prevent its components from being con-
fused with another part of the program.
Coding the createContentPane() Method
The createContentPane() method accepts no arguments, but it returns a
Container object. Container is the superclass for all containers in the API. Inside
the Classics on DVD program content pane, there will be a JPanel in the north
with a JComboBox and JLabel, and a JPanel in the center with a JTextPane and
JScrollPane.
Figure 7-19 displays the code for the createContentPane() method.
94
//create the content pane
95
public Container createContentPane ()
96
{
97
//populate the JComboBox
98
fieldCombo.addItem ( "Title" ) ;
99
fieldCombo.addItem ( "Studio" ) ;
100
fieldCombo.addItem ( "Year" ) ;
101
fieldCombo.addActionListener ( this ) ;
102
fieldCombo.setToolTipText ( "Click the drop-down arrow to display sort fields." ) ;
103
104
//construct and populate the north panel
105
JPanel northPanel = new JPanel () ;
106
northPanel.setLayout ( new FlowLayout ()) ;
107
northPanel.add ( sortPrompt ) ;
108
northPanel.add ( fieldCombo ) ;
109
110
//create the JTextPane and center panel
111
JPanel centerPanel = new JPanel () ;
112
setTabsAndStyles ( textPane ) ;
113
textPane = addTextToTextPane () ;
114
JScrollPane scrollPane = new JScrollPane ( textPane ) ;
115
scrollPane.setVerticalScrollBarPolicy ( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ) ;
116
scrollPane.setPreferredSize ( new Dimension ( 500, 200 )) ;
117
centerPanel.add ( scrollPane ) ;
118
119
//create Container and set attributes
120
Container c = getContentPane () ;
121
c.setLayout ( new BorderLayout ( 10,10 )) ;
122
c.add ( northPanel, BorderLayout .NORTH ) ;
123
c.add ( centerPanel, BorderLayout .CENTER ) ;
124
125
return c;
126
}
127
FIGURE 7-19
Search WWH ::




Custom Search