java课程设计报告-聊天室(7)

2019-01-07 16:37

课 程 设 计 报 告 书 专 用 纸

girdBagCon = new GridBagConstraints(); girdBagCon.gridx = 1; girdBagCon.gridy = 3; girdBagCon.gridwidth = 3; girdBagCon.gridheight = 1;

girdBag.setConstraints(clientMessage,girdBagCon); downPanel.add(clientMessage);

girdBagCon = new GridBagConstraints(); girdBagCon.gridx = 4;

girdBagCon.gridy = 3;

girdBag.setConstraints(clientMessageButton,girdBagCon); downPanel.add(clientMessageButton); showStatus = new JTextField(35); showStatus.setEditable(false);

girdBagCon = new GridBagConstraints(); girdBagCon.gridx = 0; girdBagCon.gridy = 5;

girdBagCon.gridwidth = 5;

girdBag.setConstraints(showStatus,girdBagCon); downPanel.add(showStatus);

contentPane.add(messageScrollPane,BorderLayout.CENTER); contentPane.add(downPanel,BorderLayout.SOUTH); //关闭程序时的操作 this.addWindowListener(

new WindowAdapter(){ public void windowClosing(WindowEvent e){

if(type == 1){ DisConnect();

girdBagCon = new GridBagConstraints();

girdBagCon.gridx = 0; girdBagCon.gridy = 3;

girdBag.setConstraints(messageLabel,girdBagCon); downPanel.add(messageLabel);

girdBagCon.gridy = 2;

girdBagCon.insets = new Insets(1,0,0,0); //girdBagCon.ipadx = 5; //girdBagCon.ipady = 5;

girdBag.setConstraints(checkbox,girdBagCon); downPanel.add(checkbox);

第 30 页 共 18 页

课 程 设 计 报 告 书 专 用 纸

消息

//调出连接设置对话框

ConnectConf conConf = new ConnectConf(this,ip,port); conConf.setVisible(true); ip = conConf.userInputIp; port = conConf.userInputPort;

} /**

* 事件处理 */

public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if (obj == userItem || obj == userButton) { //用户信息设置

}

//调出用户信息设置对话框

UserConf userConf = new UserConf(this,userName); userConf.setVisible(true);

userName = userConf.userInputName;

}

System.exit(0);

} });

else if (obj == connectItem || obj == connectButton) { //连接服务端设

}

else if (obj == loginItem || obj == loginButton) { //登录 }

Connect();

else if (obj == logoffItem || obj == logoffButton) { //注销 DisConnect(); showStatus.setText(\}

else if (obj == clientMessage || obj == clientMessageButton) { //发送 }

SendMessage();

clientMessage.setText(\

else if (obj == exitButton || obj == exitItem) { //退出 int j=JOptionPane.showConfirmDialog(

this,\真的要退出吗?\退出\

JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE); if(type == 1){ DisConnect(); }

System.exit(0);

if (j == JOptionPane.YES_OPTION){

第 31 页 共 18 页

课 程 设 计 报 告 书 专 用 纸

提示\

}

JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE); return;

public void Connect(){ try{

}

catch (Exception e){ JOptionPane.showConfirmDialog( this,\不能连接到指定的服务器。\\n请确认连接设置是否正确。\

socket = new Socket(ip,port);

}

}

else if (obj == helpItem) { //菜单栏中的帮助 //调出帮助对话框 Help helpDialog = new Help(this); }

helpDialog.setVisible(true); }

try{

output = new ObjectOutputStream(socket.getOutputStream()); output.flush();

input = new ObjectInputStream(socket.getInputStream() ); output.writeObject(userName);

output.flush(); recvThread =

ClientReceive(socket,output,input,combobox,messageShow,showStatus);

}

recvThread.start();

loginButton.setEnabled(false); loginItem.setEnabled(false); userButton.setEnabled(false); userItem.setEnabled(false);

connectButton.setEnabled(false); connectItem.setEnabled(false); logoffButton.setEnabled(true); logoffItem.setEnabled(true);

clientMessage.setEnabled(true);

messageShow.append(\连接服务器 \成功...\\n\type = 1;//标志位设为已连接

new

第 32 页 共 18 页

课 程 设 计 报 告 书 专 用 纸

} }

public void SendMessage(){ String toSomebody = combobox.getSelectedItem().toString(); String status = \ if(checkbox.isSelected()){

}

status = \悄悄话\catch (Exception e){ }

System.out.println(e); return;

public void DisConnect(){

loginButton.setEnabled(true); loginItem.setEnabled(true); userButton.setEnabled(true); userItem.setEnabled(true);

connectButton.setEnabled(true); connectItem.setEnabled(true); logoffButton.setEnabled(false); logoffItem.setEnabled(false); clientMessage.setEnabled(false); if(socket.isClosed()){ }

try{ output.writeObject(\用户下线\

output.flush(); input.close(); output.close(); socket.close();

messageShow.append(\已经与服务器断开连接...\\n\type = 0;//标志位设为未连接 return ;

}

catch (Exception e){ // }

Icon face=new ImageIcon(\

String action = actionlist.getSelectedItem().toString(); String message = clientMessage.getText(); if(socket.isClosed()){

第 33 页 共 18 页

课 程 设 计 报 告 书 专 用 纸

}

}

public static void main(String[] args) { new ChatClient(); }

}

try{

output.writeObject(\聊天信息\ output.flush();

output.writeObject(toSomebody); output.flush();

output.writeObject(action); output.flush();

output.writeObject(action); output.flush();

output.writeObject(message); output.flush(); return ;

}

catch (Exception e){ // }

(2)ClientReceive.java:该类是实现服务器端与客户端消息收发的类。

import javax.swing.*;

import java.io.*; import java.net.*; /*

* 聊天客户端消息收发类

*/

public class ClientReceive extends Thread {

private JComboBox combobox; private JTextArea textarea; Socket socket;

ObjectOutputStream output; ObjectInputStream input; JTextField showStatus;

public ClientReceive(Socket socket,ObjectOutputStream output,

input,JComboBox

combobox,JTextArea

ObjectInputStream textarea,JTextField showStatus){

this.socket = socket;

this.output = output; this.input = input;

第 34 页 共 18 页


java课程设计报告-聊天室(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:高效课堂 - 语文教学的必由之路

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: