Java Reference
In-Depth Information
Lab 12.3: The Instant Message GUI
The purpose of this lab is to become familiar with using layout managers
and panels to lay out components in a window. This lab, which is a con-
tinuation of Lab 12.2, uses an object-oriented approach to creating the
GUI. I will have you write separate methods for each nested container.
1. Open the source code file of your InstantMessageFrame class from
Lab 12.2.
2. Add the following three fields to the InstantMessageFrame class: a
JTextField named message, a JList named friends, and a JButton
named send.
3. Add a method to your InstantMessageFrame class named getMes-
sagePanel() that returns a JPanel and has no parameters. (This
method will create a JPanel that will appear in the south border of
your JFrame. It will contain a text field in which a message can be
entered and a button that sends an instant message to all the friends
in the list.)
4. Within getMessagePanel(), instantiate a new JPanel and give it Bor-
derLayout. Assign a message equal to a new JTextField by using the
no-argument constructor of JTextField. Assign send to a new JBut-
ton, passing in “Send” to the constructor. (This will be the label on
the button.)
5. Within getMessagePanel(), add the message text field to the center
of the JPanel and add the send button to the east border of the
JPanel. The panel is now ready, so return the JPanel reference.
6. Add a method to your InstantMessageFrame class named get-
FriendsPane() that returns a JScrollPane and has no parameters.
(This method will create a scrollable list that will contain the names
of others that you can chat with.)
7. Within getFriendsPane(), assign friends to a new JList by using the
no-argument constructor of JList.
8. Within getFriendsPane(), instantiate a new JScrollPane using the fol-
lowing statement:
JScrollPane pane = new JScrollPane(friends);
9. The scroll pane is ready, so return the reference pane at the end of
getFriendsPane().
10. Within the constructor of InstantMessageFrame, invoke getMes-
sagePanel(), placing the returned panel in the south border of the
content pane of InstantMessageFrame.
Search WWH ::




Custom Search