package show5;
//客户端代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileSystemView;
import javax.swing.border.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.text.*;
import data.DataPack;
import about.*;
public class Client extends JFrame
{
//入口函数
public static void main(String args[])
{
new Client();
}
//定义菜单条
private JMenu menuBegin=new JMenu("选择服务器");
private JMenuItem LJItem=new JMenuItem("连接服务器");
private JMenuItem DKItem=new JMenuItem("断开连接");
private JMenuItem TCItem=new JMenuItem("退出");
private JMenu FileBegin=new JMenu("上传文件");
private JMenuItem SelectItem=new JMenuItem("选择文件");
JMenuBar menuBar=null;
private FileInputStream fis;
private FileOutputStream dos;
//FIXME
//Action事件的定义
public Action [] ac=null;
private File file;
String myip = null;
//左边的一块面板,用于放置用户在线信息情况
private JPanel leftPane=new JPanel();
//放置一张图片
String url1 = "/about/nameF.jpg";
private JLabel leftLab=new JLabel(new ImageIcon(Client.class.getResource(url1)));
//显示当前登录的用户名
String url2 ="/about/tou1.jpg";
private JLabel userLab=new JLabel("当前用户",new ImageIcon(Client.class.getResource(url2)),JLabel.LEFT);
//显示当前所有在线用户
private JLabel allUserLab=new JLabel("当前所有在线用户 0 ");
//用于计算当前有多少用户在线
private int allUserCount=0;
//定义显示当前聊天室的所有人员列表
private DefaultListModel dl=new DefaultListModel();
private DefaultComboBoxModel dm=new DefaultComboBoxModel();
private JList list=new JList(dl);
//定义用户的聊天信息区的面板
private JPanel messagePane=new JPanel();
//定义表情,文字输入框的面板
private JPanel ComponentPane=new JPanel();
//定义显示用户的聊天信息TextPane
private JTextPane txtPane=new JTextPane();
//定义发送信息用的组件
private JLabel []lab=new JLabel[2];
private String []labStr={"我 对","要说的话"};
//显示所有在线用户名单
private JComboBox allU=new JComboBox();
private DefaultComboBoxModel dcdmAllU=new DefaultComboBoxModel();
private JComboBox [] jcomb=new JComboBox[3];
private DefaultComboBoxModel dcdm1=new DefaultComboBoxModel();
private DefaultComboBoxModel dcdm2=new DefaultComboBoxModel();
private String [] str={"黑色","红色","橙色","蓝色","蓝绿色","绿色","红紫色","粉红色","黄色"};
private DefaultComboBoxModel dcdm3=new DefaultComboBoxModel();
private String[] biaoqing ={"(无)","笑着说","哭着说","微笑道","一把鼻涕一把泪地痛诉道","嚎嚎大哭道",
"打XX了一拳,喝一声道","踢了XX一脚,大吼","吻了XX一下,深情地说","紧张地说",
"不屑一顾地说","低声下气地说","冷嘲热讽XX道","面无表情地说"};
private JCheckBox [] jcheckb=new JCheckBox[3];
private String [] jcheckbStr={"","头像选择","私聊"};
//输入要发送的话语的文本框
private JTextField jtf=new JTextField();
//发言,离开按钮
private JButton sendBtn=new JButton("发言");
private JButton exitBtn=new JButton("离开");
private String load1 =" 欢迎来到Dollars ";
private String load2 =" ------------ 在DOLLARS里,没有任何规则。------------ ";
private String load3 =" ------------ 一切都是源自你的内心 ------------ ";
private String load4=" --------------------- ";
//网络
private DataPack dp=null;
private Socket sc=null;
private ObjectInputStream ois=null;
private ObjectOutputStream oos=null;
//线程
private RecTh t=null;
//定义进入聊天室的昵称,服务器的地址,端口号
private String name="";
private String serverIP="";
private int serverPort=0;
//属性设置
public static SimpleAttributeSet sa1=new SimpleAttributeSet();
//光标样式
Cursor defaultCur=new Cursor(DEFAULT_CURSOR);
Cursor handCur=new Cursor(HAND_CURSOR);
//FIXME
public static int transFile(InputStream in, OutputStream out, int fileSize) {
int receiveLen = 0;
final int bufSize = 1000;
try {
byte[] buf = new byte[bufSize];
int len = 0;
while(fileSize - receiveLen > bufSize)
{
len = in.read(buf);
out.write(buf, 0, len);
out.flush();
receiveLen += len;
System.out.println(len);
}
while(receiveLen < fileSize)
{
len = in.read(buf, 0, fileSize - receiveLen);
System.out.println(len);
out.write(buf, 0, len);
receiveLen += len;
out.flush();
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return receiveLen;
}
//构造器
public Client()
{
//窗体属性
setSize(1024,740);
setTitle("欢迎来到本聊天室!");
//构件菜单
createMenu();
//设置LIST的CellRenderer
list.setCellRenderer(new hh());
//本窗体绑定退出事件
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
closeSocket();
System.exit(0);
}
});
//得到JTextPane的事件
ac=txtPane.getActions();
for(int i=0;i<ac.length;i++)
{
String tmp=(String)(ac[i].getValue(Action.NAME));
}
//启动是加载的字符串
txtPane.replaceSelection(load4+"\n");
txtPane.replaceSelection(load3+"\n");
txtPane.replaceSelection(load2+"\n");
txtPane.replaceSelection(load1+"\n");
//设置文本编辑区的属性
txtPane.setEditable(false);
txtPane.setFont(new Font("宋体",0,15));
txtPane.setSelectionColor(new Color(153,153,255));
txtPane.setBackground(new Color(132,145,174,50).brighter());
//标签初始化
for(int i=0;i<lab.length;i++)
{
lab[i]=new JLabel(labStr[i]);
ComponentPane.add(lab[i]);
}
//加载图片
for(int i=0;i<6;i++)
{
dcdm1.addElement(new ImageIcon(Client.class.getResource("/look2/"+i+".jpg")));
}
//加载颜色字符
for(int i=0;i<str.length;i++)
{
dcdm2.addElement(str[i]);
}
//加载动作字符
for(int i=0;i<biaoqing.length;i++)
{
dcdm3.addElement(biaoqing[i]);
}
//JComboBox的初始化
for(int i=0;i<jcomb.length;i++)
{
jcomb[i]=new JComboBox();
ComponentPane.add(jcomb[i]);
}
jcomb[1].setModel(dcdm1);
jcomb[2].setModel(dcdm2);
jcomb[0].setModel(dcdm3);
allU.setModel(dcdmAllU);
//JCheckBox的初始化
for(int i=0;i<3;i++)
{
jcheckb[i]=new JCheckBox(jcheckbStr[i]);
jcheckb[i].setForeground(Color.blue);
jcheckb[i].setBackground(new Color(221,117,162).brighter());
ComponentPane.add(jcheckb[i]);
}
//设置各组件的属性
lab[0].setForeground(Color.blue);
lab[1].setForeground(Color.blue);
sendBtn.setMargin(new Insets(0,0,0,0));
exitBtn.setMargin(new Insets(0,0,0,0));
userLab.setForeground(Color.red);
userLab.setFont(new Font("宋体",0,13));
allUserLab.setOpaque(true);
allUserLab.setForeground(Color.white);
allUserLab.setBackground(Color.red);
list.setFont(new Font("宋体",0,13));
list.setForeground(new Color(255,102,153));
list.setBackground(new Color(240,240,240));
list.setSelectedIndex(0);
//设置各组件的位置
lab[0].setBounds(15,5,80,25);
allU.setBounds(75,7,100,20);
jcomb[1].setBounds(355,7,80,20);
lab[1].setBounds(15,45,50,25);
jtf.setBounds(80,45,450,23);
//jcheckb[0].setBounds(530,45,50,25);
jcheckb[2].setBounds(530,45,55,25);
jcheckb[1].setBounds(270,10,140,25);
sendBtn.setBounds(590,45,70,23);exitBtn.setBounds(700,45,70,23);
ComponentPane.setPreferredSize(new Dimension(1024,100));
ComponentPane.setBackground(new Color(221,117,162).brighter());
ComponentPane.setLayout(null);
ComponentPane.add(jtf);ComponentPane.add(sendBtn);
ComponentPane.add(exitBtn);
ComponentPane.add(allU);
//聊天信息区的面板添加聊天信息TextPane和表情,文字输入框的面
评论0
最新资源