Java Reference
In-Depth Information
11. Within the constructor of InstantMessageFrame, invoke get-
FriendsPane(), placing the returned scroll pane in the center of the
content pane of InstantMessageFrame.
12. Save, compile, and run the InstantMessageFrame class.
Your InstantMessageFrame now has three visible components: a JList,
a JTextField, and a JButton.
Lab 12.4: Creating a Dialog Window
Sending an instant message is typically done using a dialog window in
which you enter the message in a text field. In this lab, you will write a
class to represent the GUI for this dialog window that will look similar to
that shown in Figure 12.14.
1. Write a class named InstantMessageDialog that extends JDialog.
2. Add three fields to the InstantMessageDialog class: a JButton named
send, a JButton named cancel, and a JTextField named message.
3. Add a constructor that has two parameters: a Frame to represent the
owner of the dialog and a String to represent the recipient of the
instant message. Pass the Frame parameter up to the parent con-
structor, as well as a String for the title of the window and true to
make it a modal dialog.
4. Within the constructor, initialize the two JButton fields to be a
“Send” and “Cancel” button. Initialize the message field to a new
JTextField by using the no-argument constructor of JTextField.
5. Add the two buttons to the south border of the dialog's content
pane, as shown in Figure 12.14.
6. Add the JTextField to a new JScrollPane using the statement:
JScrollPane center = new JScrollPane(message);
7. Add the center pane to the center of the dialog's content pane.
8. Add a JLabel to the north border of the dialog's content pane that
displays a message containing the name of the recipient, similar to
the label in Figure 12.14.
9. Set the size of the dialog window to be 400 pixels wide by 200 pixels
high.
10. Add a main() method to the InstantMessageDialog class that instan-
tiates a new InstantMessageDialog object. Pass in null for the owner
Search WWH ::




Custom Search