THE THIRTY-EIGHTH DAY

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aizaiee/article/details/79955818

    今天星期天,参加培训的第三十七天,今天,放假起的晚了一些,然后洗衣服,中午停水了,很烦,行,开始贴代码吧。

    

registerview:

package view;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.MouseInfo;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.SynchronousQueue;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.Timer;
import util.ImagePanelUtil;

public class RegisterView extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static int x;
	private static int y;
	private String code;
	private int time;
	private static TrayIcon trayIcon = null;
	static SystemTray tray = SystemTray.getSystemTray();
	private Socket socket;
	private DataOutputStream toServer;
	private DataInputStream fromServer;
	ActionListener messageActionListener;

	public RegisterView() {
		// TODO Auto-generated constructor stub
		try {
			socket = new Socket("localhost", 8000);
			fromServer = new DataInputStream(socket.getInputStream());
			toServer = new DataOutputStream(socket.getOutputStream());
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		init(this);
		setSize(400, 300);
		setLocationRelativeTo(null);
		// frame.setTitle("qq注册");
		setType(JFrame.Type.UTILITY);//取消任务栏
		setUndecorated(true);//取消标题栏
		setVisible(true);
		ImageIcon trayImg = new ImageIcon("image/他的王.jpg");// 托盘图标
		trayIcon = new TrayIcon(trayImg.getImage(), "QQ注册", new PopupMenu());
		trayIcon.setImageAutoSize(true);
		try {
			tray.add(trayIcon);//增加托盘图标
		} catch (AWTException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		trayIcon.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {

				if (e.getClickCount() == 1) {// 单击 1 双击 2
					setVisible(true);
					setExtendedState(JFrame.NORMAL);//单击图标,窗口还原
					toFront();
				}
			}
		});
		addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				setLocation((int) (MouseInfo.getPointerInfo().getLocation().getX() - x),
						(int) (MouseInfo.getPointerInfo().getLocation().getY() - y));
				//获得鼠标基于window窗口的坐标减去在窗口上的坐标
			}

		});
		addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				x = e.getX();//获取鼠标在程序上的坐标
				y = e.getY();
			}
		});
	}

	private void init(RegisterView registerView) {
		setLayout(new BorderLayout());
		JPanel centerPanel = new JPanel();
		JLabel usernameLabel = new JLabel("请输入你昵称:");
		JLabel passwordLabel = new JLabel("请输入密码:");
		JLabel phoneNumberLabel = new JLabel("请输入手机号:");
		JTextField messageText = new JTextField(5);
		JTextField usernameText = new JTextField(10);
		JPasswordField passwordText = new JPasswordField(10);
		JTextField phoneNumberText = new JTextField(10);
		JButton jbtMessage = new JButton("发送验证码");
		jbtMessage.setBackground(Color.blue);
		jbtMessage.setForeground(Color.white);
		jbtMessage.setFont(new Font("宋体", Font.PLAIN, 12));
		centerPanel.setLayout(new GridLayout(0, 4));
		centerPanel.add(new JLabel());
		centerPanel.add(usernameLabel);
		centerPanel.add(usernameText);
		centerPanel.add(new JLabel());
		centerPanel.add(new JLabel());
		centerPanel.add(passwordLabel);
		centerPanel.add(passwordText);
		centerPanel.add(new JLabel());
		centerPanel.add(new JLabel());
		centerPanel.add(phoneNumberLabel);
		centerPanel.add(phoneNumberText);
		centerPanel.add(new JLabel());
		centerPanel.add(new JLabel());
		centerPanel.add(messageText);
		centerPanel.add(jbtMessage);
		JPanel newCenterPanel = new JPanel(new BorderLayout());
		newCenterPanel.add(centerPanel, BorderLayout.CENTER);
		newCenterPanel.add(new JPanel(new GridLayout(0, 1)), BorderLayout.WEST);
		newCenterPanel.add(new JPanel(new GridLayout(0, 2)), BorderLayout.EAST);
		JPanel downPanel = new JPanel();
		JButton jbtSure = new JButton("QQ注册");
		jbtSure.setBackground(Color.blue);
		jbtSure.setForeground(Color.WHITE);
		jbtSure.setPreferredSize(new Dimension(200, 30));
		downPanel.add(jbtSure, BorderLayout.CENTER);
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(newCenterPanel, BorderLayout.NORTH);
		mainPanel.add(downPanel, BorderLayout.CENTER);
		JLabel tipLabel = new JLabel();
		mainPanel.add(tipLabel, BorderLayout.SOUTH);
		tipLabel.setVisible(false);
		JPanel upPanel = new ImagePanelUtil(400, 150, "./image/他的王.jpg");
		// System.out.println(getClass().getClassLoader().getResource(""));
		// Image image =
		// ImageIO.read(getClass().getResourceAsStream(".././image/他的王.jpg"));
		// upPanel.getGraphics().drawImage(image, 1, 1, 2, 2, 1, 1, 2, 2,null);
		// System.out.println(upPanel.getName());
		// upPanel.getGraphics().fillOval(160, 80, 80, 80);
		upPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		JButton jbtClose = new JButton("X");
		Font font = new Font("宋体", Font.BOLD, 20);
		// jbtClose.setContentAreaFilled(false); 设置透明
		jbtClose.setBorderPainted(false);
		// jbtClose.setOpaque(false);
		jbtClose.setFont(font);
		jbtClose.setBackground(Color.WHITE);
		jbtClose.setForeground(Color.black);
		// ImageIcon imageClose = new ImageIcon("image/关闭.jpg");
		// jbtClose.setIcon(imageClose);
		// jbtClose.setBorderPainted(false);
		JButton jbtMin = new JButton("-");
		jbtMin.setBackground(Color.WHITE);
		jbtMin.setBorderPainted(false);
		jbtMin.setFont(font);
		upPanel.add(jbtMin);
		upPanel.add(jbtClose);
		Container container = this.getContentPane();
		container.setLayout(new GridLayout(2, 0));
		container.add(upPanel);
		container.add(mainPanel);
		// centerPanel.setForeground(Color.BLUE);
		// centerPanel.setBackground(Color.BLUE);
		// downPanel.setForeground(Color.BLUE);
		// downPanel.setBackground(Color.BLUE);

		Timer timer = new Timer(0, new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jbtMessage.removeActionListener(messageActionListener);
				jbtMessage.setText((time--) + "秒后请求");
				repaint();
				if (time < 1) {
					jbtMessage.setText("发送验证码");
					jbtMessage.setBackground(Color.blue);
					repaint();
					jbtMessage.addActionListener(messageActionListener);
				}
			}
		});
		jbtMessage.addActionListener(messageActionListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String phoneNumber = phoneNumberText.getText();
				if (!phoneNumber.matches("^1\\d{10}$")) {
					tipLabel.setText("请输入正确手机号");
					tipLabel.setVisible(true);
					return;
				} else {
					tipLabel.setText("");
					tipLabel.setVisible(false);
				}
				jbtMessage.setBackground(Color.gray);
				code = getSmsDemo(phoneNumber);
				time = 59;
				timer.setDelay(1000);
				timer.setInitialDelay(0);
				timer.start();
			}
		});
		jbtClose.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
				try {
					toServer.close();
					fromServer.close();
					socket.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

		jbtClose.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				jbtClose.setBackground(Color.RED);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				jbtClose.setBackground(Color.WHITE);

			}
		});
		jbtMin.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setExtendedState(JFrame.ICONIFIED);
				// setExtendedState(JFrame.MAXIMIZED_BOTH);//最大化窗体
			}
		});
		jbtSure.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jbtMessage.addActionListener(messageActionListener);
				String username = usernameText.getText();
				tipLabel.setText("");
				tipLabel.setVisible(false);
				if (!username.matches("^.{1,8}$")) {
					tipLabel.setText("昵称为1-8个字符");
					tipLabel.setVisible(true);
					return;
				}
				String password = String.valueOf(passwordText.getPassword());
				if (!password.matches("^.{8,16}$")) {
					tipLabel.setText("密码为8-16个字符");
					tipLabel.setVisible(true);
					return;
				}
				String phoneNumber = phoneNumberText.getText();
				if (!phoneNumber.matches("^1\\d{10}$")) {
					tipLabel.setText("请输入正确手机号");
					tipLabel.setVisible(true);
					return;
				}
				System.out.println(code);
				if ((code == null) || !code.equals(messageText.getText())) {
					tipLabel.setText("验证码不正确");
					tipLabel.setVisible(true);
					return;
				}
				if (isRegister(username, password, phoneNumber)) {
					registerView.dispose();
					tray.remove(trayIcon);
					try {
						toServer.close();
						fromServer.close();
						socket.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					new LoginView().setVisible(true);
					// new LoginView(socket).setVisible(true);
				}
			}
		});
	}

	public boolean isRegister(String username, String password, String phoneNumber) {
		String user = (new StringBuffer("account")).append(",").append(username).append(",").append(password)
				.append(",").append(phoneNumber).toString();
		boolean isRegister = false;
		System.out.println(user);
		try {
			toServer.writeUTF(user);
			toServer.flush();
			isRegister = fromServer.readBoolean();
			user = fromServer.readUTF();
			JOptionPane.showMessageDialog(null, user);
		} catch (UnknownHostException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		return isRegister;
	}

	public String getSmsDemo(String phoneNumber) {
		String code = "code" + phoneNumber;
		try {
			toServer.writeUTF(code);
			toServer.flush();
			code = fromServer.readUTF();
		} catch (UnknownHostException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		return code;
		// socket = new Socket("192.168.0.103", 8000);
	}

	public static void main(String[] args) {
		JFrame frame = new RegisterView();

	}
}

loginview:

package view;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.MouseInfo;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import util.ImagePanelUtil;

public class LoginView extends JFrame {
	private static int x;
	private static int y;
    private static TrayIcon trayIcon = null;   
	private static SystemTray tray = SystemTray.getSystemTray();
	private DataOutputStream toServer;
	private DataInputStream fromServer;
	Socket socket;
    public LoginView() {//Socket socket
		// TODO Auto-generated constructor stub
//    	this.socket=socket;
    	try {
//    		socket = new Socket("localhost",8000);
    		socket = new Socket("192.168.0.104",8000);
    	    fromServer = new DataInputStream(socket.getInputStream());
			toServer = new DataOutputStream(socket.getOutputStream());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setSize(400, 300);
		setLocationRelativeTo(null);
//		frame.setTitle("qq注册");
		setUndecorated(true);
		setType(JFrame.Type.UTILITY);
		addMoveWindow();
		Container container = this.getContentPane();
	    container.setLayout(new GridLayout(2, 0));
		container.add(upPanel());
	    container.add(mainPanel(this));
		setVisible(true);
    }
    
    public JPanel upPanel() {
        JPanel upPanel = new ImagePanelUtil(400,150,"./image/他的妃.jpg");
        upPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JButton jbtClose = new JButton("X");
        Font font = new Font("宋体",Font.BOLD,20);
//        jbtClose.setContentAreaFilled(false); 设置透明
        jbtClose.setBorderPainted(false);
//        jbtClose.setOpaque(false);
        jbtClose.setFont(font);
        jbtClose.setBackground(Color.WHITE);
        jbtClose.setForeground(Color.black);
        JButton jbtMin = new JButton("-");
        jbtMin.setBackground(Color.WHITE);
        jbtMin.setBorderPainted(false);
        jbtMin.setFont(font);
        upPanel.add(jbtMin);
        upPanel.add(jbtClose);
        jbtClose.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
	    
        jbtClose.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mouseEntered(MouseEvent e) {
        		// TODO Auto-generated method stub
        		jbtClose.setBackground(Color.RED);
        	}
        	@Override
        	public void mouseExited(MouseEvent e) {
        		// TODO Auto-generated method stub
        		jbtClose.setBackground(Color.WHITE);
        		
        	}
		});
        jbtMin.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setExtendedState(JFrame.ICONIFIED);
//				setExtendedState(JFrame.MAXIMIZED_BOTH);//最大化窗体 
			}
		});
        return upPanel;
    }
    
    public JPanel mainPanel(LoginView loginView) {
    	JPanel centerPanel = new JPanel();
	    JLabel accountLabel = new JLabel("请输入账号:");
	    JLabel passwordLabel = new JLabel("请输入密码:");
	    JTextField accountText = new JTextField(10);
	    JPasswordField passwordText = new JPasswordField(10);
	    centerPanel.setLayout(new GridLayout(3, 4,0,20));
	    centerPanel.add(new JLabel());
	    centerPanel.add(accountLabel);
	    centerPanel.add(accountText);
	    centerPanel.add(new JLabel());
	    centerPanel.add(new JLabel());
	    centerPanel.add(passwordLabel);
	    centerPanel.add(passwordText);
	    centerPanel.add(new JLabel());
	    centerPanel.add(new JLabel());
	    centerPanel.add(new JLabel());
	    JPanel downPanel = new JPanel();
	    JButton jbtSure = new JButton("QQ登录");
	    jbtSure.setBackground(Color.blue);
	    jbtSure.setForeground(Color.WHITE);
	    jbtSure.setPreferredSize(new Dimension(200, 30));
	    downPanel.add(jbtSure,BorderLayout.CENTER);
	    JPanel mainPanel = new JPanel();
	    mainPanel.setLayout(new BorderLayout());
	    mainPanel.add(centerPanel, BorderLayout.NORTH);
	    mainPanel.add(downPanel, BorderLayout.CENTER);
	    JLabel tipLabel = new JLabel();
	    mainPanel.add(tipLabel,BorderLayout.SOUTH);
	    tipLabel.setVisible(false);
	    accountText.addKeyListener(new KeyAdapter() {
	    	@Override
	    	public void keyPressed(KeyEvent e) {
	    		// TODO Auto-generated method stub
	    		if(e.getKeyCode()==e.VK_ENTER) {
	    			passwordText.grabFocus();
//	    			passwordText.requestFocusInWindow();
	    		}
	    	}
		});
	    passwordText.addKeyListener(new KeyAdapter() {
	    	@Override
	    	public void keyPressed(KeyEvent e) {
	    		// TODO Auto-generated method stub
	    		if(e.getKeyCode()==e.VK_ENTER) {
	    			jbtSure.doClick();
	    		}
	    	}
		});
        jbtSure.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String account = accountText.getText();
				String password = String.valueOf(passwordText.getPassword());
				tipLabel.setText("");
				tipLabel.setVisible(false);						
//				if(!userDao.isAccountExists(account, password)) {
				if(!isAccountExists(account,password)) {
					tipLabel.setText("账号密码不匹配");
					tipLabel.setVisible(true);	
					return;
				}
				JOptionPane.showMessageDialog(null, "登录成功");
                loginView.dispose(); 
                tray.remove(trayIcon); 
                try {
                    toServer.close();
					fromServer.close();
					socket.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
                new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						
						JFrame frame = new MainView(account);
					}
				}).start();
			}
		});
        
        
	    return mainPanel;
    }
    public boolean isAccountExists(String account,String password) {
    	try {
			String usernameAndPassword =new StringBuffer("password").append(",").append(account)
					                       .append(",").append(password).toString();
			
			System.out.println(usernameAndPassword);
			toServer.writeUTF(usernameAndPassword);
			toServer.flush();
			boolean isAccountExists=fromServer.readBoolean();
			return isAccountExists;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    	return false;
    }
    public void addMoveWindow() {
    	ImageIcon trayImg = new ImageIcon("image/他的妃.jpg");// 托盘图标          
        trayIcon = new TrayIcon(trayImg.getImage(), "QQ登录", new PopupMenu());  
        trayIcon.setImageAutoSize(true);
        try {
			tray.add(trayIcon);
		} catch (AWTException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        trayIcon.addMouseListener(new MouseAdapter() {         	  
            public void mouseClicked(MouseEvent e) {  
  
                if (e.getClickCount() == 1) {// 单击 1 双击 2  
                    setVisible(true);  
                    setExtendedState(JFrame.NORMAL);  
                    toFront();  
                }  
            }    
        }); 
    	addMouseMotionListener(new MouseMotionAdapter() {
    		@Override
    		public void mouseDragged(MouseEvent e) {
    			// TODO Auto-generated method stub
    			setLocation((int)(MouseInfo.getPointerInfo().getLocation().getX()-x),
    					(int)(MouseInfo.getPointerInfo().getLocation().getY()-y));
    		}
    		
    		
    		});
    		addMouseListener(new MouseAdapter() {
    			@Override
    			public void mousePressed(MouseEvent e) {
    				// TODO Auto-generated method stub
    				x=e.getX();
    				y=e.getY();
    			}
    		});
    }
    public static void main(String[] args) {
//		try {
//			JFrame frame = new LoginView(new Socket("localhost", 8000));
			JFrame frame = new LoginView();
//		} catch (UnknownHostException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
}

mainview:

package view;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import entity.User;
import util.ImagePanelUtil;
import util.MessagePanelUtil;
import util.MyListCellRendererUtil;

public class MainView extends JFrame {
	private static int x;
	private static int y;
	private static TrayIcon trayIcon = null;
	private static SystemTray tray = SystemTray.getSystemTray();
	private String account;
	private JList jlst;
	private ObjectInputStream objectFromServer;
	private ObjectOutputStream objectToServer;
//	private ObjectInputStream objectFromServerChat;
//	private ObjectOutputStream objectToServerChat;
//	private DataOutputStream toServer;
//	private DataInputStream fromServer;
	private Socket socket;
//	private Socket chatSocket;
	private Thread thread;
	public MainView(String account) {
		// TODO Auto-generated constructor stub
		// setSize(350, 700);
		try {
			socket = new Socket("192.168.0.104",7000);
//			socket = new Socket("localhost",7000);
//			chatSocket = new Socket("localhost",6000);
//			toServer = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
//			fromServer = new DataInputStream(socket.getInputStream());
//		    socket = new Socket("localhost",7000);
			objectToServer = new ObjectOutputStream(socket.getOutputStream());
			objectFromServer = new ObjectInputStream(socket.getInputStream());
//			objectToServerChat = new ObjectOutputStream(chatSocket.getOutputStream());
//			objectFromServerChat = new ObjectInputStream(chatSocket.getInputStream());
			
			objectToServer.writeUTF(account);
			objectToServer.flush();
//			toServer = new DataOutputStream(socket.getOutputStream());
//			toServer.writeUTF("aaaa");
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.account = account;
		setBounds(1000, 30, 300, 700);
		// frame.setTitle("qq注册");
		setUndecorated(true);// 不显示标题栏
		setType(JFrame.Type.UTILITY);// 不在任务栏显示
		addMoveWindow();
		Container container = this.getContentPane();
		container.add(upPanel());
		container.setLayout(new FlowLayout());
		container.add(downPanel(this));
		container.add(addFriend(this));
		// container.setLayout(new GridLayout(0, 1));
		setVisible(true);
	   thread= new Thread(new chatThread(this));
	   thread.start();
	}
    
	/**
	 * 选择线程,判断是执行聊天框还是添加好友
	 * @author renre
	 *
	 */
	class chatThread implements Runnable{
         private MainView mainView;
         public chatThread(MainView mainView) {
			// TODO Auto-generated constructor stub
        	 this.mainView=mainView;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			
				while(true) {
					try {
//				String str = objectFromServer.readUTF();
				String option = objectFromServer.readUTF();
				System.out.println(option);
				if(option.equals("main")) {					
					String friendAccount = objectFromServer.readUTF();
					String text = objectFromServer.readUTF();
//				Object account=(Object[]) jlst.getModel().getElementAt(1);
					new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							
							JFrame frame = new ChatView(mainView,friendAccount,text);
						}
					}).start();
				} else {
					System.out.println("-------------");
					jlst.setModel(listModel(2));
					jlst.invalidate();
					repaint();  
				}
					}catch (EOFException e) {
						System.err.println("eofemain");
	
					}catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						break;
					
					}
				}
			} 
		}
		
	
	public User getUser(String account) {
		
		User user=null;
		try {
			String option ="getUser";
			objectToServer.writeUTF(option);
			objectToServer.flush();
			objectToServer.writeUTF(account);
			objectToServer.flush();
			Object object = objectFromServer.readObject();
//			System.out.println(object.toString());
			if(object instanceof User) {				
			    System.out.println("类型匹配");
				user = (User)object;			
			}else {
				System.out.println("类型不匹配");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
		}
		return user;
	}


	public JPanel upPanel() {
		User user = getUser(account);
		JPanel upPanel = new ImagePanelUtil(300, 150, "./image/他的妃.jpg");
		upPanel.setLayout(new GridLayout(1, 2));
		JPanel leftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton jbtClose = new JButton("X");
		Font font = new Font("宋体", Font.BOLD, 20);
		// jbtClose.setContentAreaFilled(false); 设置透明
		jbtClose.setBorderPainted(false);
		// jbtClose.setOpaque(false);
		jbtClose.setFont(font);
		jbtClose.setBackground(Color.WHITE);
		jbtClose.setForeground(Color.black);
		JButton jbtMin = new JButton("-");
		jbtMin.setBackground(Color.WHITE);
		jbtMin.setBorderPainted(false);
		jbtMin.setFont(font);
		ImageIcon image = new ImageIcon(user.getPhoto());
		image.setImage(image.getImage().getScaledInstance(60, 60, Image.SCALE_DEFAULT));
		JLabel photoLabel = new JLabel(image, JLabel.CENTER);
		JPanel messagePanel = new MessagePanelUtil(user.getUsername());
		messagePanel.setOpaque(false);
		messagePanel.setPreferredSize(new Dimension(40, 40));
		rightPanel.add(jbtMin);
		rightPanel.add(jbtClose);
		rightPanel.setOpaque(false);
		leftPanel.add(photoLabel);
		leftPanel.add(messagePanel);
		leftPanel.setOpaque(false);
		upPanel.add(leftPanel);
		upPanel.add(rightPanel);
		upPanel.setPreferredSize(new Dimension(300, 150));
		jbtClose.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					objectToServer.close();
//					objectToServer.close();
					objectFromServer.close();
//					objectToServerChat.close();
//					objectFromServerChat.close();
//					fromServer.close();
					socket.close();
//					chatSocket.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				System.exit(0);
			}
		});

		jbtClose.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				jbtClose.setBackground(Color.RED);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				jbtClose.setBackground(Color.WHITE);

			}
		});
		jbtMin.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setExtendedState(JFrame.ICONIFIED);
				// setExtendedState(JFrame.MAXIMIZED_BOTH);//最大化窗体
			}
		});
		return upPanel;
	}

	public List<User> getUserList(String account) {
		List<User> userList = new ArrayList<>();
		try {
			objectToServer.writeUTF("getUserList");
			objectToServer.flush();
			objectToServer.writeUTF(account);
			objectToServer.flush();
			String str = objectFromServer.readUTF();
			JOptionPane.showMessageDialog(null, str);
			System.out.println("-------------");
			userList = (List<User>) objectFromServer.readObject();
			System.out.println("--------------"+userList.size());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
		}
		return userList;
	}
	
	public List<User> getUserList1(String account) {
		List<User> userList = new ArrayList<>();
		try {
					
			userList = (List<User>) objectFromServer.readObject();
//			JOptionPane.showMessageDialog(null, userList.size());
			System.out.println("--------------"+userList.size());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
		}
		return userList;
	}

	public DefaultListModel listModel(int j) {
		List<User> userList;
		if(j==1) {			
		  userList = getUserList(account);
		}else {
		  userList = getUserList1(account);
		}
		int friendNumber = userList.size();
		ImageIcon[] images = new ImageIcon[friendNumber];
		String[] names = new String[friendNumber];
		String[] accounts = new String[friendNumber];
		for (int i = 0; i < friendNumber; i++) {
			images[i] = new ImageIcon(userList.get(i).getPhoto());
			images[i].setImage(images[i].getImage().getScaledInstance(50, 50, Image.SCALE_AREA_AVERAGING));
			names[i] = userList.get(i).getUsername();
			accounts[i] = userList.get(i).getAccount();
		}
		DefaultListModel listModel = new DefaultListModel();
		for (int i = 0; i < friendNumber; i++) {
			listModel.addElement(new Object[] { images[i], names[i] + "(" + accounts[i] + ")",accounts[i]});
		}
		return listModel;
	}

	public JScrollPane downPanel(MainView mainView) {
		// ImageIcon image = new ImageIcon("image/男.png");
		// image.setImage(image.getImage().getScaledInstance(50, 50,
		// Image.SCALE_AREA_AVERAGING));
		// String name ="";

		jlst = new JList(listModel(1));
		jlst.setBorder(BorderFactory.createLineBorder(Color.blue, 0));
		ListCellRenderer renderer = new MyListCellRendererUtil();
		JLabel labelImage = new JLabel("", JLabel.CENTER);
		jlst.setCellRenderer(renderer);
		// jlst.setPreferredSize(new Dimension(300, 700));
		JScrollPane jsPane = new JScrollPane(jlst);
		jsPane.setPreferredSize(new Dimension(300, 500));
		jsPane.invalidate();
		jlst.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				if (e.getClickCount() == 2) {
					new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							
							JFrame frame = new ChatView(mainView,jlst.getSelectedValue());
						}
					}).start();
				}
			}
		});
		return jsPane;
	}

	public void shuffList() {
		try {
		objectToServer.writeUTF("getUserList");
		objectToServer.flush();
		objectToServer.writeUTF(account);
			objectToServer.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

	public JPanel addFriend(MainView mainView) {
	
		JPanel jPanel = new JPanel();
		JButton jbtAddFriend = new JButton("添加好友");
		jPanel.add(jbtAddFriend);
		jbtAddFriend.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						
						JFrame frame = new AddFriendView(mainView);
					}
				}).start();
			}
		});
		return jPanel;
	}

	public static void main(String[] args) {
		JFrame frame = new MainView("10000020");
	}

	public void addMoveWindow() {
		ImageIcon trayImg = new ImageIcon("image/他的妃.jpg");// 托盘图标
		trayIcon = new TrayIcon(trayImg.getImage(), "test", new PopupMenu());
		trayIcon.setImageAutoSize(true);
		try {
			tray.add(trayIcon);// 显示托盘图标
		} catch (AWTException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		trayIcon.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {

				if (e.getClickCount() == 1) {// 单击 1 双击 2
					setVisible(true);
					setExtendedState(JFrame.NORMAL);
					toFront(); // 此窗口可见
				}
			}
		});
		addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				setLocation((int) (MouseInfo.getPointerInfo().getLocation().getX() - x),
						(int) (MouseInfo.getPointerInfo().getLocation().getY() - y));
			}

		});
		addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				x = e.getX();
				y = e.getY();
			}
		});
		addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				JOptionPane.showMessageDialog(null, "主界面关闭");
				try {
				objectToServer.close();
				objectFromServer.close();
					socket.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}

	public ObjectInputStream getObjectFromServer() {
		return objectFromServer;
	}
	
	public void setObjectFromServer(ObjectInputStream objectFromServer) {
		this.objectFromServer = objectFromServer;
	}
	
	public ObjectOutputStream getObjectToServer() {
		return objectToServer;
	}
	
	public void setObjectToServer(ObjectOutputStream objectToServer) {
		this.objectToServer = objectToServer;
	}
}

