展开全部

客户端

------------------------

import java.awt.*;

import java.awt.event.*;

import java.net.*;

import java.io.*;

import java.util.logging.Level;

import java.util.logging.Logger;

class myframe extends Frame implements ActionListener,WindowListener,Runnable,KeyListener

{

Thread mythread = new Thread(this);

Socket mysocket;

DataInputStream in;

DataOutputStream out;

Label label_ip = new Label("IP");

Label label_port = new Label("Port");

TextField text_ip = new TextField("127.1.1.0",15);

TextField text_port = new TextField("8888",15);

Button button_connect = new Button("连接");

TextArea text_area_show = new TextArea();

TextField text_send = new TextField(45);

Button button_send = new Button("发送");

myframe()

{

Panel panel1 = new Panel();

Panel panel2 = new Panel();

panel1.setLayout(new FlowLayout());

panel1.add(label_ip);

panel1.add(text_ip);

panel1.add(label_port);

panel1.add(text_port);

panel1.add(button_connect);

panel2.setLayout(new FlowLayout());

panel2.add(text_send);

panel2.add(button_send);

add(panel1,BorderLayout.NORTH);

add(text_area_show,BorderLayout.CENTER);

add(panel2,BorderLayout.SOUTH);

text_send.addKeyListener(this);

button_connect.addActionListener(this);

button_send.addActionListener(this);

addWindowListener(this);

this.setTitle("客户端");

setBounds(200,200,400,350);

setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == button_connect)

{

try

{

String ip = null,port = null;

ip = text_ip.getText();

port = text_port.getText();

mysocket = new Socket(ip, Integer.parseInt(port));

in = new DataInputStream(mysocket.getInputStream());

out = new DataOutputStream(mysocket.getOutputStream());

}

catch (UnknownHostException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);

}

mythread.start();

}

if(e.getSource() == button_send)

{

if(mysocket.isConnected() == true)

{

String temp = null;

temp = text_send.getText();

try

{

out.writeUTF(temp);

text_send.setText(null);

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

}

public void run()

{

while(true)

{

String temp = null;

try

{

temp = in.readUTF();

}

catch (IOException ex)

{

text_area_show.append("服务器62616964757a686964616fe58685e5aeb931333262373934退出\n");

return;

}

temp += "\n";

text_area_show.append(temp);

}

}

public void keyPressed(KeyEvent e)

{

if(e.getKeyCode() == KeyEvent.VK_ENTER)

{

String temp = null;

temp = text_send.getText();

try

{

out.writeUTF(temp);

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

public void windowOpened(WindowEvent e) {}

public void windowClosed(WindowEvent e) {}

public void windowIconified(WindowEvent e) {}

public void windowDeiconified(WindowEvent e) {}

public void windowActivated(WindowEvent e) {}

public void windowDeactivated(WindowEvent e) {}

public void keyTyped(KeyEvent e) {}

public void keyReleased(KeyEvent e) {}

}

public class mywindow

{

public static void main(String argv[])

{

myframe f = new myframe();

}

}

服务器

-------------------------------------

import java.net.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.*;

class SocketList

{

int length = 0;

Socket socketlist[] = new Socket[10];

SocketList()

{

}

void add(Socket s)

{

if(length == 10)

{

return;

}

socketlist[length] = s;

length++;

}

void Delete(Socket s)

{

int i = 0;

for(i = 0;i < length;i++)

{

if(socketlist[i] == s)

{

break;

}

}

for(int j = i;j < length;j++)

{

socketlist[j] = socketlist[j + 1];

}

length--;

/*

for(int k = 0;k < length;k++)

{

if(socketlist[k].isBound() == true)

{

System.out.print("error");

}

}*/

}

}

class FrameThread extends Thread

{

myframe frame;

FrameThread(myframe f)

{

frame = f;

start();

}

public void run()

{

while(true)

{

InetAddress addr = null;

Socket s = null;

try

{

s = frame.server.accept();

addr = s.getInetAddress();

frame.socketlist.add(s);

for(int i = 0;i < frame.socketlist.length;i++)

{

if(frame.socketlist.socketlist[i] != s )

{

DataInputStream in = new DataInputStream(frame.socketlist.socketlist[i].getInputStream());

DataOutputStream out= new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream());

out.writeUTF(addr + "连接到服务器");

}

}

}

catch (IOException ex)

{

Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex);

}

frame.text_area_show.append(addr + "连接到服务器\n");

try

{

frame.list.add(InetAddress.getLocalHost().getHostAddress() + "-" + s.getPort());

}

catch (UnknownHostException ex)

{

Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex);

}

try

{

new ClientThread(s, frame);

}

catch (IOException ex)

{

Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

}

class ClientThread extends Thread

{

DataInputStream in = null;

DataOutputStream out = null;

Socket mysocket = new Socket();

myframe frame = null;

InetAddress addr = null;

ClientThread(Socket s,myframe f) throws IOException

{

mysocket = s;

addr = mysocket.getLocalAddress();

frame = f;

in = new DataInputStream(mysocket.getInputStream());

out = new DataOutputStream(mysocket.getOutputStream());

out.writeUTF("成功连接服务器");

start();

}

public void run()

{

while(true)

{

String temp = null;

try

{

temp = in.readUTF();

for(int i = 0;i < frame.socketlist.length;i++)

{

DataInputStream in_temp = new DataInputStream(frame.socketlist.socketlist[i].getInputStream());

DataOutputStream out_temp = new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream());

out_temp.writeUTF(addr + "说: " + temp);

}

frame.text_area_show.append(addr + "说: " + temp + "\n");

}

catch (IOException ex)

{

frame.socketlist.Delete(mysocket);

frame.text_area_show.append(addr + "退出服务器\n");

for(int i = 0;i < frame.socketlist.length;i++)

{

try

{

DataInputStream in_temp = new DataInputStream(frame.socketlist.socketlist[i].getInputStream());

}

catch (IOException ex1)

{

Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1);

}

DataOutputStream out_temp = null;

try

{

out_temp = new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream());

}

catch (IOException ex1)

{

Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1);

}

try

{

out_temp.writeUTF(addr + "退出服务器");

}

catch (IOException ex1)

{

Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1);

}

}

return;

}

}

}

}

