package com.demo;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import java.awt.event.ActionEvent;


/**
 * 这是聊天室  客户端
 */
public class ChatRoom extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	private Socket socket =null;
	private JTextField textField_3;
	//昵称
	private String name="游客";
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ChatRoom frame = new ChatRoom();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ChatRoom() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 670, 432);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
	     JScrollPane scrollPane_1 = new JScrollPane();
	        scrollPane_1.setBounds(24, 69, 294, 189);
	        contentPane.add(scrollPane_1);                
	    
		JTextArea textArea = new JTextArea();
		textArea.setBounds(24, 69, 294, 189);
		scrollPane_1.setViewportView(textArea);
		JButton button = new JButton("连接服务器");
		button.setBounds(387, 10, 143, 23);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if(button.getText().equals("连接服务器")) {
					
					try {
						socket = new Socket(textField.getText(), Integer.valueOf(textField_1.getText()));
						new SocketData2(socket, textArea).start();
						
					} catch (IOException e) {
						try {
							socket.close();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}finally {
						button.setText("断开连接");
					}
				}else {
					try {
						socket.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}finally {
						button.setText("连接服务器");
					}
				}
				
			}
		});
		contentPane.setLayout(null);
		contentPane.add(button);
		
		JLabel lblIp = new JLabel("ip");
		lblIp.setBounds(24, 18, 23, 15);
		contentPane.add(lblIp);
		
		JLabel label = new JLabel("端口");
		label.setBounds(181, 14, 48, 15);
		contentPane.add(label);
		
		textField = new JTextField();
		textField.setBounds(63, 15, 66, 21);
		textField.setText("127.0.0.1");
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(252, 11, 66, 21);
		textField_1.setText("8080");
		textField_1.setColumns(10);
		contentPane.add(textField_1);
		
	
		contentPane.add(scrollPane_1);
		
		JButton button_1 = new JButton("发送");
		button_1.setBounds(484, 318, 79, 23);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if( socket!=null ) {
					/*
					 * 方法2 比较简单   但是不通用   好多测试工具都是用的byte 字节   方法2 不能接受byte字节 
					 * 
					 */
					
					//发送方法1
					try {
						socket.getOutputStream().write((name+":"+textField_3.getText()).getBytes());
					} catch (IOException e1) {
						try {
							socket.close();
						} catch (IOException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
//						e1.printStackTrace();
					}
					//发送方法2 
					/* try {
							 DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
							 dos.writeUTF(name+":"+textField_3.getText());
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} */
				}
			}
		});
		contentPane.add(button_1);
		
		JLabel label_1 = new JLabel("自定义昵称");
		label_1.setBounds(484, 74, 66, 15);
		contentPane.add(label_1);
		
		textField_2 = new JTextField();
		textField_2.setBounds(484, 126, 66, 21);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		JButton button_2 = new JButton("设置昵称");
		button_2.setBounds(484, 176, 93, 23);
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				name = textField_2.getText();
			}
		});
		contentPane.add(button_2);
		
		textField_3 = new JTextField();
		textField_3.setBounds(24, 319, 281, 21);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		JButton button_3 = new JButton("清空记录");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textArea.setText("");
			}
		});
		button_3.setBounds(484, 235, 93, 23);
		contentPane.add(button_3);
	}
}
最近下载更多
jxd2007kl  LV2 2023年11月29日
谢小饭_  LV8 2022年1月21日
9843637  LV9 2021年12月15日
tangjj7260  LV18 2021年12月10日
dfz12345  LV4 2021年12月8日
 LV2 2021年6月16日
miner22  LV9 2021年5月27日
chat511  LV3 2021年5月12日
15508061020  LV1 2020年9月3日
BruceQ  LV14 2020年7月15日
最近浏览更多
zeng1206  LV5 2023年12月28日
jxd2007kl  LV2 2023年11月29日
jidea  LV2 2023年11月29日
阿布屋脊  LV7 2023年8月15日
fuyouou  LV5 2023年6月29日
CL200228  LV4 2023年5月4日
17771013609  LV4 2023年4月12日
sslhss 2022年11月30日
暂无贡献等级
2543317781 2022年11月26日
暂无贡献等级
罗清晨  LV11 2022年9月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友