當前位置:首頁 » 編程語言 » java聊天工具

java聊天工具

發布時間: 2022-04-20 15:50:46

A. java 如何做一個客服聊天工具

如果做web的話,首先如果你是客戶,你發送信息到伺服器端,伺服器端把你的信息保存到資料庫中,其中這信息包含了你的目標對象的標識,也就是客服MM的工號之類的信息,然後客服MM這邊是定時到這個信息表中抓取最新的幾倏數據就行了。

B. 如何用Java寫聊天軟體

做界面肯定要swing 然後結合Socket編寫網路程序 多個客戶端的話 要啟動線程來配置每個客戶端

C. 用JAVA編寫的簡單的聊天工具程序及解釋

控制台簡單聊天工具
服務端的代碼
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Server {

public static int PORT = 1007;
private ServerSocket server;
public Server() throws IOException {
server = new ServerSocket(PORT);
}
public static void main(String[] args) throws Exception {
System.out.println("--Server--");
Server server = new Server();
server.accept();
}

public void accept() throws IOException {
Socket client = server.accept();//等待客戶端的連接
getInfo(client.getInputStream());//啟動等待消息線程
toInfo(client.getOutputStream());//啟動發送消息線程
}

//等待客戶端發送消息
public void getInfo(InputStream in) throws IOException {
final Scanner sc = new Scanner(in);//獲取客戶端的輸入流
new Thread() {
@Override
public void run() {
while(true) {
if(sc.hasNextLine()) {//如果客戶端有發送消息過來
System.out.println("Client:" + sc.nextLine());//列印客戶端的消息
}
}
}
}.start();
}

//發送消息到客戶端
public void toInfo(OutputStream out) throws IOException {
final PrintWriter pw = new PrintWriter(out, true);//獲取客戶端的輸出流,自動清空緩存的內容
final Scanner sc = new Scanner(System.in);//獲取控制台的標准輸入流,從控制台輸入數據
new Thread() {
@Override
public void run() {
while(true) {
pw.println(sc.nextLine());//將輸入的數據發送給客戶端
}
}
}.start();
}
}

客戶端的代碼
import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {

public static void main(String[] args) throws Exception, IOException {
System.out.println("--Client--");
Client client = new Client();
client.connection("localhost", Server.PORT);
}

public void connection(String host, int port) throws IOException {
Socket client = new Socket(host, port);
getInfo(client.getInputStream());
toInfo(client.getOutputStream());
}

public void getInfo(InputStream in) throws IOException {
final Scanner sc = new Scanner(in);
new Thread() {
@Override
public void run() {
while(true) {
if(sc.hasNextLine()) {
System.out.println("Server:" + sc.nextLine());
}
}
}
}.start();
}

public void toInfo(OutputStream out) throws IOException {
final PrintWriter pw = new PrintWriter(out, true);
final Scanner sc = new Scanner(System.in);
new Thread() {
@Override
public void run() {
while(true) {
pw.println(sc.nextLine());
}
}
}.start();
}
}

D. 怎麼樣用JAVA做個聊天軟體

/**
* 基於UDP協議的聊天程序
*
* 2007.9.18
* */

//導入包
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.net.*;

public class Chat extends JFrame implements ActionListener
{
//廣播地址或者對方的地址
public static final String sendIP = "127.0.0.1";
//發送埠9527
public static final int sendPort = 8000;

JPanel p = new JPanel();
List lst = new List(); //消息顯示
JTextField txtIP = new JTextField(18); //填寫IP地址
JTextField txtMSG = new JTextField(20); //填寫發送消息
JLabel lblIP = new JLabel("IP地址:");
JLabel lblMSG = new JLabel("消息:");
JButton btnSend = new JButton("發送");

byte [] buf;

//定義DatagramSocket的對象必須進行異常處理
//發送和接收數據報包的套接字
DatagramSocket ds = null;

//=============構造函數=====================
public Chat()
{

CreateInterFace();
//注冊消息框監聽器
txtMSG.addActionListener(this);
btnSend.addActionListener(this);

try
{
//埠:9527
ds =new DatagramSocket(sendPort);
}
catch(Exception ex)
{

ex.printStackTrace();
}

//============接受消息============
//匿名類
new Thread(new Runnable()
{

public void run()
{
byte buf[] = new byte[1024];

//表示接受數據報包
while(true)
{
try
{
DatagramPacket dp = new DatagramPacket(buf,1024,InetAddress.getByName(txtIP.getText()),sendPort);
ds.receive(dp);
lst.add("【消息來自】◆" + dp.getAddress().getHostAddress() + "◆"+"【說】:" + new String (buf,0,dp.getLength()) /*+ dp.getPort()*/,0);
}
catch(Exception e)
{
if(ds.isClosed())
{
e.printStackTrace();
}
}
}
}
}).start();

//關閉窗體事件
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
System.out.println("test");
int n=JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
{
dispose();
System.exit(0);
ds.close();//關閉ds對象//關閉數據報套接字
}
}
});

}

