Ⅰ 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);
}}
望采纳