package com.test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class Client extends JFrame implements ActionListener{
	private JTextArea txtSend;
	private JTextArea txtMsg;
	private JButton btnSend,btnHistory;
	@SuppressWarnings("rawtypes")
	private JList lUser;
	private Vector<String> list;
	private DataOutputStream outputStream;
	private Socket socket;
	private String userName;
	private Parameterx parameterx;
	public Client(Socket socket, String name){
		this.userName = name;
		this.socket = socket;
		setBounds(200,300, 600, 400);
		setLayout(new BorderLayout());
		setTitle(name + "的聊天室");
		init();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void init(){
		txtSend = new JTextArea(3, 10);
		txtMsg = new JTextArea();
		txtMsg.setEditable(false);
		btnSend = new JButton("发送");
		btnHistory = new JButton("消息日志");
		btnHistory.addActionListener(this);
		btnSend.addActionListener(this);
		JScrollPane jspSend = new JScrollPane(txtSend);
		jspSend.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		JPanel p1 = new JPanel(new BorderLayout());
		p1.setBorder(new TitledBorder("对话框"));
		JPanel pp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		pp.add(btnHistory);
		p1.add(pp, "North");
		p1.add(jspSend,"Center");
		p1.add(btnSend, "East");
		
		
		list = new Vector<String>();
		list.add(userName);
		lUser = new JList(list);
		JScrollPane left = new JScrollPane(lUser);
		left.setBorder(new TitledBorder("在线用户"));
		
		JScrollPane right = new JScrollPane(txtMsg);
		right.setBorder(new TitledBorder("消息框"));
		
		JSplitPane p2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left,right);  
        p2.setDividerLocation(100);
        

        add(p2, "Center");
        add(p1, "South");
        
        parameterx = new Parameterx();
        
        try {
			outputStream = new DataOutputStream(socket.getOutputStream());
			outputStream.writeUTF("renew|" + userName);
			outputStream.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
        new Thread(new ClientThread(socket,	txtMsg, list, txtSend, lUser)).start();
	}
	@SuppressWarnings("static-access")
	public void actionPerformed(ActionEvent e){
		
		if(e.getSource() == btnHistory){
			//this.dispose();
			new ChatHistory();
		}else if(e.getSource() == btnSend){
			String strSend = txtSend.getText().trim();
			if(strSend.equals("")){
				JOptionPane.showMessageDialog(null, parameterx.nomessage);
			}else{
				try{
					outputStream = new DataOutputStream(socket.getOutputStream());
					outputStream.writeUTF("message|" + strSend + "|" + userName);
					outputStream.flush();
					
				}catch (IOException ef) {
					// TODO: handle exception
				}
			}
		}
	}
}
最近下载更多
taoshen95  LV14 1月19日
漫步的海星  LV4 2023年8月21日
2017143155  LV12 2023年6月24日
wanglinddad  LV54 2022年6月18日
微信网友_6003487859068928  LV5 2022年6月16日
zhendong  LV7 2022年3月6日
dfz12345  LV4 2021年12月8日
胡萝北呀  LV3 2021年11月28日
ComeDebug  LV6 2021年9月6日
忧麦紫  LV18 2021年6月26日
最近浏览更多
taoshen95  LV14 1月19日
陈小灏  LV14 2023年12月27日
求学的熊猫  LV6 2023年12月25日
wuxin427 2023年12月20日
暂无贡献等级
fuyouou  LV5 2023年6月29日
2017143155  LV12 2023年6月24日
17683946472  LV9 2023年6月8日
CL200228  LV4 2023年5月4日
青柠编程  LV13 2023年4月18日
xingbing  LV9 2023年2月10日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友