class myframe extends JFrame implements ActionListener,WindowListener,KeyListener

{

List list = new List(16);

TextArea text_area_show = new TextArea(15,40);

TextField text_send = new TextField(50);

Button button_send = new Button("发送");

ServerSocket server = null;

FrameThread thread;

SocketList socketlist = new SocketList();

myframe() throws IOException

{

Panel panel1 = new Panel();

panel1.setLayout(new FlowLayout());

panel1.add(text_area_show);

panel1.add(list);

list.addActionListener(this);

Panel panel2 = new Panel();

panel2.setLayout(new FlowLayout());

panel2.add(text_send);

panel2.add(button_send);

setLayout(new FlowLayout());

add(panel1);

add(panel2);

text_send.addKeyListener(this);

button_send.addActionListener(this);

addWindowListener(this);

server = new ServerSocket(8888);

thread = new FrameThread(this);

setTitle("服务器");

setBounds(200,200,450,330);

setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == list)

{

text_area_show.append(list.getSelectedItem() + "\n");

}

if(e.getSource() == button_send)

{

for(int i = 0;i < socketlist.length;i++)

{

try

{

DataInputStream in = new DataInputStream(socketlist.socketlist[i].getInputStream());

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);

}

DataOutputStream out = null;

try

{

out = new DataOutputStream(socketlist.socketlist[i].getOutputStream());

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);

}

try

{

out.writeUTF(text_send.getText());

text_send.setText(null);

// text_area_show.append(text_send.getText() + "\n");

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

}

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

public void windowOpened(WindowEvent e) {}

public void windowClosed(WindowEvent e) {}

public void windowIconified(WindowEvent e) {}

public void windowDeiconified(WindowEvent e) {}

public void windowActivated(WindowEvent e) {}

public void windowDeactivated(WindowEvent e) {}

public void keyTyped(KeyEvent e) {}

public void keyPressed(KeyEvent e)

{

if(e.getKeyCode() == KeyEvent.VK_ENTER)

{

for(int i = 0;i < socketlist.length;i++)

{

try

{

DataInputStream in = new DataInputStream(socketlist.socketlist[i].getInputStream());

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);

}

DataOutputStream out = null;

try

{

out = new DataOutputStream(socketlist.socketlist[i].getOutputStream());

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);

}

try

{

out.writeUTF(text_send.getText());

text_area_show.append(text_send.getText() + "\n");

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

}

public void keyReleased(KeyEvent e) {}

}

public class mywindow

{

public static void main(String argv[]) throws IOException

{

myframe f = new myframe();

}

}

本回答由提问者推荐

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

java jui_急求用带jui界面写的java聊天程序!!!相关推荐

  1. python实现的、带GUI界面电影票房数据可视化程序

    代码地址如下: http://www.demodashi.com/demo/14588.html ##详细说明: Tushare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从 ...

  2. 利用Java GUI,结合Java Socket,Java多线程,服务器,制作一个简单的具有界面的多用户实时聊天程序(从GUI,到Socket,到多线程,到服务器,项目级别详细教程)

