java编写简单聊天界面_急需一个java编程实现的简单聊天窗口代码

展开全部

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

public class ClientDemo01 {

public static void main(String[] args){

JFrame f=new JFrame("AA");

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JTextArea ta=new JTextArea(15,30);

ta.setEditable(false); //文本域只读

JScrollPane sp=new JScrollPane(ta); //滚动窗格e69da5e6ba903231313335323631343130323136353331333332626162

JTextField tf=new JTextField(20);

JButton b=new JButton("发送");

p1.add(sp);

p2.add(tf);

p2.add(b);

f.add(p1,"Center");

f.add(p2,"South");

f.setBounds(300,300,360,300);

f.setVisible(true);

f.setResizable(false);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Socket socket=null;

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

try{

socket=new Socket("192.168.0.4",5000);

bis=new BufferedInputStream(socket.getInputStream());

bos=new BufferedOutputStream(socket.getOutputStream());

MyThread01 mt=new MyThread01(bis,ta);

mt.start();

}catch(Exception e){

e.printStackTrace();

}

b.addActionListener(new ButtonActionListener01(tf,ta,bos));

}

}

class ButtonActionListener01 implements ActionListener{

JTextField tf;

JTextArea ta;

BufferedOutputStream bos;

public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){

this.tf=tf;

this.ta=ta;

this.bos=bos;

}

public void actionPerformed(ActionEvent e){

String message=tf.getText();

if(!message.equals("")){

tf.setText(""); //清空文本框

ta.append("AA:"+message+"\n"); //添加到文本域并换行

try{

bos.write(message.getBytes());

bos.flush();

}catch(Exception ex){

System.out.println("发送失败");

}

}

}

}

class MyThread01 extends Thread{

BufferedInputStream bis;

JTextArea ta;

public MyThread01(BufferedInputStream bis,JTextArea ta){

this.bis=bis;

this.ta=ta;

}

public void run(){

try{

while(true){

byte[] b=new byte[100];

int length=bis.read(b);

String message=new String(b,0,length);

ta.append("BB:"+message+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

} import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

public class ServerDemo01{

public static void main(String[] args){

JFrame f=new JFrame("BB");

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JTextArea ta=new JTextArea(12,30); //文本域,第一个参数为行数,第二个参数为列数

ta.setEditable(false); //文本域只读

JScrollPane sp=new JScrollPane(ta); //滚动窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("发送");

p1.add(sp);

p2.add(tf);

p2.add(b);

f.add(p1,"Center");

f.add(p2,"South");

f.setBounds(300,300,360,300);

f.setVisible(true);

f.setResizable(false);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ServerSocket server=null;

Socket socket=null;

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

try{

server=new ServerSocket(5000);

//ta.append("等待AA连接...\n");

socket=server.accept();

//ta.append("AA已连接\n");

bis=new BufferedInputStream(socket.getInputStream());

bos=new BufferedOutputStream(socket.getOutputStream());

MyThread1 mt=new MyThread1(bis,ta);

mt.start();

}catch(Exception e){

e.printStackTrace();

}

b.addActionListener(new ButtonActionListener1(tf,ta,bos));

}

}

class ButtonActionListener1 implements ActionListener{

JTextField tf;

JTextArea ta;

BufferedOutputStream bos;

public ButtonActionListener1(JTextField tf,JTextArea ta,BufferedOutputStream bos){

this.tf=tf;

this.ta=ta;

this.bos=bos;

}

public void actionPerformed(ActionEvent e){

String message=tf.getText(); //获取文本框中的内容

if(!message.equals("")){

tf.setText(""); //清空文本框

ta.append("BB:"+message+"\n"); //添加到文本域并换行

try{

bos.write(message.getBytes());

bos.flush();

}catch(Exception ex){

System.out.println("发送失败!");

}

}

}

}

class MyThread1 extends Thread{

BufferedInputStream bis;

JTextArea ta;

public MyThread1(BufferedInputStream bis,JTextArea ta){

this.bis=bis;

this.ta=ta;

}

public void run(){

try{

while(true){

byte[] b=new byte[100];

int length=bis.read(b);

String message=new String(b,0,length);

ta.append("AA:"+message+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java语言是一种非常强大的编程语言,可以用来开发各种应用程序,包括聊天窗口界面。在本文中,我们将介绍使用Java编写简单聊天窗口界面的步骤。 首先,我们需要创建一个Java应用程序。我们可以使用Eclipse或NetBeans等集成开发环境来创建Java应用程序。在创建项目时,我们需要选择“Swing应用程序”模板,这将创建包含一个主窗口和若干个组件的初始代码。 接下来,我们需要添加聊天窗口界面的组件。组件包括文本输入框、按钮、消息显示框等。我们可以使用Swing提供的组件库来添加这些组件。例如,使用JTextField可以添加一个文本输入框;使用JButton可以添加一个按钮。我们还可以使用JTextArea来显示聊天消息。 聊天窗口界面需要支持发送和接收消息的功能。我们可以添加一个发送按钮,当用户单击该按钮时,程序会将文本框中输入的消息发送给对方。在接收消息时,程序需要将对方发送的消息显示在消息显示框中。为了实现这些功能,我们需要为发送按钮添加单击事件,并使用Socket编写服务器端和客户端程序。 最后,我们需要测试聊天窗口界面。我们可以在同一台计算机上模拟两个聊天用户,分别运行服务器端和客户端程序。当我们在客户端输入一条聊天消息并单击发送按钮时,服务器端程序将接收到该消息,并将消息发送给另一个客户端。客户端接收到消息后,将其显示在消息显示框中。 总结一下,用Java编写简单聊天窗口界面需要我们完成以下步骤:创建Java应用程序、添加聊天窗口界面的组件、添加发送和接收消息的功能,并测试聊天窗口界面。如果您想二次开发该应用程序,还可以加入其他功能,例如消息记录、在线状态显示等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值