真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

聊天小程序的java代碼 java聊天小程序課程設(shè)計代碼

如何用java做一個聊天小程序 要求使用圖形用戶界面,可以實現(xiàn)一個聊天室中多人聊天,也可以兩人私聊,

給你一個簡單的實現(xiàn)吧,注意一定要先運行MyServer.java

從策劃到設(shè)計制作,每一步都追求做到細膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供網(wǎng)站建設(shè)、成都做網(wǎng)站、網(wǎng)站策劃、網(wǎng)頁設(shè)計、域名與空間、網(wǎng)頁空間、網(wǎng)絡(luò)營銷、VI設(shè)計、 網(wǎng)站改版、漏洞修補等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進步。

//MyCilent.java

import java.io.*;

import java.net.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MyClient extends JFrame implements ActionListener{

JTextField tf;

JTextArea tx;

JButton bt;

PrintWriter out;

public MyClient(){

tf=new JTextField(20);

tx=new JTextArea();

tx.setLineWrap(true);

tx.setWrapStyleWord(true);

JPanel pan=new JPanel();

JScrollPane jsp=new JScrollPane(tx);

add(jsp,"Center");

bt=new JButton("SEND");

bt.addActionListener(this);

pan.add(tf);

pan.add(bt);

add(pan,"South");

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

setTitle("THE CLIENT");

setSize(400,300);

setVisible(true);

try{

Socket socket=new Socket("127.0.0.1",1680);

out=new PrintWriter(socket.getOutputStream(),true);

InputStreamReader in = new InputStreamReader(socket.getInputStream());

BufferedReader sin=new BufferedReader(in);

String s;

while(true){

s=sin.readLine();

tx.append("#Server Said#: "+s+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==bt){

tx.append("@Client Said@: "+tf.getText()+"\n");

out.println(tf.getText());

tf.setText("");

}

}

public static void main(String[] args){

MyClient mct = new MyClient();

}

}

//MyServer.java

import java.io.*;

import java.net.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MyServer extends JFrame implements ActionListener{

JTextField tf;

JTextArea tx;

JButton bt;

JScrollPane jsp;

JPanel pan;

PrintWriter out;

public MyServer(){

tx=new JTextArea();

tx.setLineWrap(true);

tx.setWrapStyleWord(true);

jsp=new JScrollPane(tx);

tf=new JTextField(20);

bt=new JButton("SEND");

bt.addActionListener(this);

pan=new JPanel();

pan.add(tf);

pan.add(bt);

add(pan,"South");

add(jsp,"Center");

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

setTitle("THE SERVER");

setSize(400,300);

setVisible(true);

try{

ServerSocket server = new ServerSocket(1680);

Socket socket = server.accept();

InputStreamReader in = new InputStreamReader(socket.getInputStream());

BufferedReader sin=new BufferedReader(in);

out=new PrintWriter(socket.getOutputStream(),true);

while(true){

String s=sin.readLine();

tx.append("@Client Said@: "+s+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==bt){

String st = tf.getText();

tx.append("#Server Said#: "+st+"\n");

out.println(st);

tf.setText("");

}

}

public static void main(String[] args){

MyServer msr = new MyServer();

}

}

急需一個java編程實現(xiàn)的簡單聊天窗口代碼

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); //滾動窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("發(fā)送");

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("發(fā)送失敗");

}

}

}

}

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); //文本域,第一個參數(shù)為行數(shù),第二個參數(shù)為列數(shù)

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

JScrollPane sp=new JScrollPane(ta); //滾動窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("發(fā)送");

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(); //獲取文本框中的內(nèi)容

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

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

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

try{

bos.write(message.getBytes());

bos.flush();

}catch(Exception ex){

System.out.println("發(fā)送失敗!");

}

}

}

}

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

}

}

}

java聊天小程序

不知道你為什么要用這個 流 DataInputStream sin = new DataInputStream(System.in);

你程序沒反應(yīng)是你讀的時候一直阻塞著。你用 BufferedReader試試 肯定有反應(yīng)。


網(wǎng)頁題目:聊天小程序的java代碼 java聊天小程序課程設(shè)計代碼
轉(zhuǎn)載注明:http://weahome.cn/article/dopocod.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部