Exercise 1: Creating The ChatClient GUI Part 1
(Level 3)
In this exercise, you will
create a GUI for a
chat room
application. You will use a complex
layout to position properly several GUI components in a frame.
Task 1 -
Creating the ChatClient
Program
Using a text editor, create the
ChatClient class source file in the project
directory. This class must implement teh GUI desing in Figure 10 - 1 on page
Lab 10 - 2.
1. Declare the ChatClient
class.
import java.awt.*;
public class ChatClient {
//insert code here
}
2. Add four atrributes to the
ChatClient class to hold the GUI
components.
private TextArea output;
private TextField input;
private Button sendButton;
private Button quitButton;
private TextArea output;
3.
Add a public constructor that initializes each of the
four GUI component attributes: The text area soukd be 10 rows tall and 50
columns wide, the text field should be 50 columns wide, the send button
should have word Send in the display, and the quit
button should display a similar label.
public ChatClient(){
output = new TextArea(10,50);
input = new TextField(50);
sendButton = new Button("Send");
quitButton = new Button("Quit");
}
4.
Create a launchFrame
method that constructs tha layout of the components. Feel free to use nested
panels and any layout managers that will help you construct the layout in
the GUI design shown above.
public void launchFrame(){
Frame frame = new Frame("Chat Room");
//Use the Border Layout for the frame
frame.setLayout( new BorderLayout());
frame.add(output, BorderLayout.WEST);
frame.add(input, BorderLayout.SOUTH);
//Create teh button panel
Panel p1 = new Panel();
p1.setLayout( new GridLayout(2,1));
p1.add(sendButton);
p1.add(quitButton);
//Add the button panel to the center
frame.add(p1, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
5.
Create teh main
method. This method will instantiate a new ChatClient
object and then call the launchFrame
method.
public static void main(String[] args){
ChatClient c = new ChatClient();
c.launchFrame();
}
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 10 - 1 on page Lab 10 - 2. If your GUI does
not look exactly like the figure, then the code to tweak the design to match
this figure.
java ChatClient
Note - Your GUI program has no
controller code to stop the program from runnig. To stop the program,
use the Control-C (for the Solaris OS) or Control-Z (for the Microsoft
Windows OS) key sequence. |