    目录 规划 Java GUI设计 Java Socket Java 服务器 利用 IntelliJ IDEA软件为例 首先,我们应当了解,像运行在两台电脑或者手机终端这样的程序一般是没有办法直接相互发 ...

  3. 写java线程导致电脑内存不足_如何写出让java虚拟机发生内存溢出异常OutOfMemoryError的代码...

    程序小白在写代码的过程中,经常会不经意间写出发生内存溢出异常的代码.很多时候这类异常如何产生的都傻傻弄不清楚,如果能故意写出让jvm发生内存溢出的代码,有时候看来也并非一件容易的事.最近通过学习< ...

  4. 从0-1带你手写代码生成器(Java版)

    前言 在实际的项目开发中,根据数据库表创建实体.service.controller等结构是一件非常繁琐的事.所以我们经常需要使用到各种代码生成器,例如mybatis-plus,若依等框架都有自己的代 ...

  5. java有没有求组合的函数_如何在Java 8中使用compose和andThen组合函数

    在这篇文章中,课课家将讨论利用java8的Function接口提供的两个组合函数--compose和andThen来实现函数的组合. 什么是函数组合? 首先需要创建一些小的可重用函数,然后将这些小函数 ...

  6. java图遍历求最长路径_如何在Java中使用递归实现矩阵中最长路径的返回

    我正试图用递归来解决这个问题. 问题是:对于二维正整数数组,我如何返回最长路径(步骤),以便最长路径的每个单元格中的值是从整数的降序序列开始的,并且每个单元格和单元格之间的差异是一个给定的数字(num ...

  7. java 异常处理 return,求大神!!!!java 异常处理中return的意思

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 在方法中使用return 就是返回到方法的调用方,而在main 方法中使用,返回到JVM(虚拟机),如果把上面的main 方法中的代码放在一个方法中,如下 ...

  8. 用matlab实现女声变男声步骤,急求能用的matlab 男声变女声的程序....!!!

    这是从论坛https://www.ilovematlab.cn/thread-101925-1-45.html下载的m文件 : % ct1 clear all,close all, clc; % 定义 ...

  9. 一文带你深入理解【Java基础】· Java基本语法:程序流程控制

    写在前面 Hello大家好, 我是[麟-小白],一位软件工程专业的学生,喜好计算机知识.希望大家能够一起学习进步呀!本人是一名在读大学生,专业水平有限,如发现错误或不足之处,请多多指正!谢谢大家!!! ...

  10. 一文带你深入理解【Java基础】· 数组

    写在前面 Hello大家好, 我是[麟-小白],一位软件工程专业的学生,喜好计算机知识.希望大家能够一起学习进步呀!本人是一名在读大学生,专业水平有限,如发现错误或不足之处,请多多指正!谢谢大家!!! ...

最新文章

  1. 2022-2028年中国刨花板市场投资分析及前景预测报告(全卷)
  2. Xamarin.FormsShell基础教程(7)Shell项目关于页面的介绍
  3. python去哪里学-杭州python编程入门去哪学?
  4. Firefox联手Chrome合作开发网页VR标准
  5. GridView格式化数据失效
  6. 判断一个点是否在三角形内
  7. Xamarin Live Player Preview 2: 连续运行和调试应用程序
  8. Migw用CMD编译C语言,NOTEPAD++用MinGW编译C,C++语言
  9. app推送以及提示音java,springboot 整合 Jpush 极光推送
  10. mysql or全表_mysql or条件可以使用索引而避免全表
  11. Web安全测试实战之测试HTTP方法
  12. 免费UI圆角字体素材|字体设计密码:字形设计中“圆角”的应用规范
  13. 如果“王思聪”们创业就一定能成功
  14. Swift 新建 APP 黑屏问题
  15. Codeforces Round #734 (Div. 3)_B2. Wonderful Coloring - 2(贪心)
  16. webrtc 研究-带宽控制
  17. 对接中国银联刷卡支付系统架构小demo
  18. 基于成本效益的深度信任网络的智能LEACH的多级动态优化附Matlab代码
  19. 次世代游戏建模这7问
  20. 独立后台带分销功能月老办事处交友盲盒微信小程序源码新版,更好的裂变推广引流

热门文章

  1. 镜头超声波马达的工作原理
  2. 2013/9/2 ruby on rails学习感想
  3. 平面设计师主要做什么?平面设计的工作内容有哪些?
  4. 软件测试技术+HR面试
  5. 3.字体样式,分隔线与段落
  6. php怎么将农历转换成公历,php农历转公历怎么实现
  7. 人物连连看 python课程设计 pygame学习
  8. TOM邮箱|国内适合商务人士的邮箱是什么邮箱
  9. 欧美文学与信仰传统书摘
  10. git如何选择性合并_git - 如何挑选一系列提交并合并到另一个分支?