课 程 设 计 报 告 书 专 用 纸
int i =0;
combobox.addItem(\所有人\while(i < usernames.length){ combobox.addItem(usernames[i]); }
i ++;
}
this.combobox = combobox; this.textarea = textarea; this.showStatus = showStatus;
public void run(){
while(!socket.isClosed()){
try{
String type = (String)input.readObject(); if(type.equalsIgnoreCase(\系统信息\
String sysmsg = (String)input.readObject(); textarea.append(\系统信息: \
}
else if(type.equalsIgnoreCase(\服务关闭\ output.close(); }
else if(type.equalsIgnoreCase(\聊天信息\ String message = (String)input.readObject(); }
textarea.append(message); input.close(); socket.close();
textarea.append(\服务器已关闭!\\n\break;
else if(type.equalsIgnoreCase(\用户列表\ String userlist = (String)input.readObject(); String usernames[] = userlist.split(\ combobox.removeAllItems();
combobox.setSelectedIndex(0);
showStatus.setText(\在线用户 \人\}
}
catch (Exception e ){ }
System.out.println(e);
第 35 页 共 18 页
课 程 设 计 报 告 书 专 用 纸
}
}
}
(3)ConnectConf.java:该类继承自JDialog,是用户对所要连接的服务器IP及侦听端口进行修改配置的类。
import java.awt.*; import java.net.*; import javax.swing.*; import java.awt.event.*; /**
* 生成连接信息输入的对话框 * 让用户输入连接服务器的IP和端口
*/
public class ConnectConf extends JDialog { /** * */
private static final long serialVersionUID = 1L; JPanel panelUserConf = new JPanel(); JButton save = new JButton();
JButton cancel = new JButton();
JLabel DLGINFO=new JLabel(\ 127.0.0.1:8888\
JPanel panelSave = new JPanel(); JLabel message = new JLabel(); String userInputIp; int userInputPort;
JTextField inputIp; JTextField inputPort;
public ConnectConf(JFrame frame,String ip,int port) { super(frame, true); this.userInputIp = ip; this.userInputPort = port; try {
jbInit();
}
catch (Exception e) { e.printStackTrace();
}
默认连接设置为 第 36 页 共 18 页
课 程 设 计 报 告 书 专 用 纸
panelUserConf.setLayout(new GridLayout(2,2,1,1)); panelUserConf.add(message);
panelUserConf.add(inputIp);
panelUserConf.add(new JLabel(\请输入服务器的端口号:\panelUserConf.add(inputPort);
panelSave.add(new Label(\panelSave.add(save); panelSave.add(cancel);
panelSave.add(new Label(\Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout());
contentPane.add(panelUserConf, BorderLayout.NORTH); contentPane.add(DLGINFO, BorderLayout.CENTER); contentPane.add(panelSave, BorderLayout.SOUTH); //保存按钮的事件处理 save.addActionListener( new ActionListener() {
public void actionPerformed (ActionEvent a) { int savePort;
//判断端口号是否合法 try{ userInputIp
=
\
+
private void jbInit() throws Exception {
this.setSize(new Dimension(300, 130)); this.setTitle(\连接设置\
message.setText(\请输入服务器的IP地址:\inputIp = new JTextField(10); inputIp.setText(userInputIp); inputPort = new JTextField(4);
inputPort.setText(\save.setText(\保存\cancel.setText(\取消\ }
//设置运行位置,使对话框居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); this.setLocation( (int) (screenSize.width - 400) / 2 + 50, (int) (screenSize.height - 600) / 2 + 150); this.setResizable(false);
InetAddress.getByName(inputIp.getText());
userInputIp = userInputIp.substring(1);
第 37 页 共 18 页
课 程 设 计 报 告 书 专 用 纸
}
catch(UnknownHostException e){
DLGINFO.setText( \错误的IP地
址!\
}
}
//userInputIp = inputIP; //判断端口号是否合法 try{ }
catch(NumberFormatException e){ DLGINFO.setText(\错误的端口号,端口号 }
inputPort.setText(\return;
savePort = Integer.parseInt(inputPort.getText()); if(savePort<1 || savePort>65535){ DLGINFO.setText(\侦听端口必须是 }
inputPort.setText(\return; return;
0-65535之间的整数!\
userInputPort = savePort; dispose();
请填写整数!\
);
}
//关闭对话框时的操作 this.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){
}
DLGINFO.setText(\ 默认连接设置为
127.0.0.1:8888\
);
}
//取消按钮的事件处理 cancel.addActionListener( new ActionListener(){
第 38 页 共 18 页
课 程 设 计 报 告 书 专 用 纸
public void actionPerformed(ActionEvent e){ }
DLGINFO.setText(\ 默认连接设置为 dispose();
127.0.0.1:8888\
}
}
});
(4)UserConf.java:该类继承自JDialog,是用户对连接到服务器所显示的用户名进行修改配置的类。
import java.awt.*; import javax.swing.*;
import java.awt.event.*; /**
* 生成用户信息输入对话框的类 * 让用户输入自己的用户名 */
public class UserConf extends JDialog { /** * */
private static final long serialVersionUID = 1L; JPanel panelUserConf = new JPanel(); JButton save = new JButton(); JButton cancel = new JButton();
JLabel DLGINFO=new JLabel(\客\
JPanel panelSave = new JPanel();
JLabel message = new JLabel(); String userInputName; JTextField userName ;
public UserConf(JFrame frame,String str) { super(frame, true);
this.userInputName = str; try { jbInit(); }
catch (Exception e) { e.printStackTrace(); }
//设置运行位置,使对话框居中
默认用户名为:匆匆过 第 39 页 共 18 页