首页>代码>java socket聊天最简单的示例>/qq/src/com/wenqier/test/Test.java
package com.wenqier.test;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.wenqier.get.GetMessage;
import com.wenqier.send.SendMessage;

/**
 * 用户界面
 * @author wenqier
 *
 */
class WindowTextArea extends JFrame implements ActionListener {

	String s;
	JTextArea text1;
	JTextArea text2;
	JButton button1, button2, button3;
	SendMessage t2;
	GetMessage t1;
	JLabel lable1, lable2;
	JTextField text;
	JPanel jPanel;

	WindowTextArea() {

		jPanel = new JPanel();
		
		lable1 = new JLabel("对方ip");
		text = new JTextField(20);

		text1 = new JTextArea(3, 38);
		text2 = new JTextArea(6, 38);
		text2.setEditable(false);
		
		button1 = new JButton("发送");
		button2 = new JButton("关闭");
		button3 = new JButton("确定ip");

		setBounds(100, 100, 450, 300);
		setVisible(true);
		
		Container con = getContentPane();
		con.setLayout(new FlowLayout());
		con.add(lable1);
		con.add(text);
		con.add(button3);

		BorderLayout b = new BorderLayout();
		jPanel.setLayout(b);
		
		jPanel.add(new JScrollPane(text1), b.SOUTH);
		jPanel.add(new JScrollPane(text2), b.NORTH);
		
		con.add(jPanel);

		con.add(button1);
		con.add(button2);

		button1.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		con.validate();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	public void runthread(String ip) {
		t1 = new GetMessage(5050, text2);
		t1.start();
		t2 = new SendMessage(ip, 5050, text2);
		t2.start();
	}

	public void actionPerformed(ActionEvent e) {
		// 事件接听处理
		if (e.getSource() == button2) {
			System.exit(0);
		}
		if (e.getSource() == button1) {

			text2.append("你说:" + text1.getText() + "\n");

			t2.send(text1.getText());

			text1.setText("");
		}
		if (e.getSource() == button3) {
			s = text.getText();

			runthread(s);

		}
	}
}

/**
 * 测试
 * 
 * @author wenqier
 * 
 */
public class Test {

	public static void main(String[] args) {

		new WindowTextArea();

	}
}
最近下载更多
28128觅北  LV2 2024年5月22日
zeng1206  LV7 2023年12月28日
微信网友_6712701718712320  LV1 2023年10月29日
yitian111  LV1 2021年10月5日
kraynxn  LV8 2021年9月28日
lyws1986  LV17 2021年7月19日
王小懒猪  LV1 2021年6月29日
tim135790  LV1 2021年4月18日
liangge2115  LV27 2021年1月1日
zxp888  LV4 2020年12月18日
最近浏览更多
ilsfcqdf  LV3 2024年9月12日
周鸣郝  LV2 2024年5月26日
28128觅北  LV2 2024年5月22日
zeng1206  LV7 2023年12月28日
微信网友_6712701718712320  LV1 2023年10月29日
阿超在大学修仙  LV1 2023年9月15日
qq1357574774  LV2 2023年7月20日
fuyouou  LV5 2023年6月26日
1WQAQW1  LV2 2023年6月12日
aixiao5213  LV1 2022年12月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友