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

展开全部

客户端

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

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();

}

}

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值