优质文档qq聊天字体免费word范文模板 10页.docx
《优质文档qq聊天字体免费word范文模板 10页.docx》由会员分享,可在线阅读,更多相关《优质文档qq聊天字体免费word范文模板 10页.docx(8页珍藏版)》请在冰豆网上搜索。
优质文档qq聊天字体免费word范文模板10页
本文部分内容来自网络整理,本司不为其真实性负责,如有异议或侵权请及时联系,本司将立即删除!
==本文为word格式,下载后可方便编辑和修改!
==
qq聊天字体免费
篇一:
java聊天窗口,设置不同颜色,字体,保存聊天记录QQ聊天
昆明理工大学信息工程与自动化学院学生实验报告
(201X—201X学年第1学期)
课程名称:
Java上机报告开课实验室:
信自楼444201X年11月03日
实验目的:
1、了解文件和流的概念,字节的输入输出,掌握文件的操作。
2、了解网络通信,URL访问网络,掌握TCPSocket通信和TCPSocket的工作方式。
实验原理:
1、采用TCPSocket通信是实现的点对点聊天,ChatServer类提供服务端的TCP的链接服务,ChatTCPSocketJFrame类实现客户端TCPSocket通信以及两者的图形界面。
2、Socket对象用于在服务端和客户端的TCP链接之间进行发送和接受的双向数据通信,从Socket对象中能够获得字节输入流和字节输出流。
实验操作:
1、对教材中的例题进行修改,将文本区中对方和自己的字符串以不同的字体和颜色区别显示。
2、将聊天记录存储到文件中aaa.text。
实验结果:
通过网上查资料,因为JtextArea不能对单个的字体改颜色,所以改用JtextPane作为聊天的文本区。
经过很多次的修改终于得到正确的结果,实验截图如下:
聊天界面:
代码:
服务器代码
import.*;
importjava.io.*;
publicclassChatTCPServer
{
publicChatTCPServer(intport,Stringname)throwsIOException//约定端口、网名{//本机IP地址和指定端口构成服务端的Socket
ServerSocketserver=newServerSocket(port);//ServerSocket提供TCP连接服务
Socketclient=server.accept();//等待接收客户端的连接申请
newChatTCPSocketJFrame(name,client);//服务端的聊天室,图形用户界面同客户端
server.close();
}
publicstaticvoidmain(Stringargs[])throwsIOException
{
newChatTCPServer(201X,"花仙子");//启动服务端,约定端口,指定网名
}
}
//客户端代码
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.text.AttributeSet;
importjavax.swing.text.BadLocationException;
importjavax.swing.text.Document;
importjavax.swing.text.SimpleAttributeSet;
importjavax.swing.text.StyleConstants;
importjava.io.*;
import.InetAddress;
import.Socket;
publicclassChatTCPSocketJFrameextendsJFrameimplementsActionListener
{
privateStringname;//网名
privateSocketsocket;//TCPSocket对象//privateJTextAreatext_receiver;
privateJTextPanetextPane;//显示对话内容的文本区
privateJTextFieldtext_sender;//输入发送内容的文本行privateJButtonbutton_send,button_leave,button_save;//发送和离线按钮
privatePrintWritercout;//字符输出流对象
publicChatTCPSocketJFrame(Stringname,Socketsocket)throwsIOException//提供图形用户界面
{
super("聊天室"+name+""+InetAddress.getLocalHost()+":
"+socket.getLocalPort());
this.setBounds(320,240,400,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.text_receiver=newJTextArea();
this.textPane=newJTextPane();
//this.text_receiver.setEditable(true);
this.textPane.setEditable(false);
this.getContentPane().add(newJScrollPane(this.textPane));
JPanelpanel=newJPanel();
this.getContentPane().add(panel,"South");
this.text_sender=newJTextField(10);
panel.add(this.text_sender);
button_send=newJButton("发送");
panel.add(button_send);
button_send.addActionListener(this);
button_leave=newJButton("离线");
panel.add(button_leave);
button_leave.addActionListener(this);
this.setVisible(true);
button_save=newJButton("保存聊天记录");
panel.add(button_save);
button_save.addActionListener(this);
this.setVisible(true);
this.name=name;
this.socket=socket;
this.cout=newPrintWriter(socket.getOutputStream(),true);//获得Socket对象的输出流,立即flush
this.cout.println(name);//发送自己网名给对方
BufferedReadercin=newBufferedReader(new
InputStreamReader(socket.getInputStream()));
//获得Socket对象的输入流,创建字符输入流
Stringaline=cin.readLine();//接收到对方网名setDocs("链接"+aline+"成功"+"",Color.black,true,15);//显示对方发来的内容aline=cin.readLine();
while(aline!
=null&&!
aline.equals("bye"))//从输入流接收对方发来的字符串
{
setDocs(aline+"",Color.blue,true,20);//显示对方发来的内容
aline=cin.readLine();
}
cin.close();
cout.close();
socket.close();//关闭Socket连接button_send.setEnabled(false);
button_leave.setEnabled(false);
}
publicvoidsetDocs(Stringstr,Colorcol,booleanbold,intfontSize){
SimpleAttributeSetattrSet=newSimpleAttributeSet();StyleConstants.setForeground(attrSet,col);
//颜色
if(bold==true){
StyleConstants.setBold(attrSet,true);
}//字体类型
StyleConstants.setFontSize(attrSet,fontSize);
//字体大小
insert(str,attrSet);
}
publicvoidinsert(Stringstr,AttributeSetattrSet)
{
Documentdoc=textPane.getDocument();
str="\n"+str;
try{
doc.insertString(doc.getLength(),str,attrSet);
}
catch(BadLocationExceptione){
System.out.println("BadLocationException:
"+e);
}
}
publicChatTCPSocketJFrame(Stringname,Stringhost,intport)throwsIOException//客户端
{
this(name,newSocket(host,port));//客户端向指定主机的端口发出TCP连接请求
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==button_send)//发送按钮
{
this.cout.println(name+"说:
"+text_sender.getText());//通过流发送给对方setDocs("我说:
"+text_sender.getText()+"",Color.red,false,25);
text_sender.setText("");
}
if(e.getSource()==button_leave)//离线
{
//textPane.("我离线\n");
setDocs("我离线\n"+"\r",Color.red,false,20);
this.cout.println(name+"离线\n"+"bye");//发送给对方离线约定button_send.setEnabled(false);
button_leave.setEnabled(false);
}
if(e.getSource()==button_save)
{
PrintWriteroutputStream=null;
try{
outputStream=newPrintWriter(newFileWriter("D:
/aaa.txt"));outputStream.println(textPane.getText());
outputStream.close();
}
catch(IOExceptione1){
e1.printStackTrace();
篇二:
Photoshop制作QQ聊天图片
Photos