S 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)
{
JOptionPane.showMessageDialog(opt,"ChitChat_Broadcast_Messenger\nCopyright
Fatima_Ahmed","About_ChitChat_Broadcast_Messenger",J
OptionPane.INFORMATION_MESSAGE);
}
}
}
Figure 2: ABOUT对话框
用Java Socket制作广播信使程序(3)
时间:2010-09-13
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类:
Figure 3: Client信使窗口询问每次连接初始化的用户登陆名
Figure 4: 一个客户机窗口,包含一些字体,颜色选取框和一个菜单来控制窗口。
用Java Socket制作广播信使程序(4)
时间:2010-09-13
在Client类中导入以下文件。我们已经创建了另一个类“SketchFrame”,它是用来定义一些客户机窗口的interface的。我们描叙一下JAVA中的Socket类的一些基本功能,并在客户端线程上实现了start ( ), run ( ),和stop ( )方法。这个类有以下导入的文件:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ChatClient implements Runnable, WindowListener,
ActionListener, ListSelectionListener {
protected String host;
protected int port;
public Text
|