⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframtest.java

📁 简单的QQ聊天。。服务器端可监视客户端上下线情况
💻 JAVA
字号:
package com.chat.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.JLabel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import com.chat.constants.ClientConstants;
import com.chat.dto.MsgDTO;
import com.chat.io.TxtManager;


import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.io.*;
import main.ClientMainClass;
import java.util.*;

@SuppressWarnings("serial") 
public class MainFramtest extends JFrame implements Runnable

{

	private Socket socket;

	private ObjectInputStream ois;

	private ObjectOutputStream oos;

	private boolean isConnected = false;

    private JList frendList=null;

	private MsgDTO tempMsgDTO = new MsgDTO();

	private static String us;

	private String ip = "127.0.0.1";

	private int port = 8888;
	
	protected static List<MsgDTO> ms = new ArrayList<MsgDTO>();
	
	HashMap hm=new HashMap();    

	public MainFramtest(String userId, String ip, String port) {
		super();
		this.setSize(200, 600);
		this.tempMsgDTO.setUserId(userId);//!!
		this.ip = ip;
		this.port = Integer.parseInt(port);
		us=userId;

		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBackground(new Color(128, 128, 255));
		panel.setPreferredSize(new Dimension(0, 60));
		getContentPane().add(panel, BorderLayout.NORTH);

		final JPanel panel_7 = new JPanel();
		panel_7.setLayout(new BorderLayout());
		panel_7.setBounds(10, 10, 40, 40);
		panel.add(panel_7);

		final JLabel image = new JLabel();
		image
				.setIcon(new ImageIcon(getClass().getResource(
						"/images/head2.jpg")));
		panel_7.add(image, BorderLayout.CENTER);

		final JLabel numberLabel = new JLabel();
		numberLabel.setText("用户: " + this.tempMsgDTO.getUserId() + " 当前在线");
		numberLabel.setBounds(65, 24, 117, 15);
		panel.add(numberLabel);

		final JPanel panel_1 = new JPanel();
		panel_1.setLayout(new BorderLayout());
		getContentPane().add(panel_1, BorderLayout.CENTER);

		final JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.setBackground(new Color(255, 255, 255));
		// tabbedPane.set
		panel_1.add(tabbedPane, BorderLayout.CENTER);
		tabbedPane.setTabPlacement(JTabbedPane.LEFT);

		final JPanel panel_3 = new JPanel();
		panel_3.setLayout(new BorderLayout());
		tabbedPane.addTab("我的好友", null, panel_3, null);
		
		System.out.println("当前qq"+MainFramtest.this.tempMsgDTO.getUserId());
		
		frendList=new JList();
        frendList.setCellRenderer(new chat.list.ChecBoxListCellRender());
        frendList.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getButton()==MouseEvent.BUTTON1 && e.getClickCount() == 2) {
					
					ListModel lm=frendList.getModel();
					String useridcomein=lm.getElementAt(frendList.getSelectedIndex()).toString();
					
					
					String useridself=MainFramtest.this.tempMsgDTO.getUserId();
					System.out.println(useridself+"与"+useridcomein+"对话");
					ClientFramenew cl=new ClientFramenew(useridself,useridcomein,MainFramtest.this);
					

				}
			}
		});
        
		panel_3.add(frendList);
		//frendList.setListData(new Object[]{"孙伟"
		//,"孟轲"});		
	

		final JPanel panel_2 = new JPanel();
		panel_2.setLayout(null);
		panel_2.setBackground(new Color(255, 255, 255));
		/*final Littlepanel li = new Littlepanel();
		li.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {

				}
			}
		});
		li.setBounds(35, 30, 52, 43);
		li.setSize(50, 50);
		panel_2.add(li);*/
		tabbedPane.addTab("黑名单", null, panel_2, null);

		final JPanel panel_4 = new JPanel();
		panel_4.setLayout(new FlowLayout());
		tabbedPane.addTab("陌生人", null, panel_4, null);

		final JPanel panel_6 = new JPanel();
		panel_6.setBackground(new Color(255, 255, 255));
		panel_6.setLayout(new BorderLayout());
		tabbedPane.addTab("系统设置", null, panel_6, null);

		final JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBackground(new Color(255, 255, 255));
		scrollPane_1.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		panel_6.add(scrollPane_1, BorderLayout.CENTER);

		final JPanel panel_5 = new JPanel();
		panel_5.setLayout(null);
		panel_5.setBackground(new Color(128, 128, 255));
		panel_5.setPreferredSize(new Dimension(0, 40));
		getContentPane().add(panel_5, BorderLayout.SOUTH);

		final JButton button = new JButton();
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				send(ClientConstants.LOGOUT+ClientConstants.LOGIN_SPLIT+tempMsgDTO.getUserId());
				dispose();
				System.exit(0);
			}
		});
		button.setText("退出");
		button.setBounds(10, 10, 76, 23);
		panel_5.add(button);

		final JLabel hi = new JLabel();
		hi.setIcon(new ImageIcon(getClass().getResource(
				"/images/ChatRoomButton.gif")));
		hi.setBounds(153, 14, 17, 15);
		hi.setToolTipText("");
		panel_5.add(hi);

		final JLabel hi_1 = new JLabel();
		hi_1.setToolTipText("");
		hi_1.setIcon(new ImageIcon(getClass().getResource(
		"/images/QQTVButton.gif")));
		hi_1.setBounds(130, 14, 17, 15);
		panel_5.add(hi_1);

		final JLabel hi_2 = new JLabel();
		hi_2.setToolTipText("");
		hi_2.setIcon(new ImageIcon(getClass().getResource(
		"/images/1381Game.gif")));
		hi_2.setBounds(105, 14, 17, 15);
		panel_5.add(hi_2);

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.connect();
		new Thread(this).start();
	}

	public static void main(String args[]) {
		// new MainFramtest("adad","sss","80");
		try {
			MainFramtest frame = new MainFramtest(args[0],args[1],args[2]);
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void connect(){
		try{
			socket=new  Socket(this.ip,this.port);
			oos=new ObjectOutputStream(socket.getOutputStream());
			ois=new ObjectInputStream(socket.getInputStream());
			this.isConnected=true;
			
			System.out.println("连接成功");
			
			send(ClientConstants.LOGIN_REQUEST+ClientConstants.LOGIN_SPLIT+this.tempMsgDTO.getUserId());
			this.setVisible(true);
		}catch(IOException e){
			e.printStackTrace();
			javax.swing.JOptionPane.showMessageDialog(null, "登陆失败,服务器不存在或端口错误!");
			//dispose();
			System.exit(0);
			
		}
	}

	public boolean send(Object obj) {
		try {
			oos.writeObject(obj);
			oos.flush();
			return true;
		} catch (IOException e) {
			System.out.println(e.getMessage());
//			System.out.println("发送失败");
			javax.swing.JOptionPane.showMessageDialog(null, "信息发送失败,服务器可能已经关闭!");
			e.printStackTrace();
			return false;
		}

	}
	public void run(){
		try{
			while(isConnected==true){
			Object tempObj = ois.readObject();
			if(tempObj instanceof String){
				if(tempObj.equals(ClientConstants.LOGIN_REPEAT)){
						dispose();
						javax.swing.JOptionPane.showMessageDialog(null, "重复登录");
						ClientMainClass.main(new String[0]);
				}

				if(tempObj.equals(ClientConstants.LOGIN_SUCCESS)){
					TxtManager.saveUserId(this.tempMsgDTO.getUserId());//?
				}
				
			}
			if(tempObj instanceof java.util.List){
				
				java.util.List list=(java.util.List)tempObj;

				frendList.setListData(list.toArray());
				
				frendList.repaint();
				
				
			}			
			if(tempObj instanceof MsgDTO ){
				MsgDTO tempMsgDTO = (MsgDTO) tempObj;
				//setMessageText(tempMsgDTO);
				//hm.put(tempMsgDTO.getUserId(), tempMsgDTO);
				if(tempMsgDTO.getToWhere().equals(us)){//this.tempMsgDTO.getUserId()
				ms.add(tempMsgDTO);
				System.out.println("主面板接收的包的towhere"+
						tempMsgDTO.getToWhere()+"主面板的id"+us);
				}
				
			}
		}
		}
		catch(IOException e){System.out.println(e.getMessage());}
		catch(ClassNotFoundException e){System.out.println(e.getMessage());}
	}
	
	


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -