当前位置:首页>> >>


基于java的聊天系统的设计于实现(系统 论文).rar

收藏

资源目录
    文档预览:
    编号:20180913143531562    类型:共享资源    大小:549.01KB    格式:RAR    上传时间:2018-09-13
    尺寸:148x200像素    分辨率:72dpi   颜色:RGB    工具:   
    40
    金币
    关 键 词:
    基于 java 聊天 系统 设计 实现 论文
    资源描述:
    运行时,用户登录端:import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.net.*;import java.io.*;public class New extends JFrame {//登录窗口类JPanel contentPane;//*****************String server;//服务器名int serport;//端口private Socket socket;private BufferedReader in;//输入输出流private PrintWriter out;//***********//程序界面JPanel jPanel1 = new JPanel();JLabel jLabel1 = new JLabel();JLabel jLabel2 = new JLabel();JTextField jicq = new JTextField();JLabel jLabel3 = new JLabel();JPasswordField password = new JPasswordField();JPanel jPanel2 = new JPanel();JButton login = new JButton();JButton newuser = new JButton();JButton quit = new JButton();JLabel jLabel6 = new JLabel();JTextField servername = new JTextField();JLabel jLabel7 = new JLabel();JTextField serverport = new JTextField();public New() {enableEvents(AWTEvent.WINDOW_EVENT_MASK);try { jbInit();server=servername.getText().toString().trim();serport=Integer.parseInt(serverport.getText().trim());} catch(Exception e) { e.printStackTrace(); }}private void jbInit() throws Exception {contentPane = (JPanel) this.getContentPane();contentPane.setLayout(null);this.setResizable(false);this.setSize(new Dimension(344, 245));this.setTitle(“New JICQ“);//contentPane.add(text, null);jPanel1.setBounds(new Rectangle(2, 3, 348, 110));jPanel1.setLayout(null);jLabel1.setText(“请输入你的信息(本系统由江西师大软件工程 2 班王冲海开发)欢迎使用!“);jLabel1.setBounds(new Rectangle(5, 7, 403, 8));jLabel2.setText(“你的 QQ:“);jLabel2.setBounds(new Rectangle(7, 66, 58, 18));jicq.setBounds(new Rectangle(68, 65, 97, 22));jLabel3.setText(“你的密码:“);jLabel3.setBounds(new Rectangle(173, 66, 67, 18));password.setBounds(new Rectangle(237, 63, 94, 22));jPanel2.setBounds(new Rectangle(8, 154, 347, 151));jPanel2.setLayout(null);login.setText(“登录“);login.setBounds(new Rectangle(5, 27, 79, 29));login.addMouseListener(new java.awt.event.MouseAdapter() {public void mouseClicked(MouseEvent e) {login_mouseClicked(e);}});newuser.setText(“注册“);newuser.setBounds(new Rectangle(118, 28, 79, 29));newuser.addMouseListener(new java.awt.event.MouseAdapter() {public void mouseClicked(MouseEvent e) {newuser_mouseClicked(e);}});quit.setText(“退出“);quit.setBounds(new Rectangle(228, 26, 79, 29));quit.addMouseListener(new java.awt.event.MouseAdapter() {public void mouseClicked(MouseEvent e) {quit_mouseClicked(e);}});jLabel6.setText(“服务器“);jLabel6.setBounds(new Rectangle(20, 132, 41, 18));servername.setText(“Wchonghai“);servername.setBounds(new Rectangle(73, 135, 102, 22));jLabel7.setText(“端口“);jLabel7.setBounds(new Rectangle(191, 137, 41, 18));serverport.setText(“8080“);serverport.setBounds(new Rectangle(241, 131, 90, 30));contentPane.add(jPanel1, null);jPanel1.add(jLabel1, null);jPanel1.add(jLabel2, null);jPanel1.add(jicq, null);jPanel1.add(jLabel3, null);jPanel1.add(password, null);contentPane.add(jPanel2, null);jPanel2.add(login, null);jPanel2.add(quit, null);jPanel2.add(newuser, null);contentPane.add(jLabel6, null);contentPane.add(servername, null);contentPane.add(jLabel7, null);contentPane.add(serverport, null);}protected void processWindowEvent(WindowEvent e) {super.processWindowEvent(e);if (e.getID() == WindowEvent.WINDOW_CLOSING) {System.exit(0);}}public static void main(String[] args) {//主程序New f=new New();f.setVisible(true);}void login_mouseClicked(MouseEvent e) {//登录按扭try{Socket socket=new Socket(InetAddress.getByName(server),serport);//连接服务器BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);out.println(“login“);//告诉服务器我要登录out.println(jicq.getText());out.println(password.getPassword());String str=“ “;//do{str=in.readLine().trim();//从服务器读取消息//如果失败就告诉出错if(str.equals(“false“)) JOptionPane.showMessageDialog(this,“对不起,出错了:-(“,“ok“,JOptionPane.INFORMATION_MESSAGE);else{//如果成功就打开主程序this.dispose();int g=Integer.parseInt(jicq.getText());MainWin f2=new MainWin(g,server,serport);f2.setVisible(true);}//System.out.println(“\n“);//}while(!str.equals(“ok“));}catch(IOException e1){}}void newuser_mouseClicked(MouseEvent e) {//新建用户按纽this.dispose();JDialog d=new Register(server,serport);//打开新建窗口d.pack();d.setLocationRelativeTo(this);d.setSize(400,400);d.show();}void quit_mouseClicked(MouseEvent e) {//关闭按扭this.dispose();System.exit(0);}}用户注册界面:import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.net.*;import java.io.*;import javax.swing.border.*;public class Register extends JDialog {//新建用户类//以下创建程序界面JPanel panel1 = new JPanel();JLabel jLabel1 = new JLabel();JTextField nickname = new JTextField();JLabel jLabel2 = new JLabel();JLabel jLabel3 = new JLabel();JPasswordField password = new JPasswordField();JLabel jLabel4 = new JLabel();JTextField email = new JTextField();JLabel jLabel5 = new JLabel();JLabel jLabel6 = new JLabel();JTextPane info = new JTextPane();JButton jButton1 = new JButton();JButton jButton2 = new JButton();JLabel jLabel7 = new JLabel();JRadioButton boy = new JRadioButton();JRadioButton girl = new JRadioButton();JLabel jLabel8 = new JLabel();JComboBox place = new JComboBox();JComboBox headpic = new JComboBox();//***************************private String[] pics = new String[] {//头像“1.jpg“, “3.jpg“,“5.jpg“, “7.jpg“};String sername;//服务器名int serverport;//服务器端口public Register(String s,int port) {//构造函数sername=s;serverport=port;try {jbInit();//创建窗口pack();}catch(Exception ex) {ex.printStackTrace();}//this(null, ““, true);}void jbInit() throws Exception {//创建窗口panel1.setLayout(null);this.getContentPane().setLayout(null);panel1.setMaximumSize(new Dimension(200, 200));panel1.setMinimumSize(new Dimension(200, 100));panel1.setBounds(new Rectangle(-2, 0, 419, 452));this.setTitle(“register“);jLabel1.setText(“昵称“);jLabel1.setBounds(new Rectangle(9, 45, 41, 18));nickname.setBounds(new Rectangle(50, 44, 128, 22));jLabel2.setText(“请填写以下内容“);jLabel2.setBounds(new Rectangle(9, 9, 103, 18));jLabel3.setText(“密码“);jLabel3.setBounds(new Rectangle(200, 44, 41, 18));password.setBounds(new Rectangle(247, 42, 100, 22));jLabel4.setText(“电子邮件“);jLabel4.setBounds(new Rectangle(2, 102, 58, 18));email.setBounds(new Rectangle(55, 96, 124, 22));jLabel5.setText(“头像“);jLabel5.setBounds(new Rectangle(193, 96, 51, 18));//***************ComboBoxModel model = new HeadPicCombobox(pics);ListCellRenderer renderer = new HeadpicCellRenderer();jLabel6.setText(“个人资料“);jLabel6.setBounds(new Rectangle(6, 189, 87, 18));info.setBounds(new Rectangle(5, 208, 363, 103));jButton1.setText(“确定“);jButton1.setBounds(new Rectangle(147, 330, 79, 29));jButton1.addMouseListener(new java.awt.event.MouseAdapter() {public void mouseClicked(MouseEvent e) {jButton1_mouseClicked(e);}});jButton2.setText(“取消“);jButton2.setBounds(new Rectangle(260, 329, 79, 29));jLabel7.setText(“性别“);jLabel7.setBounds(new Rectangle(9, 156, 41, 18));boy.setText(“男“);boy.setBounds(new Rectangle(43, 152, 38, 26));girl.setText(“女“);girl.setBounds(new Rectangle(80, 152, 56, 26));jLabel8.setText(“来自“);jLabel8.setBounds(new Rectangle(147, 154, 41, 18));place.setToolTipText(““);place.addItem(“江西师大软件学院 “);place.addItem(“软件工程 2 班王冲海“);place.setBounds(new Rectangle(181, 153, 163, 22));headpic.setBounds(new Rectangle(249, 91, 71, 28));headpic.setModel(model);headpic.setRenderer(renderer);this.getContentPane().add(jButton1, null);this.getContentPane().add(panel1, null);panel1.add(jLabel2, null);panel1.add(jLabel1, null);panel1.add(nickname, null);panel1.add(jLabel3, null);panel1.add(password, null);panel1.add(jLabel4, null);panel1.add(email, null);panel1.add(jLabel5, null);panel1.add(info, null);panel1.add(jButton2, null);panel1.add(jLabel6, null);panel1.add(jLabel7, null);panel1.add(boy, null);panel1.add(jLabel8, null);panel1.add(girl, null);panel1.add(place, null);panel1.add(headpic, null);}void jButton1_mouseClicked(MouseEvent e) {try{ System.out.println(sername);System.out.println(serverport);Socket socket=new Socket(InetAddress.getByName(sername),serverport);//连接服务器BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);out.println(“new“);//发送新建用户请求out.println(nickname.getText().trim());//发送呢称等信息out.println(password.getPassword());out.println(email.getText().trim());out.println(info.getText().trim());out.println(place.getSelectedItem());out.println(headpic.getSelectedIndex());//head picindexint no;no=Integer.parseInt(in.readLine());System.out.print(no);String str=“ “;//do{str=in.readLine().trim();//从服务器读取信息//如果出错if(str.equals(“false“)) JOptionPane.showMessageDialog(this,“对不起,出错了:-(“,“ok“,JOptionPane.INFORMATION_MESSAGE);else{//如果成功就告诉用户其号码JOptionPane.showMessageDialog(this,“your javaicq#is“+no,“ok“,JOptionPane.INFORMATION_MESSAGE);this.dispose();//并打开主窗口MainWin f2=new MainWin(no,sername,serverport);f2.setVisible(true);}//System.out.println(“\n“);//}while(!str.equals(“ok“));// socket.close();}catch(IOException e1){}}}class HeadPicCombobox extends DefaultComboBoxModel {//头象列表类public HeadPicCombobox(String[] pics) {for(int i=0; i < pics.length; ++i) {addElement(new Object[] { new ImageIcon(pics[i]) } );}}public Icon getIcon(Object object) {Object[] array = (Object[])object;return (Icon)array[0];}}class HeadpicCellRenderer extends JLabel implements ListCellRenderer {private BorderlineBorder = BorderFactory.createLineBorder(Color.red, 2),emptyBorder = BorderFactory.createEmptyBorder(2,2,2,2);public HeadpicCellRenderer() {setOpaque(true);}public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) {HeadPicCombobox model =(HeadPicCombobox)list.getModel();setIcon(model.getIcon(value));if(isSelected) {setForeground(list.getSelectionForeground());setBackground(list.getSelectionBackground());}else {setForeground(list.getForeground());setBackground(list.getBackground());}if(cellHasFocus) setBorder(lineBorder);else setBorder(emptyBorder);return this;}}主界面运行端:import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.util.Vector;import java.net.*;import java.io.*;class FindFriend2 extends JFrame {//查找好友类JLabel jLabel1 = new JLabel();JButton find2 = new JButton();JButton jButton1 = new JButton();JButton jButton2 = new JButton();JButton jButton3 = new JButton();JList list2;///////////////////以下是好友的呢称,性别等信息Vector nickname=new Vector();Vector sex=new Vector();Vector place=new Vector();Vector jicq=new Vector();Vector ip=new Vector();Vector pic=new Vector();Vector status=new Vector();Vector emails=new Vector();Vector infos=new Vector();//以下临时保存好友的呢称,性别等信息Vector tmpjicq=new Vector();//jicqidVector tmpname=new Vector();//jicqnameVector tmpip=new Vector();//ipVector tmppic=new Vector();//pic infoVector tmpstatus=new Vector();//statusVector tmpemail=new Vector();Vector tmpinfo=new Vector();//以下创建网络相关变量Socket socket;BufferedReader in;PrintWriter out;int myid;String serverhost;int servport;DatagramPacket sendPacket;DatagramSocket sendSocket;int sendPort=5000;//////////////////JPopupMenu findmenu = new JPopupMenu();JMenuItem look = new JMenuItem();JMenuItem add = new JMenuItem();public FindFriend2(int whoami,String host,int port) {//查找好友类构造函数enableEvents(AWTEvent.WINDOW_EVENT_MASK);try {serverhost=host;servport=port;myid=whoami;jbInit();}catch(Exception e) {e.printStackTrace();}//以下与服务器连接try{socket=new Socket(InetAddress.getByName(serverhost),servport);in=new BufferedReader(new InputStreamReader(socket.getInputStream()));out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);sendSocket=new DatagramSocket();}catch(IOException e1){}}private void jbInit() throws Exception {//以下是程序界面jLabel1.setText(“下面是在线的朋友“);jLabel1.setBounds(new Rectangle(11, 11, 211, 18));this.getContentPane().setLayout(new FlowLayout());find2.setText(“查找“);find2.setBounds(new Rectangle(8, 289, 79, 29));find2.addMouseListener(new java.awt.event.MouseAdapter() {public void mouseClicked(MouseEvent e) {find2_mouseClicked(e);}});jButton1.setText(“next“);jButton1.setBounds(new Rectangle(110, 288, 79, 29));jButton2.setText(“up“);jButton2.setBounds(new Rectangle(211, 285, 79, 29));jButton3.setText(“cancel“);
    展开阅读全文
    1
      金牌文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
    0条评论

    还可以输入200字符

    暂无评论,赶快抢占沙发吧。

    关于本文
    本文标题:基于java的聊天系统的设计于实现(系统 论文).rar
    链接地址:http://www.gold-doc.com/p-116476.html
    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服客服 - 联系我们
    copyright@ 2014-2018 金牌文库网站版权所有
    经营许可证编号:浙ICP备15046084号-3
    收起
    展开