?? frame1.java~180~
字號:
package chatserver;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.net.*;//需要增加的包
import java.io.*;
import java.util.Vector;
import java.util.StringTokenizer;
import java.lang.Thread;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
BorderLayout borderLayout1 = new BorderLayout();
BorderLayout borderLayout2 = new BorderLayout();
JPanel jPanel3 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
PaneLayout paneLayout1 = new PaneLayout();
JPanel jPanel4 = new JPanel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
PaneLayout paneLayout2 = new PaneLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
JTextArea jTextArea2 = new JTextArea();
JPanel jPanel5 = new JPanel();
JTextField jTextField1 = new JTextField();
PaneLayout paneLayout3 = new PaneLayout();
JButton send = new JButton();
JButton sysexit = new JButton();
JTextField jTextField2 = new JTextField();
java.awt.List list1 = new java.awt.List();
JButton link = new JButton();//用來列出目前連接的客戶數
//定義相關變量
static ServerSocket serverSocket=null;//建立服務器的socket
Socket socket=null; //用來存儲一個連接套接字
BufferedReader cin=null;//用來實現接受從客戶端發來的數據流
PrintWriter cout=null;//用來實現向客戶端發送信息的流
static Vector clients=new Vector(20); //用vector向量數組存儲連接客戶變量
static int active_connects=0;//記錄目前連接的客戶數
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setLocale(java.util.Locale.getDefault());
this.setSize(new Dimension(407, 379));
this.setTitle("網絡聊天服務器");
jPanel1.setLayout(borderLayout1);
jPanel2.setLayout(borderLayout2);
jLabel1.setBorder(BorderFactory.createEtchedBorder());
jLabel1.setText("用戶名");
jLabel2.setBorder(BorderFactory.createEtchedBorder());
jLabel2.setText("IP地址");
jLabel3.setBorder(BorderFactory.createEtchedBorder());
jLabel3.setToolTipText("");
jLabel3.setText("連接情況");
jPanel3.setLayout(paneLayout1);
jLabel4.setBorder(BorderFactory.createEtchedBorder());
jLabel4.setText("信息類型");
jLabel5.setBorder(BorderFactory.createEtchedBorder());
jLabel5.setText("信息來源");
jLabel6.setBorder(BorderFactory.createEtchedBorder());
jLabel6.setText("信息內容");
jPanel4.setLayout(paneLayout2);
jTextField1.setEditable(false);
jTextField1.setText("");
jPanel5.setLayout(paneLayout3);
send.setText("發送");
send.addActionListener(new Frame1_send_actionAdapter(this));
sysexit.setText("退出");
sysexit.addActionListener(new Frame1_sysexit_actionAdapter(this));
jTextArea2.setText("");
link.setText("連接");
link.addActionListener(new Frame1_link_actionAdapter(this));
contentPane.add(jPanel1, new XYConstraints(-2, 0, 402, 152));
jPanel1.add(jPanel3, BorderLayout.NORTH);
jPanel1.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(list1, null);
contentPane.add(jPanel2, new XYConstraints(-1, 160, 401, 140));
jPanel2.add(jPanel4, BorderLayout.NORTH);
jPanel4.add(jLabel4, new PaneConstraints("jLabel4", "jLabel4", PaneConstraints.ROOT, 0.5f));
jPanel4.add(jLabel5, new PaneConstraints("jLabel5", "jLabel4", PaneConstraints.RIGHT, 0.7431421f));
jPanel4.add(jLabel6, new PaneConstraints("jLabel6", "jLabel5", PaneConstraints.RIGHT, 0.36577183f));
jPanel2.add(jScrollPane2, BorderLayout.CENTER);
jPanel2.add(jPanel5, BorderLayout.SOUTH);
contentPane.add(jTextField2, new XYConstraints(1, 295, 306, 31));
contentPane.add(jTextField1, new XYConstraints(2, 329, 242, -1));
contentPane.add(send, new XYConstraints(328, 301, 60, 22));
contentPane.add(sysexit, new XYConstraints(329, 329, 60, 22));
contentPane.add(link, new XYConstraints(261, 330, 56, 22));
jScrollPane2.getViewport().add(jTextArea2, null);
jPanel3.add(jLabel1, new PaneConstraints("jLabel1", "jLabel1", PaneConstraints.ROOT, 0.5f));
jPanel3.add(jLabel2, new PaneConstraints("jLabel2", "jLabel1", PaneConstraints.RIGHT, 0.8557214f));
jPanel3.add(jLabel3, new PaneConstraints("jLabel3", "jLabel2", PaneConstraints.RIGHT, 0.5826087f));
}
//File | Exit action performed
public void jMenuItemClear_actionPerformed(ActionEvent e) {
System.exit(0);
}
//Help | About action performed
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuItemClear_actionPerformed(null);
}
}
//添加的方法!!!!
public static void notifyRoom(){//監視聊天窗口的連接變化,不
//斷刷新clients數組并刷新用戶列表信息
StringBuffer people=new StringBuffer("PEOPLE");
//增加PEOPLE表示是客戶列表信息
for(int i=0;i<clients.size();i++)
{
Client c=(Client)clients.elementAt(i);
people.append(":"+c.name);
}
sendClients(people);
}
public static boolean checkName(Client newClient)
{
for(int i=0;i<clients.size();i++)
{
Client c=(Client)clients.elementAt(i);
if((c!=newClient)&&c.equals(newClient.name))
return false;
}
return true;
}
public synchronized void disconnect(Client c)//缺少static
{
try{
jTextArea2.append("系統消息: "+c.name+" 斷開連接\n");
active_connects--; //連接數減1
c.send(new StringBuffer("QUIT"+":"+c.name));
//向客戶發送斷開信息
clients.removeElement(c);
jTextField1.setText("目前已經有"+clients.size()+"用戶連接");
c.socket.close();//斷開連接
}
catch(IOException e)
{
jTextArea2.append("系統消息:"+"Error "+e);
}
}
public static synchronized void sendClients(StringBuffer msg)
{//發送信息到所有用戶
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
c.send(msg);
}
}
public synchronized void sendtoClient(StringBuffer msg,String name)
//將信息發送到特定的單個用戶
{
for(int i=0;i<clients.size();i++)
{
Client c=(Client)clients.elementAt(i);
if(c.name.equalsIgnoreCase(name))
{
jTextArea2.append(name);
c.send(msg);
// break;
}
}
}
public void closeAll()//關閉所有游的連接信息
{
while(clients.size()>0)
//需要在整個數組中將每一個連接遍歷,然后刪除信息
{
Client c=(Client)clients.firstElement();
try{
c.socket.close();
}
catch(IOException e){
jTextArea2.append("系統消息:"+" 聊天服務器"+ " Error "+e);
}
finally{
clients.removeElement(c);
//從clients數組中刪除客戶相關的socket信息
}
}
}
class Client extends Thread{
String name; //用來存儲客戶的連接姓名
String ip; //用來存儲客戶的連接IP
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -