日期:2014-05-20  浏览次数:20711 次

麻烦各位前辈解决一下,怎样让client连接到服务器。
这是一个聊天室client端,怎样连接到服务器,求各位解决。

import javax.swing.SwingUtilities;
import java.util.Date;
import java.awt.BorderLayout;
import java.text.*;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
//import java.awt.Point;
import java.awt.Color;
import javax.swing.JTextArea;
import javax.swing.JLabel;
//import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import java.awt.ComponentOrientation;
import java.awt.Rectangle;
import java.net.InetAddress;

import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.xml.crypto.Data;
import java.net.UnknownHostException;
//import java.sql.Date;
public class UserFrm extends JFrame {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JLabel lalOnline = null;

private JPanel pnlsend = null;

private JLabel lblSpeak = null;

private JTextField txtSend = null;

private JButton btnSend = null;

private JLabel lblIp = null;

private JButton btnSmile = null;

private JScrollPane slpScroll = null;

private JTextArea txaTalk = null;
/**
 * This method initializes pnlsend
 * 
 * @return javax.swing.JPanel
 */
private JPanel getPnlsend() {
if (pnlsend == null) {
FlowLayout flowLayout = new FlowLayout(); flowLayout.setAlignment(java.awt.FlowLayout.CENTER);
flowLayout.setVgap(13);
flowLayout.setHgap(31);
lblIp = new JLabel();
String a,b;
try {
InetAddress ip = InetAddress.getLocalHost();
a = ip.getHostAddress();
b = ip.getHostName();
//c = ip.getHostAddress();
System.out.println("IP地址:"+a);
System.out.println("主机名:"+b);
// InetAddress net = InetAddress.getByName("www.baidu.com "); // 根据域名获取IP地址
// System.out.println(net);


lblIp.setText("本机IP地址:"+a);
lblIp.setPreferredSize(new Dimension(155, 22));
lblIp.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
}catch (UnknownHostException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
lblSpeak = new JLabel();
lblSpeak.setText("发言:");
lblSpeak.setDisabledIcon(new ImageIcon("F:/000.jpg"));
lblSpeak.setFont(new Font("\u5b8b\u4f53", Font.BOLD, 12));
pnlsend = new JPanel();
pnlsend.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
pnlsend.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14));
pnlsend.setLayout(flowLayout);
pnlsend.setPreferredSize(new Dimension(120, 50));
pnlsend.add(getBtnSmile(), null);
pnlsend.add(lblSpeak, null);
pnlsend.add(getTxtSend(), null);
pnlsend.add(getBtnSend(), null);
pnlsend.add(lblIp, null);
}
return pnlsend;
}

/**
 * This method initializes txtSend
 * 
 * @return javax.swing.JTextField
 */
private JTextField getTxtSend() {
if (txtSend == null) {
txtSend = new JTextField();
txtSend.setPreferredSize(new Dimension(178, 25));
txtSend.setFont(new Font("\u5b8b\u4f53", Font.BOLD, 12));
txtSend.setText("欢迎来到北科大QQ聊天室 !");
long m =System.currentTimeMillis();  // 获取当前时间
System.out.println("当前时间 :"+new Date(m));
//txtSend.setText("");
txtSend.setVisible(true);
}
return txtSend;
}


/**
 * This method initializes btnSend
 * 
 * @return javax.swing.JButton
 */
private JButton getBtnSend() {
if (btnSend == null) {
btnSend = new JButton();
btnSend.setPreferredSize(new Dimension(100, 22));
btnSend.setFont(new Font("\u5b8b\u4f53", Font.BOLD, 12));
btnSend.setActionCommand(" 发  送");
btnSend.setText(" 发 送");
btnSend.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
 
//  处理事件
// long m =System.currentTimeMillis();  // 获取当前时间
// System.out.println("当前时间 :"+new Date(m));
/********************************************/
Date now = new Date();   // 获取当前时间
DateFormat t = DateFormat.getDateTimeInstance();