Ⅰ java 用udp 如何在聊天界面上顯示誰上線
伺服器端維護一個在線用戶的列表,當一個人上線的時候,這個人放到這個列表中,同時查找這個人的所有好友,並將好友及在線好友的地址發送給這個用戶。然後讓伺服器或者用戶本人發送他上線的消息,給每個在線的好友。
每過一段時間,這個人要和伺服器ping一下,若這個人沒有ping,則向這個人的所有在線好友發送這個人下線的消息。
Ⅱ java中如何實現多窗口聊天求大神幫忙
多開幾個聊天窗口就行了。弄一個輔助類,這個類裡面記錄當前打開的聊天窗口的句柄,這樣方便關閉指定的聊天窗口。
或者是直接把所有的窗口集成在一個窗口裡面,弄成面板的模式,上面的新開一個窗口在這里就意味著新開一個面板。這種方式比較符合現在人的習慣,個人推薦。就是這種的http://blog.csdn.net/sweetgirl520/article/details/51346263。
Ⅲ 如何使用java編寫一個聊天小程序,要求使用圖形用戶界面,求高手!非常感謝!
首先,最快的法子是到網上下一個模板,照著學就是;
如果你要手動從零開始的話,那首先學習一項java如何用TCP或是UDP通信,放心,這不會很難,因為都是封裝好的類;其次,就是學習一下java的圖形界面設計,這個也不會很難因為eclipse有圖形化編輯的插件,你幾乎不用編碼,當然這是指java的自帶UI,那個長的實在不敢恭維;
然後呢?然後就好了,你就可以開始寫了。
Ⅳ 用JAVA如何實現圖形界面的聊天室寫出源代碼
給你說一下原理,自己操作。圖形界面需要用swing構造。客戶端和伺服器用socket傳遞消息。一個客戶端設置一個線程。
客戶端之間的通信需要伺服器使用Map鍵值數據對來管理,一個鍵值就是一個用戶(用戶的線程編號),對應的數據就是要發送的消息。
Ⅳ 如何做QQ聊天界面用Java swing做,下面的界面怎麼實現,我想的是給JMenu里加圖片吧文字掉但是不行,求教
jide 開放源碼項目 jide common layer里有個swing組件 JideSplitButton 就是提供這個功能的
Ⅵ java 聊天界面 空布局,加滾動條panel.add(scroll);scroll.setBounds( , , , );怎麼填
scroll.setBounds( , , , );
參數分別為 x,y,width,height
width和height可能順序不對
分別的意思就是
x: 相對於容器最左邊的距離,
y:相對於容器最上面的距離,
width:水平長度
height:垂直高度
然後我估計你就知道怎麼寫了
不過值得一提的是一般來說只有布局方式為null的時候setBounds才有用。
Ⅶ 求java網路聊天室(B/S模式)程序代碼
共四個java文件,源代碼如下:
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
public class ChatArea extends Panel implements ActionListener,Runnable
{
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread threadMessage=null;
TextArea 談話顯示區,私聊顯示區=null;
TextField 送出信息=null;
Button 確定,刷新談話區,刷新私聊區;
Label 提示條=null;
String name=null;
Hashtable listTable;
List listComponent=null;
Choice privateChatList;
int width,height;
public ChatArea(String name,Hashtable listTable,int width,int height)
{
setLayout(null);
setBackground(Color.orange);
this.width=width;
this.height=height;
setSize(width,height);
this.listTable=listTable;
this.name=name;
threadMessage=new Thread(this);
談話顯示區=new TextArea(10,10);
私聊顯示區=new TextArea(10,10);
確定=new Button("送出信息到:");
刷新談話區=new Button("刷新談話區");
刷新私聊區=new Button("刷新私聊區");
提示條=new Label("雙擊聊天者可私聊",Label.CENTER);
送出信息=new TextField(28);
確定.addActionListener(this);
送出信息.addActionListener(this);
刷新談話區.addActionListener(this);
刷新私聊區.addActionListener(this);
listComponent=new List();
listComponent.addActionListener(this);
privateChatList=new Choice();
privateChatList.add("大家(*)");
privateChatList.select(0);
add(談話顯示區);
談話顯示區.setBounds(10,10,(width-120)/2,(height-120));
add(私聊顯示區);
私聊顯示區.setBounds(10+(width-120)/2,10,(width-120)/2,(height-120));
add(listComponent);
listComponent.setBounds(10+(width-120),10,100,(height-160));
add(提示條);
提示條.setBounds(10+(width-120),10+(height-160),110,40);
Panel pSouth=new Panel();
pSouth.add(送出信息);
pSouth.add(確定);
pSouth.add(privateChatList);
pSouth.add(刷新談話區);
pSouth.add(刷新私聊區);
add(pSouth);
pSouth.setBounds(10,20+(height-120),width-20,60);
}
public void setName(String s)
{
name=s;
}
public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out)
{
this.socket=socket;
this.in=in;
this.out=out;
try
{
threadMessage.start();
}
catch(Exception e)
{
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==確定||e.getSource()==送出信息)
{
String message="";
String people=privateChatList.getSelectedItem();
people=people.substring(0,people.indexOf("("));
message=送出信息.getText();
if(message.length()>0)
{
try {
if(people.equals("大家"))
{
out.writeUTF("公共聊天內容:"+name+"說:"+message);
}
else
{
out.writeUTF("私人聊天內容:"+name+"悄悄地說:"+message+"#"+people);
}
}
catch(IOException event)
{
}
}
}
else if(e.getSource()==listComponent)
{
privateChatList.insert(listComponent.getSelectedItem(),0);
privateChatList.repaint();
}
else if(e.getSource()==刷新談話區)
{
談話顯示區.setText(null);
}
else if(e.getSource()==刷新私聊區)
{
私聊顯示區.setText(null);
}
}
public void run()
{
while(true)
{
String s=null;
try
{
s=in.readUTF();
if(s.startsWith("聊天內容:"))
{
String content=s.substring(s.indexOf(":")+1);
談話顯示區.append("\n"+content);
}
if(s.startsWith("私人聊天內容:"))
{
String content=s.substring(s.indexOf(":")+1);
私聊顯示區.append("\n"+content);
}
else if(s.startsWith("聊天者:"))
{
String people=s.substring(s.indexOf(":")+1,s.indexOf("性別"));
String sex=s.substring(s.indexOf("性別")+2);
listTable.put(people,people+"("+sex+")");
listComponent.add((String)listTable.get(people));
listComponent.repaint();
}
else if(s.startsWith("用戶離線:"))
{
String awayPeopleName=s.substring(s.indexOf(":")+1);
listComponent.remove((String)listTable.get(awayPeopleName));
listComponent.repaint();
談話顯示區.append("\n"+(String)listTable.get(awayPeopleName)+"離線");
listTable.remove(awayPeopleName);
}
Thread.sleep(5);
}
catch(IOException e)
{
listComponent.removeAll();
listComponent.repaint();
listTable.clear();
談話顯示區.setText("和伺服器的連接已中斷\n必須刷新瀏覽器才能再次聊天");
break;
}
catch(InterruptedException e)
{
}
}
}
}
ChatServer.java
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer
{
public static void main(String args[])
{
ServerSocket server=null;
Socket you=null;
Hashtable peopleList;
peopleList=new Hashtable();
while(true)
{
try
{
server=new ServerSocket(6666);
}
catch(IOException e1)
{
System.out.println("正在監聽");
}
try {
you=server.accept();
InetAddress address=you.getInetAddress();
System.out.println("用戶的IP:"+address);
}
catch (IOException e)
{
}
if(you!=null)
{
Server_thread peopleThread=new Server_thread(you,peopleList);
peopleThread.start();
}
else {
continue;
}
}
}
}
class Server_thread extends Thread
{
String name=null,sex=null;
Socket socket=null;
File file=null;
DataOutputStream out=null;
DataInputStream in=null;
Hashtable peopleList=null;
Server_thread(Socket t,Hashtable list)
{
peopleList=list;
socket=t;
try {
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException e)
{
}
}
public void run()
{
while(true)
{ String s=null;
try
{
s=in.readUTF();
if(s.startsWith("姓名:"))
{
name=s.substring(s.indexOf(":")+1,s.indexOf("性別"));
sex=s.substring(s.lastIndexOf(":")+1);
boolean boo=peopleList.containsKey(name);
if(boo==false)
{
peopleList.put(name,this);
out.writeUTF("可以聊天:");
Enumeration enum=peopleList.elements();
while(enum.hasMoreElements())
{
Server_thread th=(Server_thread)enum.nextElement();
th.out.writeUTF("聊天者:"+name+"性別"+sex);
if(th!=this)
{
out.writeUTF("聊天者:"+th.name+"性別"+th.sex);
}
}
}
else
{
out.writeUTF("不可以聊天:");
}
}
else if(s.startsWith("公共聊天內容:"))
{
String message=s.substring(s.indexOf(":")+1);
Enumeration enum=peopleList.elements();
while(enum.hasMoreElements())
{
((Server_thread)enum.nextElement()).out.writeUTF("聊天內容:"+message);
}
}
else if(s.startsWith("用戶離開:"))
{
Enumeration enum=peopleList.elements();
while(enum.hasMoreElements())
{ try
{
Server_thread th=(Server_thread)enum.nextElement();
if(th!=this&&th.isAlive())
{
th.out.writeUTF("用戶離線:"+name);
}
}
catch(IOException eee)
{
}
}
peopleList.remove(name);
socket.close();
System.out.println(name+"用戶離開了");
break;
}
else if(s.startsWith("私人聊天內容:"))
{
String 悄悄話=s.substring(s.indexOf(":")+1,s.indexOf("#"));
String toPeople=s.substring(s.indexOf("#")+1);
Server_thread toThread=(Server_thread)peopleList.get(toPeople);
if(toThread!=null)
{
toThread.out.writeUTF("私人聊天內容:"+悄悄話);
}
else
{
out.writeUTF("私人聊天內容:"+toPeople+"已經離線");
}
}
}
catch(IOException ee)
{
Enumeration enum=peopleList.elements();
while(enum.hasMoreElements())
{ try
{
Server_thread th=(Server_thread)enum.nextElement();
if(th!=this&&th.isAlive())
{
th.out.writeUTF("用戶離線:"+name);
}
}
catch(IOException eee)
{
}
}
peopleList.remove(name);
try
{
socket.close();
}
catch(IOException eee)
{
}
System.out.println(name+"用戶離開了");
break;
}
}
}
}
import java.awt.*;
import java.io.*;
import java.net.*;
import java.applet.*;
import java.util.Hashtable;
ClientChat.java
public class ClientChat extends Applet implements Runnable
{
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
InputNameTextField 用戶提交昵稱界面=null;
ChatArea 用戶聊天界面=null;
Hashtable listTable;
Label 提示條;
Panel north, center;
Thread thread;
public void init()
{
int width=getSize().width;
int height=getSize().height;
listTable=new Hashtable();
setLayout(new BorderLayout());
用戶提交昵稱界面=new InputNameTextField(listTable);
int h=用戶提交昵稱界面.getSize().height;
用戶聊天界面=new ChatArea("",listTable,width,height-(h+5));
用戶聊天界面.setVisible(false);
提示條=new Label("正在連接到伺服器,請稍等...",Label.CENTER);
提示條.setForeground(Color.red);
north=new Panel(new FlowLayout(FlowLayout.LEFT));
center=new Panel();
north.add(用戶提交昵稱界面);
north.add(提示條);
center.add(用戶聊天界面);
add(north,BorderLayout.NORTH);
add(center,BorderLayout.CENTER);
validate();
}
public void start()
{
if(socket!=null&&in!=null&&out!=null)
{ try
{
socket.close();
in.close();
out.close();
用戶聊天界面.setVisible(false);
}
catch(Exception ee)
{
}
}
try
{
socket = new Socket(this.getCodeBase().getHost(), 6666);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException ee)
{
提示條.setText("連接失敗");
}
if(socket!=null)
{
InetAddress address=socket.getInetAddress();
提示條.setText("連接:"+address+"成功");
用戶提交昵稱界面.setSocketConnection(socket,in,out);
north.validate();
}
if(thread==null)
{
thread=new Thread(this);
thread.start();
}
}
public void stop()
{
try
{
socket.close();
thread=null;
}
catch(IOException e)
{
this.showStatus(e.toString());
}
}
public void run()
{
while(thread!=null)
{
if(用戶提交昵稱界面.get能否聊天()==true)
{
用戶聊天界面.setVisible(true);
用戶聊天界面.setName(用戶提交昵稱界面.getName());
用戶聊天界面.setSocketConnection(socket,in,out);
提示條.setText("祝聊天愉快!");
center.validate();
break;
}
try
{
Thread.sleep(100);
}
catch(Exception e)
{
}
}
}
}
InputNameTextField。java
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
public class InputNameTextField extends Panel implements ActionListener,Runnable
{
TextField nameFile=null;
String name=null;
Checkbox male=null,female=null;
CheckboxGroup group=null;
Button 進入聊天室=null,退出聊天室=null;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread=null;
boolean 能否聊天=false;
Hashtable listTable;
public InputNameTextField(Hashtable listTable)
{
this.listTable=listTable;
nameFile=new TextField(10);
group=new CheckboxGroup();
male=new Checkbox("男",true,group);
female=new Checkbox("女",false,group);
進入聊天室=new Button("進入");
退出聊天室=new Button("退出");
進入聊天室.addActionListener(this);
退出聊天室.addActionListener(this);
thread=new Thread(this);
add(new Label("昵稱:"));
add(nameFile);
add(male);
add(female);
add(進入聊天室);
add(退出聊天室);
退出聊天室.setEnabled(false);
}
public void set能否聊天(boolean b)
{
能否聊天=b;
}
public boolean get能否聊天()
{
return 能否聊天;
}
public String getName()
{
return name;
}
public void setName(String s)
{
name=s;
}
public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out)
{
this.socket=socket;
this.in=in;
this.out=out;
try{
thread.start();
}
catch(Exception e)
{
nameFile.setText(""+e);
}
}
public Socket getSocket()
{
return socket;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==進入聊天室)
{
退出聊天室.setEnabled(true);
if(能否聊天==true)
{
nameFile.setText("您正在聊天:"+name);
}
else
{
this.setName(nameFile.getText());
String sex=group.getSelectedCheckbox().getLabel();
if(socket!=null&&name!=null)
{
try{
out.writeUTF("姓名:"+name+"性別:"+sex);
}
catch(IOException ee)
{
nameFile.setText("沒有連通伺服器"+ee);
}
}
}
}
if(e.getSource()==退出聊天室)
{
try
{
out.writeUTF("用戶離開:");
}
catch(IOException ee)
{
}
}
}
public void run()
{
String message=null;
while(true)
{
if(in!=null)
{
try
{
message=in.readUTF();
}
catch(IOException e)
{
nameFile.setText("和伺服器斷開"+e);
}
}
if(message.startsWith("可以聊天:"))
{
能否聊天=true;
break;
}
else if(message.startsWith("聊天者:"))
{
String people=message.substring(message.indexOf(":")+1);
listTable.put(people,people);
}
else if(message.startsWith("不可以聊天:"))
{
能否聊天=false;
nameFile.setText("該昵稱已被佔用");
}
}
}
}
Ⅷ 用JAVA開發一個在線聊天系統需要哪些軟體
基礎社交,社交基本的需求就是可以發語音、發圖片、發文字。
Ⅸ 那個用java實現的聊天窗口程序誰有
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.xml.bind.JAXB;
public class ClientChat extends JFrame {
private String userName;//登陸成功的用戶名
private JTextArea jta_recive=new JTextArea(15,25);//顯示接受到消息的組件
private JComboBox jcb_users=new JComboBox();//在線用戶的下接框
private File myfile;
public static void main(String [] args){
new ClientChat("林海方");//生成一個對象
}
public ClientChat(String userName){
this.userName=userName;//用戶名
add(jta_recive);
add(jcb_users);
showFrame();
initial();
}
public void showFrame(){
this.setTitle("netjava 歡迎"+this.userName+"的使用!!!");
java.awt.FlowLayout f1=new java.awt.FlowLayout(0);
this.setLayout(f1);
this.setSize(300, 500);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
private void initial(){
JLabel la_name = new JLabel("接收到的消息");
JLabel la_users = new JLabel("發送給:");
final JTextField jtf_send = new JTextField(25);//發送輸出框
javax.swing.JButton bu_send = new javax.swing.JButton("send");
javax.swing.JButton bu_history = new javax.swing.JButton("聊天記錄");
jcb_users.addItem("張三");
jcb_users.addItem("李四");
jcb_users.addItem("王保長");
add(la_name);
add(jta_recive);
add(jcb_users);
add(jtf_send);
add(la_users);
add(jcb_users);
add(bu_send);
add(bu_history);
ActionListener sendListener = new ActionListener(){
public void actionPerformed(ActionEvent e){
byte[] chi;
String reciver = (String) jcb_users.getSelectedItem();
reciver = reciver.trim();//去除空格
String content = jtf_send.getText();//
jta_recive.append(userName+"對"+reciver+"說:"+content+"\r\n");//顯示到桌面
jtf_send.setText("");
myfile = new File ("G:\\Java實驗7.txt");
FileOutputStream fout = null;
try {
fout = new FileOutputStream(myfile,true);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
chi=content.getBytes();
try {
fout.write(chi);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
};
ActionListener historyListener = new ActionListener(){ /**/
// String content_history;
public void actionPerformed(ActionEvent e){
byte [] b = new byte[1024];
FileInputStream fin = null;
try {
fin = new FileInputStream(myfile);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String message = "sefsdfg";
try {
fin.read(b);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
message =new String(b);
jta_recive.append("聊天記錄:"+message);
}
};
bu_send.addActionListener(sendListener);
jtf_send.addActionListener(sendListener);
bu_history.addActionListener(historyListener);
jtf_send.setText("");
}
}
Ⅹ qq聊天界面怎麼寫啊,用java語言寫
愛應用團隊為你解答
網路資料
package cn.myself.myproject.FrameProject;import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;import cn.myself.myproject.employeepj.view.common.CenterWindow;
/**
* 程序功能:QQ登陸面板
* 學習內容:GridBagLayout布局方式的學習
* 以GridBagLayout方式布局的容器,其容器中的每個組件必須由一個GridBagConstrains類的實例對象進行大小,位置等約束。
* @author huliu 2009-06-26
* 難題:a.帳號後面是什麼框?
* b.圖片的相對路徑怎麼設置?
*/
public class QQRegistBoard extends JFrame{
JPanel p1;
GridBagLayout gb1;
GridBagConstraints gbc1;
JButton btn1,btn2;
JLabel label0,label1,label2,label3,label4,label5;
JTextField text1,text2;
JComboBox box1,box2;
JCheckBox check1,check2;
JList list1;
/**
* 構造方法
*/
public QQRegistBoard(){
super("2009正式版(huliu)");
p1=new JPanel();
gb1=new GridBagLayout();
gbc1=new GridBagConstraints();
p1.setLayout(gb1);//GridBagLayout布局。網袋布局
getContentPane().add(p1); //取得當前容器對象
this.setSize(350,250);
CenterWindow.centerW(this);
Icon icon1 = new ImageIcon("./QQ2.jpg");
// Icon icon1 = new ImageIcon("./QQ.jpg"); //載入圖片,當前目錄下的QQ.jpg
// Icon icon1 = new ImageIcon("src/cn/mysef/images/QQ1.jpg");
label0=new JLabel(icon1);
label1=new JLabel("帳號:");
label2=new JLabel("注冊新帳號");
label3=new JLabel("密碼:");
label4=new JLabel("取回密碼");
label5=new JLabel("狀態:");
text1=new JTextField(10);
text2=new JTextField(10);
String[] str1={"313558851","313857401","690442763"};
box1=new JComboBox(str1);
box1.setEditable(true);//設置ComboBox欄位值是否為可編輯
box2=new JComboBox();
check1=new JCheckBox("記住密碼",true);
check2=new JCheckBox("自動登錄");
btn1=new JButton("設置");
btn2=new JButton("登錄");
p1.add(label0,GBC(0,0,3,1,new Insets(5,2,2,4)));//圖片
p1.add(label1,GBC(1,0,1,1,new Insets(4,2,2,4)));
p1.add(box1, GBC(1,1,1,1,new Insets(4,2,2,0)));
//p1.add(text2,GBC(1,1,1,1));
p1.add(label2,GBC(1,2,1,1,new Insets(4,2,2,3)));
p1.add(label3,GBC(2,0,1,1,new Insets(4,2,2,3)));
p1.add(text1, GBC(2,1,1,1,new Insets(5,2,2,3)));
p1.add(label4,GBC(2,2,1,1,new Insets(4,2,2,3)));
p1.add(label5,GBC(3,0,1,1,new Insets(4,2,2,3)));
p1.add(check1,GBC(3,1,1,1,new Insets(4,2,2,3)));
p1.add(check2,GBC(3,2,1,1,new Insets(4,2,2,3)));
p1.add(btn1 ,GBC(4,0,1,1,new Insets(4,2,2,3)));
p1.add(btn2 ,GBC(4,2,1,1,new Insets(4,2,2,3)));
}
/**
* GBC方法:功能是設計以GridBagLayout方式布局的容器(如Panel容器對象)內的組件的位置,大小等約束的。
* @param gridy
* @param gridx
* @param gridwidth
* @param gridheight
* @return GridBagStraints實對象
* Insets(int top, int left, int bottom, int right),與其它組件之間距離(上,左,下,右)
*/
public GridBagConstraints GBC(int gridy,int gridx,int gridwidth,int gridheight,Insets insets){
GridBagConstraints gbc1=new GridBagConstraints();
gbc1.gridx=gridx; //列
gbc1.gridy=gridy; //行
gbc1.gridwidth=gridwidth;//寬度
gbc1.gridheight=gridheight; //高度
//insets=new Insets(1,1,1,1);
gbc1.insets=insets;
return gbc1;
}
public static void main(String[] args){
new QQRegistBoard().setVisible(true);
}}
望採納