Exercise: Creating the Chatclient GUI, Part 3 (Level 3)
                   
In this exercise, you will enhance the GUI for a chat room application. You will complete
the GUI for the ChatClient
               
by adding a Choice component and two menus.
               
Task 1 - Modifying the ChatClient Program
-
Using a text editor, modify the ChatClient source file in the project directory. This class
must implement the GUI design in Figure 12-1 of Level 1.
-
Add the username Choice component under the Send and Quit buttons. This component
enables you to select a name that is posted with every chat line taht you enter. Add
several username options including your full name and a few nicknames, such as
Grand Poobah or Java Geek.
-
public class ChatClient {
     // existing code here
     private Choice usernames;
     // existing code here
     public ChatClient() {
         // more GUI components initialized
         usernames = new Choice();
         usernames.add("Bryan Basham");
         usernames.add("Grand Poobah");
         usernames.add("Java Geek");
    }
    // existing code here
}
-
Enhance the listeners for the Send button and TextField to write the username to the output
TextArea component.
-
private class SendHandler implements AciotnListener {
     public void actionPerformed(ActionEvent e) {
         String text = input.getText();
         output.append(usernames.getSelectedItem() + ": " + text "\n");
         input.setText("");
    }
}
-
Add the File menu. This menu must include a Quit menu item that terminates the program when
selected.
-
public void launchFrame {
     // existing code here
     // Create menu bar and File menu
     Menu mb = new MenuBar();
     Menu file = new Menu("File");
     MenuItem quitMenuItem = new ManuItem("Quit");
     quitMenuItem.addAciotnListener(new ActionListener()) {
         public void actionPerfiormed(ActionEvent e) {
             System.exit(0);
         }
     });
     file.add(quitMenuItem);
     mb.add(file);
     frame.setMenuBar(mb);
     // existing code here
}
-
(Optional) Add the Help menu. This menu must include an About menu item that pops up a simple
dialog box, which display a comment about the program and about you, the developer.
-
public void launchFrame() {
     // existing menu bar code here
     // Add help menu to menu bar
     Menu help = new Menu("Help");
     MenuItem aboutMenuItem = new MenuItem("About");
     aboutMenuItem.addActionListener(new AboutListener());
     help.add(aboutMenuItem);
    m.setHelpMenu(help);
     // existing code here
}
-
private class AboutHandler implements ActionListener {
     public void actionPerformed(ActionEvent e) {
         // Create the aboutDialog when it is requested
         if( aboutDialog == null) {
             aboutDialog = new AboutDialog(frame, "About", true);
         }
         aboutDialog.setVisible(true);
     }
}
private class AboutDialog extends Dialog implements ActionListener {
     public AboutDialog(Frame parent, String title, boolean modal) {
         super(parent, title, modal);
         add(new Label("The ChatClient is a neat tool that allows you" +
         " to talk to other ChatClients via a ChatServer"),
         BorderLayout.NORTH);
         Button b = new Button("OK");
         add(b, BorderLayout.SOUTH);
         b.addActionListener(this);
         pack();
     }
     // Hide the dialog box when the OK button is pushed
     public void actionPerformed(ActionEvent e) {
        setvisible(false);
     }
}
Task 2 - Compiling the ChatClient Program
-
On the command line, use the javac command to compile the ChatClient program.
javac ChatClient.java
Task 3 - Running the ChatClient Program
-
On the command line, use the java command to run the ChatClient program.
You should see the GUI shown in Figure 12-1 of Level 1. If your GUI does not look exactly like
the figure, then edit the code to tweak the design to match this figure.
java ChatClient