addFriendview:

package view;

import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import entity.User;

public class AddFriendView extends JFrame {
	private String account;
	private Socket socket;
	private ObjectOutputStream objectToServer;
	private ObjectInputStream objectFromServer;
//	private DataOutputStream toServer;
//	private DataInputStream fromServer;

	public void loadStream(MainView mainView) throws IOException {

//			toServer = new DataOutputStream(socket.getOutputStream());
		objectToServer = new ObjectOutputStream(socket.getOutputStream());
		objectFromServer = new ObjectInputStream(socket.getInputStream());

	}

	public AddFriendView(MainView mainView) {// MainView mainView
		// TODO Auto-generated constructor stub
		this.account = mainView.getAccount();
		try {
//			socket = new Socket("localhost",6000);
			socket = new Socket("192.168.0.104",6000);
			loadStream(mainView);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		init(mainView);
		setLocationRelativeTo(null);
		setSize(400, 300);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
	}

	public void init(MainView mainView) {
		JPanel mainPanel = new JPanel(new GridLayout(2, 1));
		JPanel upPanel = new JPanel();
		JPanel downPanel = new JPanel();
		JTextField accountText = new JTextField(8);
		JButton jbtSearch = new JButton("搜索");
		JLabel jlbAccount = new JLabel("搜一下好友");
		upPanel.add(accountText);
		upPanel.add(jbtSearch);
		downPanel.add(jlbAccount);
		mainPanel.add(upPanel);
		mainPanel.add(downPanel);
		add(mainPanel);
		JButton jbtAddFriend = new JButton("添加");
		jbtSearch.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jlbAccount.setName("搜一下好友");

				String accountSearch = accountText.getText();
				downPanel.remove(jbtAddFriend);
				downPanel.invalidate();
				if (isFriendExists(account, accountSearch)) {
					jlbAccount.setText("已是好友,无须添加");
					return;
				}
				User user = getUser(accountSearch);
				// System.out.println(user==null);
				if (user == null) {
					jlbAccount.setText("查无此账号");
					return;
				}
				ImageIcon userImage = new ImageIcon(user.getPhoto());
				userImage.setImage(userImage.getImage().getScaledInstance(50, 50, Image.SCALE_AREA_AVERAGING));
				jlbAccount.setIcon(userImage);
				jlbAccount.setText(user.getUsername());

				downPanel.add(jbtAddFriend);
				downPanel.invalidate();
				jbtAddFriend.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						if (isFriendExists(account, accountSearch)) {
							jlbAccount.setText("已是好友,无须添加");
							return;
						}
						addFriend(account, accountSearch);
						JOptionPane.showMessageDialog(null, "好友请求已发送");
						mainView.shuffList();
					}
				});
				// repaint();
			}

		});
	}

	/**
	 * 从服务器获得user
	 * 
	 * @param account
	 * @return
	 */
	public User getUser(String account) {
		User user = new User();
		try {
//			objectToServer = new DataOutputStream(socket.getOutputStream());
			objectToServer.writeUTF("getUser");
			objectToServer.flush();
			objectToServer.writeUTF(account);
			objectToServer.flush();
			user = (User) objectFromServer.readObject();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

	/**
	 * 判断是否为好友
	 * 
	 * @param account
	 * @param accountSearch
	 * @return
	 */
	public boolean isFriendExists(String account, String accountSearch) {
		boolean result = false;
		try {
//			toServer = new DataOutputStream(socket.getOutputStream());
//			objectToServer = new ObjectOutputStream(socket.getOutputStream());
//			objectFromServer = new ObjectInputStream(socket.getInputStream());
//			fromServer = new DataInputStream(socket.getInputStream());
            objectToServer.writeUTF("isFriendExists");
            objectToServer.flush();
            objectToServer.writeUTF(account);
            objectToServer.flush();
            objectToServer.writeUTF(accountSearch);
            objectToServer.flush();
			result = objectFromServer.readBoolean();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public boolean addFriend(String account1, String account2) {
		boolean result=false;
		try {
//			toServer = new DataOutputStream(socket.getOutputStream());
//			objectToServer = new ObjectOutputStream(socket.getOutputStream());
//			objectFromServer = new ObjectInputStream(socket.getInputStream());
//			fromServer = new DataInputStream(socket.getInputStream());
			objectToServer.writeUTF("addFriend");
			objectToServer.flush();
			objectToServer.writeUTF(account);
			objectToServer.flush();
			objectToServer.writeUTF(account2);
			objectToServer.flush();
			result=objectFromServer.readBoolean();
//			JOptionPane.showMessageDialog(null, result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public static void main(String[] args) {
		JFrame frame = new AddFriendView(new MainView("10000018"));
	}
}

chatview:

package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import entity.User;
import util.ImagePanelUtil;

public class ChatView extends JFrame {
	private String account;
	private Socket socket;
	private ObjectOutputStream objectToServer;
	private ObjectInputStream objectFromServer;
	public void loadStream(MainView mainView) throws IOException {

//		toServer = new DataOutputStream(socket.getOutputStream());
		objectToServer = new ObjectOutputStream(socket.getOutputStream());
		objectFromServer = new ObjectInputStream(socket.getInputStream());
}
	public ChatView(MainView mainView) {
		// TODO Auto-generated constructor stub
		try {
//			socket = new Socket("localhost", 9000);
			socket = new Socket("192.168.0.104", 9000);
			loadStream(mainView);
			account = mainView.getAccount();
			objectToServer.writeUTF(account);
			objectToServer.flush();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		add(upPanel(mainView, "10000018"));
		
	}
    public ChatView(MainView mainView,Object o) {
		// TODO Auto-generated constructor stub
    	this(mainView);
//    	JOptionPane.showMessageDialog(null, "1111111--11111");
        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        container.add(upPanel(mainView, o),BorderLayout.NORTH);
        container.add(centerPanel((String)((Object[])o)[2]),BorderLayout.CENTER);
        JPanel eastPanel = new ImagePanelUtil(100, 400, "image/他的妃.jpg");
        eastPanel.setPreferredSize(new Dimension(100, 400));
        container.add(eastPanel,BorderLayout.EAST);
        setLocationRelativeTo(null);
		setSize(500, 400);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
		windowEvent();
    }
    
    public void windowEvent() {
    	addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				JOptionPane.showMessageDialog(null, "聊天框关闭");

				try {
					objectToServer.close();
					objectFromServer.close();
					socket.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
    }
    
    public ChatView(MainView mainView ,String friendAccount,String text) {
		// TODO Auto-generated constructor stub
    	this(mainView);
//    	JOptionPane.showMessageDialog(null, friendAccount);
        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        container.add(upPanel(mainView, friendAccount),BorderLayout.NORTH);
        container.add(centerPanel(friendAccount,text),BorderLayout.CENTER);
        JPanel eastPanel = new ImagePanelUtil(100, 400, "image/他的妃.jpg");
        eastPanel.setPreferredSize(new Dimension(100, 400));
        container.add(eastPanel,BorderLayout.EAST);
        setLocationRelativeTo(null);
		setSize(500, 400);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
		windowEvent();

	}
    
    public JPanel centerPanel(String... account) {
//    	JOptionPane.showMessageDialog(null, "centerPanel"+account);
    	JPanel centerPanel = new JPanel();
    	centerPanel.setLayout(new GridLayout(2,1));
    	JTextArea showArea = new JTextArea();
    	if(account.length>1) {
    	showArea.setText(account[1]);
    	}
    	showArea.setEditable(false);
    	JTextArea writeArea = new JTextArea();
    	writeArea.setBorder(BorderFactory.createTitledBorder(
    			BorderFactory.createLineBorder(Color.blue,3), "输入框"));
    	centerPanel.add(showArea);
    	centerPanel.add(writeArea); 
//        centerPanel.add(eastPanel);
    	new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(true) {
					try {
						String text=objectFromServer.readUTF();
						showArea.append(new Date()+"\n");
						showArea.append(text+"\n");
					} catch(EOFException e) {
						break;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						break;
//						e.printStackTrace();
					}
				}
			}
		}).start();
    	writeArea.addKeyListener(new KeyAdapter() {
    		@Override
    		public void keyPressed(KeyEvent e) {
    			// TODO Auto-generated method stub
    			if(e.getKeyCode()==e.VK_ENTER) {
    				String text = writeArea.getText();
    				try {
    					objectToServer.writeUTF("chat");
    					objectToServer.flush();
    					objectToServer.writeUTF(account[0]);
    					objectToServer.flush();
    					objectToServer.writeUTF(text);
    					objectToServer.flush();
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    				showArea.append("                                       "+new Date()+"\n");
    				showArea.append(text+"\n");
    				writeArea.setText("");
    			}
    		}
		});
    	return centerPanel;
    }

    public JPanel upPanel(MainView mainView,String friendAccount) {
    	JPanel upJPanel = new JPanel();
      	User user = getUser(friendAccount);
      	JOptionPane.showMessageDialog(null, user.getPhoto());
    	ImageIcon image = new ImageIcon(user.getPhoto());
    	image.setImage(image.getImage().getScaledInstance(50, 50, Image.SCALE_AREA_AVERAGING));
    	String name = friendAccount;
    	JLabel jlLabel = new JLabel(name, image, JLabel.RIGHT);
    	add(jlLabel);
    	upJPanel.add(jlLabel);
    	return upJPanel;
    }
    public JPanel upPanel(MainView mainView,Object o) {
    	JPanel upJPanel = new JPanel();
    	Object[] account = (Object[]) o;
    	ImageIcon image = (ImageIcon) account[0];
    	String name = (String) account[1];
    	JLabel jlLabel = new JLabel(name, image, JLabel.RIGHT);
    	add(jlLabel);
    	upJPanel.add(jlLabel);
    	return upJPanel;
    }
    
	public User getUser(String account) {
		User user = new User();
		try {
//			objectToServer = new DataOutputStream(socket.getOutputStream());
			objectToServer.writeUTF("getUser");
			objectToServer.flush();
			objectToServer.writeUTF(account);
			objectToServer.flush();
			user = (User) objectFromServer.readObject();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
    public static void main(String[] args) {
//		JFrame frame = new ChatView(new Object());
    	JFrame frame = new ChatView(new MainView("10000018"),"10000019");
	}
}

运行情况:


好,就这样了,挺累。结束。

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页