Java Reference
In-Depth Information
5
public class PanelDemo extends JFrame
6
{
7
public static void main(String[] args)
8
{
9
PanelFrame panelFrame = new PanelFrame();
10
panelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
11
panelFrame.setSize( 450 , 200 );
12
panelFrame.setVisible( true );
13
}
14
} // end class PanelDemo
Fig. 12.46 | Testing PanelFrame . (Part 2 of 2.)
After JPanel buttonJPanel is declared (line 11 of Fig. 12.45) and created (line 19),
line 20 sets buttonJPanel 's layout to a GridLayout of one row and five columns (there
are five JButton s in array buttons ). Lines 23-27 add the JButton s in the array to the
JPanel . Line 26 adds the buttons directly to the JPanel —class JPanel does not have a
content pane, unlike a JFrame . Line 29 uses the JFrame 's default BorderLayout to add
buttonJPanel to the SOUTH region. The SOUTH region is as tall as the buttons on button-
JPanel . A JPanel is sized to the components it contains. As more components are added,
the JPanel grows (according to the restrictions of its layout manager) to accommodate
the components. Resize the window to see how the layout manager affects the size of the
JButton s.
12.20 JTextArea
A JTextArea provides an area for manipulating multiple lines of text . Like class JTextField ,
JTextArea is a subclass of JTextComponent , which declares common methods for JText-
Field s, JTextArea s and several other text-based GUI components.
The application in Figs. 12.47-12.48 demonstrates JTextArea s. One JTextArea dis-
plays text that the user can select. The other is uneditable by the user and is used to display
the text the user selected in the first JTextArea . Unlike JTextField s, JTextArea s do not
have action events—when you press Enter while typing in a JTextArea , the cursor simply
moves to the next line. As with multiple-selection JList s (Section 12.13), an external
event from another GUI component indicates when to process the text in a JTextArea .
For example, when typing an e-mail message, you normally click a Send button to send
the text of the message to the recipient. Similarly, when editing a document in a word pro-
cessor, you normally save the file by selecting a Save or Save As… menu item. In this pro-
gram, the button Copy >>> generates the external event that copies the selected text in the
left JTextArea and displays it in the right JTextArea .
 
 
Search WWH ::




Custom Search