//界面設計布局
public void CreateInterFace()
{
this.add(lst,BorderLayout.CENTER);
this.add(p,BorderLayout.SOUTH);
p.add(lblIP);
p.add(txtIP);
p.add(lblMSG);
p.add(txtMSG);
p.add(btnSend);
txtIP.setText(sendIP);
//背景顏色
lst.setBackground(Color.yellow);

//JAVA默認風格
this.setUndecorated(true);
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

this.setSize(600,500);
this.setTitle("〓聊天室〓");
this.setResizable(false);//不能改變窗體大小
this.setLocationRelativeTo(null);//窗體居中
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setVisible(true);
txtMSG.requestFocus();//消息框得到焦點
}

//===============================Main函數===============================

public static void main(String[]args)
{
new Chat();
}

//================================發送消息===============================
//消息框回車發送消息事件
public void actionPerformed(ActionEvent e)
{
//得到文本內容
buf = txtMSG.getText().getBytes();

//判斷消息框是否為空
if (txtMSG.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"發送消息不能為空","提示",JOptionPane.WARNING_MESSAGE);
}
else{
try
{
InetAddress address = InetAddress.getByName(sendIP);
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);
ds.send(dp);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
txtMSG.setText("");//清空消息框

//點發送按鈕發送消息事件
if(e.getSource()==btnSend)
{
buf = txtMSG.getText().getBytes();

try
{
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);
}
catch(Exception ex)
{
ex.printStackTrace();
}

txtMSG.setText("");//清空消息框
txtMSG.requestFocus();
}

}

}

E. 用JAVA開發一個在線聊天系統需要哪些軟體

你好,提問者:
開發Java的軟體一般用eclipse或者idea就可以了。包括Java開發環境的搭建,jdk什麼的。
主要掌握的技能應該有TCP通訊協議,客戶端服務端的開發,還有多線程或線程池這些吧。
建議找視頻,或者項目研究下代碼。
如果解決了你的問題,請採納,如有疑問,請提問。謝謝!

F. 怎麼用JAVA做個聊天工具

先要做個登錄界面, 再做聊天界面 ,伺服器端, 再連資料庫, 我這剛剛寫過這個程序,
這只是一個登錄了
import java.awt.* ;
import javax.swing.* ;
import java.awt.event.*;
import java.net.* ;

public class Login extends JFrame implements ActionListener {
JTextField t_username = new JTextField() ;
JPasswordField t_password = new JPasswordField() ;

Login() {
//設置窗體屬性
this.setSize(250 , 150) ;
this.setTitle("QQ登錄") ;

int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() ;

this.setLocation((width-250)/2,(height-150)/2) ;

//new一大堆組件
JLabel l_username = new JLabel("用戶名") ;
JLabel l_password = new JLabel("密碼") ;

JButton b_login = new JButton("登錄") ;
JButton b_cancel = new JButton("取消") ;
JButton b_reg = new JButton("注冊") ;

//注冊事件監聽
b_login.addActionListener(this) ;
b_cancel.addActionListener(this) ;
b_reg.addActionListener(this) ;

//布置輸入面板
JPanel p_input = new JPanel() ;
p_input.setLayout(new GridLayout(2 ,2 )) ;

p_input.add(l_username) ;
p_input.add(t_username) ;

p_input.add(l_password) ;
p_input.add(t_password) ;

//布置按鈕面板
JPanel p_button = new JPanel() ;
p_button.setLayout(new FlowLayout()) ;

p_button.add(b_login) ;
p_button.add(b_cancel) ;
p_button.add(b_reg) ;

//布置窗體
this.setLayout(new BorderLayout()) ;

this.add(p_input , BorderLayout.CENTER) ;
this.add(p_button , BorderLayout.SOUTH) ;
}
public static void main(String args[]){
Login w = new Login() ;
w.setVisible(true) ;
}

/**
* Method actionPerformed
*
*
* @param e
*
*/
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("登錄")){
//將用戶名和密碼發送到伺服器
try {
Socket s = new Socket("127.0.0.1" , 8000) ;

MyNet mn = new MyNet(s) ;

mn.sender(t_username.getText()+"%"+t_password.getText()) ;

//接收伺服器發送來的確認信息
if(mn.receive().equals("ok")){
Main w = new Main(t_username.getText()) ;
w.setMyNet(mn) ;
w.setVisible(true) ;
this.setVisible(false) ;
}
}
catch (Exception ex) {
}
}
if(e.getActionCommand().equals("取消")){
System.exit(0) ;
}
if(e.getActionCommand().equals("注冊")){
}
}
}

