public class ChatServer extends JFrame { public ChatServer(String title) //CONSTRUCTOR TO INITIALIZE THE //ChatServer CLASS { output = new TextArea (15,40); //output IS A TextArea COMPONENT //OF THE ChatServer CLASS output.setEditable (false); output.setFont(f); output.setForeground(Color.blue);
setTitle(title); //TO SET THE TITLE OF THE CLIENT WINDOW setJMenuBar(menuBar); //TO INITIALIZE THE MENU BAR ON THE WINDOW JMenu fileMenu = new JMenu("File"); JMenu colorMenu = new JMenu("Color"); JMenu helpMenu = new JMenu("Help");
//Main menu Shortcuts: fileMenu.setMnemonic('F'); colorMenu.setMnemonic('C'); helpMenu.setMnemonic('H');
//About Dialog init: aboutItem = new JMenuItem("About"); //aboutItem.addActionListener((ActionListener)this); helpMenu.add(aboutItem); addMenuItem(helpMenu,aboutAction = new AboutAction("About"));
//Initialize menu items: menuBar.add(fileMenu); menuBar.add(colorMenu); menuBar.add(helpMenu);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
class AboutAction extends AbstractAction //CREATES AN ABSTRACT
//INTERNAL CLASS FOR //About { JOptionPane opt; String name; public AboutAction(String Name) { this.name=Name; }
//About menu event: public void actionPerformed(ActionEvent ae) { //if(ae.getSource() == aboutAction) {
public static void main (String args[]) throws IOException {
ChatServer ServerWindow = new ChatServer("ChitChat Broadcast Messenger: Server Window"); //CREATES AN OBJECT OF SERVER Toolkit theKit = ServerWindow.getToolkit(); //TO CREATE AN OBJECT //OF ToolKit Dimension wndSize = theKit.getScreenSize();
ServerWindow.setBounds(wndSize.width/4,wndSize.height/4,wndSize.width/2,wndSize.height/2); ServerWindow.setVisible(true); ServerWindow.getContentPane().add ("North", output); //TO ADD THE TextArea (output) AT THE NORTH OF THE WINDOW ServerWindow.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER)); //TO SET THE LAYOUT AS CENTRALLY FLOW ServerWindow.pack(); //TO PACK THE SERVER WINDOW WITH ABOVE //INITIALIZE COMPONENTS
if (args.length != 1) throw new IllegalArgumentException ("Syntax: ChatServer<port>"); int port = Integer.parseInt (args[0]); String logins; ServerSocket server = new ServerSocket (port); //TO CREATE AN OBJECT FOR SERVER'S SOCKET while (true) { Socket client = server.accept (); //CALLS THE accept() //METHOD WHENEVER THE //CLIENTS REQUEST System.out.PRintln ("Accepted from " + client.getInetAddress ()+ " with name "+logins); ChatHandler handler = new ChatHandler (client,yourname); handler.start (); //THE BROADCASTING OF MESSAGES IS //STARTED BY start() METHOD output.append ("/n Accepted from " + client.getInetAddress ()+"/n"); } } socket是通过另一个类“ChatHandler”创建的,是包含在Demo Project文件中。现在,我们设计一个Client类:
public class ChatClient implements Runnable, WindowListener,
ActionListener, ListSelectionListener { protected String host; protected int port; public TextArea output; protected TextField input; String yourname; SketchFrame window;
public ChatClient (String host, int port, SketchFrame window) { //CONSTRUCTOR INITIALIZING THE ChatClient CLASS this.host = host; //host AND port WILL BE USED TO OPEN THE //SOCKET this.port = port; this.yourname=JOptionPane.showInputDialog("Enter Login name:"); //TO CREATE AN INPUT DIALOG BOX
window.setSize(100,100); //TO SET THE SIZE OF THE CLIENT //WINDOW window.getContentPane().add (output,BorderLayout.CENTER); //TO ADD TextArea (output) AT THE CENTER OF THE WINDOW window.getContentPane().add (input,BorderLayout.SOUTH); //TO ADD THE Textbox (input) AT THE BOTTOM (SOUTH)