G. 求用java編寫的聊天工具

這個只有源碼,沒有製作成軟體,在電腦可以直接運行,功能簡單,只有1,2項功能。如果想要完整的提交內容還是要花點錢的,畢竟這也是別人辛苦的腦力勞動。

H. 怎麼用java做區域網的聊天工具(聊天室)

呵呵,樓主您好!要用Java做聊天室說簡單也不簡單,但是說難呢也不難.
說簡單點,就是會話跟蹤技術(我個人這樣理解).要做聊天室,您需要
使用到的工具: tomcat 伺服器(因為是免費的,其他也可以哦,呵呵).
Myeclipse(sun公司提供的編寫Java程序的工具,別說你不知道哈,
哪樣的話我就暈倒了哦,呵呵)

頁面框架的設計:index.jsp(聊天室主頁面)index_top.jsp(聊天室的頂部頁面)
usersonline.jsp(在線人數的統計及顯示頁面) sendMessage.jsp(發送信息的頁面)
showMessage.jsp(顯示聊天信息的頁面)register.jsp(用戶注冊的頁面)
login.jsp(用戶登錄頁面)
當然,這是最簡單的設計方式咯.您也可以設計得更好點.

頁面介紹與功能:
index.jsp 主要是聊天室的主頁面.由上中下3個框架組成,中間部分在分為
左右2個框架.實際上index.jsp就是一個由於5個框架組成的頁面

頂部框架:放index_top.jsp頁面.可以設計自己聊天室的特色(比如說:logo)
中間部分的左邊框架:showMessage.jsp 顯示聊天的信息
中間部分的右邊框架:usersonline.jsp(在線人數的統計及顯示頁面)
底部框架:sendMessage.jsp 這個發送信息的jsp頁面.不多說吧
聊天室的框架的設計大楷就是這樣子咯

實現聊天:
1.編寫一個servlet,用戶處理的信息(包括驗證用戶是否登錄和聊天信息)。
2.用戶發送信息之後,將發送的信息存放到Application中(群聊)(放在session中就是私聊)
3.顯示信息的頁面每個XX秒中獲取session或者Application中的數據顯示出來就OK了

更多的東西還是需要您學習Ajax之後再做,會有不一樣的效果哦。祝您成功喲.呵呵

I. 誰知道有像米聊一樣的聊天軟體,是JAVA通用版本

目前還有愛悠悠和QQ通訊錄支持發送語音和圖片,具了解沃友也將推出通用的JAVA版本

J. JAVA編寫的聊天工具

//以下引入包
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.applet.*;
public class regit extends JApplet implements ActionListener,ItemListener//介面
{
String s=null;
//對象的聲明
JLabel labelname,labelpass,labelsxe,labeladdress,labelmail,labelphone;//標簽
JTextField textname,textaddress,textmail,textphone;//文本框
JRadioButton r1,r2;//單選按紐
ButtonGroup bg;//組
JPasswordField textpass;//密碼域
JButton buttonregit,buttonreset;//注冊按紐 重寫按紐
JPanel p;//面板
String sex;//定義性別字元串

URL url;//統一資源定位
BufferedWriter out1,out2;//流
BufferedReader in;

//布局方式
GridBagLayout gbl;
GridBagConstraints gc;
AppletContext co;//介面

//初始化
public void init()
{
//new 對象
labelname=new JLabel("用 戶 名:");
labelpass=new JLabel("用戶密碼:");
labelsxe=new JLabel("性別:");
labeladdress=new JLabel("地址:");
labelmail=new JLabel("電子郵件:");
labelphone=new JLabel("聯系電話:");

textname=new JTextField(15); textname.setForeground(Color.red);
textname.setToolTipText("請在這輸入你的用戶名");
textaddress=new JTextField(15);textaddress.setForeground(Color.red);
textaddress.setToolTipText("請在這輸入你的地址");
textmail=new JTextField(15); textmail.setForeground(Color.red);
textmail.setToolTipText("請在這里輸入你的E-mail地址");
textphone=new JTextField(15); textphone.setForeground(Color.red);
textphone.setToolTipText("請在這輸入你的電話號碼");

r1=new JRadioButton("男"); r1.setBackground(new Color(47,177,210));//設置顏色
r2=new JRadioButton("女"); r2.setBackground(new Color(47,177,210));//設置顏色
bg=new ButtonGroup();
bg.add(r1);bg.add(r2);//加入組,實現單選

textpass=new JPasswordField(15);
textpass.setToolTipText("在這里輸入密碼");
textpass.setForeground(Color.red);

buttonregit=new JButton("注冊"); buttonregit.setBackground(new Color(47,177,210));//設置顏色
buttonregit.setToolTipText("點擊按紐完成注冊");
buttonreset=new JButton("填寫"); buttonreset.setBackground(new Color(47,177,210));//設置顏色
buttonreset.setToolTipText("點擊按紐刷新重寫");

gbl=new GridBagLayout(); ///////////////////////////////////////
gc=new GridBagConstraints(); //////採用GridBagLayout布局方式////////

p=new JPanel();
p.setLayout(gbl);
p.setBackground(new Color(47,177,210));

this.getContentPane().add(p);//加入面板

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=2;
gbl.setConstraints(labelname,gc);
p.add(labelname);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=2;
gbl.setConstraints(textname,gc);
p.add(textname);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=4;
gbl.setConstraints(labelpass,gc);
p.add(labelpass);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=4;
gbl.setConstraints(textpass,gc);
p.add(textpass);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=6;
gbl.setConstraints(labelsxe,gc);
p.add(labelsxe);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=6;
gbl.setConstraints(r1,gc);
p.add(r1);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=5;
gc.gridy=6;
gbl.setConstraints(r2,gc);
p.add(r2);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=8;
gbl.setConstraints(labeladdress,gc);
p.add(labeladdress);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=8;
gbl.setConstraints(textaddress,gc);
p.add(textaddress);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=10;
gbl.setConstraints(labelmail,gc);
p.add(labelmail);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=10;
gbl.setConstraints(textmail,gc);
p.add(textmail);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=12;
gbl.setConstraints(labelphone,gc);
p.add(labelphone);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=12;
gbl.setConstraints(textphone,gc);
p.add(textphone);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=16;
gbl.setConstraints(buttonregit,gc);
p.add(buttonregit);

gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=16;
gbl.setConstraints(buttonreset,gc);
p.add(buttonreset);

/////////////////////////////////////////////
co=this.getAppletContext();
/////////////////////////////////////////////
buttonregit.addActionListener(this);//按紐事件的監聽
buttonreset.addActionListener(this);//按紐事件的監聽
r1.addItemListener(this);//選擇事件的監聽
r2.addItemListener(this);//選擇事件的監聽
textphone.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//注冊按紐事件
if(e.getSource()==buttonregit)
{
String s1=textname.getText(); ////////////////////
String s2=new String(textpass.getPassword());////////////////////
String s3=textaddress.getText(); // 定義字元串 //
String s4=textmail.getText(); ////////////////////
String s5=textphone.getText(); ////////////////////
//判斷注冊資料 信息 是否為空
if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)
{
int error=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(null,"資料不能為空,請重新注冊!","【溫馨提示】",error);
return;//彈出對話框並返回
}

try//寫入到txt文件
{
in=new BufferedReader(new FileReader("d:\\迷離視線聊天室\\password.txt"));
}
catch(Exception ee){}
String ss=s1;
try
{
while((s=in.readLine())!=null)
{
if(s.startsWith(ss))
{
JOptionPane.showMessageDialog(null,"用戶名已經存在,請更換名字!");
textname.setText("");//設置為空,重新輸入
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
return;
}
}
}
catch(Exception ee){}
///////////////////////////以上代碼判斷是否有同名

{
try
{
out1=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\password.txt",true));
out2=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\message.txt",true));
}//創建文件
catch(Exception ee)
{}

try
{
out1.write(s1+"#"+s2);//寫
out1.newLine();
out2.write("用戶名:"+s1);
out2.newLine();
out2.write("密碼:"+s2);
out2.newLine();
out2.write("性別:"+sex);
out2.newLine();
out2.write("地址:"+s3);
out2.newLine();
out2.write("電子郵件:"+s4);
out2.newLine();
out2.write("電話:"+s5);
out2.newLine();
out1.flush();
out2.flush();//清理緩沖
out1.close();
out2.close();
}
catch(Exception ee)
{}
JOptionPane.showMessageDialog(null,"注冊成功!");
try
{
String qss="http://localhost/chatroom/chatjiemian.htm";
url=new url(/qss);//連接上網址
co.showDocument(url);
}
catch(Exception exx)
{}
}

}
//////////////////////以下為回車事件
if(e.getSource()==textphone)
{
String s1=textname.getText(); ////////////////////
String s2=new String(textpass.getPassword());////////////////////
String s3=textaddress.getText(); // 定義字元串 //
String s4=textmail.getText(); ////////////////////
String s5=textphone.getText(); ////////////////////
//判斷注冊資料 信息 是否為空
if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)
{
int error=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(null,"資料不能為空,請重新注冊!","【溫馨提示】",error);
return;//彈出對話框並返回
}

try//寫入到txt文件
{
in=new BufferedReader(new FileReader("d:\\迷離視線聊天室\\password.txt"));
}
catch(Exception ee){}
String ss=s1;
try
{
while((s=in.readLine())!=null)
{
if(s.startsWith(ss))
{
JOptionPane.showMessageDialog(null,"用戶名已經存在,請更換名字!");
textname.setText("");//設置為空,重新輸入
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
return;
}
}
}
catch(Exception ee){}
///////////////////////////以上代碼判斷是否有同名

{
try
{
out1=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\password.txt",true));
out2=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\message.txt",true));
}//創建文件
catch(Exception ee)
{}

try
{
out1.write(s1+"#"+s2);//寫
out1.newLine();
out2.write("用戶名:"+s1+"密碼:"+s2+"性別:"+sex+"地址:"+s3+"電子郵件:"+s4+"電話:"+s5);//寫
out2.newLine();
out1.flush();
out2.flush();//清理緩沖
out1.close();
out2.close();
}
catch(Exception ee)
{}
JOptionPane.showMessageDialog(null,"注冊成功!");
try
{
String qss="http://localhost/chatroom/chatjiemian.htm";
url=new url(/qss);//連接上網址
co.showDocument(url);
}
catch(Exception exx)
{}
}

}
if(e.getSource()==buttonreset)//刷新重寫事件
{
textname.setText("");
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
}

}
//////////////////////////////////////////
//
public void itemStateChanged(ItemEvent ex)
{
if(ex.getSource()==r1)
{
sex=new String("男");
}
else if(ex.getSource()==r2)
{
sex=new String("女");
}
}
}

熱點內容
webrtc伺服器搭建哪家價格低 發布:2024-04-27 01:30:08 瀏覽:139
oracle資料庫無法啟動 發布:2024-04-27 01:29:20 瀏覽:612
倪萍超級訪問 發布:2024-04-27 01:23:29 瀏覽:704
java集合循環 發布:2024-04-27 01:17:18 瀏覽:593
解壓喪屍片 發布:2024-04-27 01:02:28 瀏覽:370
編程師加班 發布:2024-04-27 00:49:24 瀏覽:910
lol四川伺服器雲空間 發布:2024-04-27 00:42:08 瀏覽:934
卡宴怎麼看配置 發布:2024-04-27 00:41:08 瀏覽:942
央視影音緩存視頻怎麼下載視頻 發布:2024-04-27 00:25:55 瀏覽:584
手機緩存的視頻怎麼看 發布:2024-04-27 00:11:05 瀏覽:58