本文目录
- eclipse编写一个非常简单的聊天界面
- 急需一个java编程实现的简单聊天窗口代码
- java编写的聊天窗口代码怎么布局,怎么任意改变Panel的大小
- 求助用java编写的简单的一对一聊天程序类似qq的需要界面谢谢
- 求用Java编写的聊天室界面
- 在java里编写聊天软件的图像界面代码怎么写
- 有谁知道怎样用java写一个简易的聊天对话框我这方面学的不好,马上要交作业了,现在急需大家的帮助~~
- 如何使用java编写一个聊天小程序,要求使用图形用户界面,求高手!非常感谢!
eclipse编写一个非常简单的聊天界面
//服务器端代码
import java.awt.FlowLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class QLServer extends JFrame{
public JTextArea jtextarea = null;
public void lanuchFrame(String str){
this.setName(str);
init();
}
private void init() {
setLayout(new FlowLayout());
jtextarea =new JTextArea(20, 17);
jtextarea.setLineWrap(true);
jtextarea.setEditable(false);
this.getContentPane().add(new JScrollPane(jtextarea));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,400);
setLocationRelativeTo(null);
setResizable(false);
}
ServerSocket server = null;
Collection cClients = new ArrayList《ClientConn》();//加个泛型
public void startServer() throws IOException{
while(true){
Socket s = server.accept();
cClients.add(new ClientConn(s));
jtextarea.append(“new client login“ + s.getInetAddress() + “:“ + s.getPort()+“\n“);
}
}
public QLServer(int port,String str) throws IOException{
server = new ServerSocket(port);
lanuchFrame(str);
}
class ClientConn implements Runnable
{
Socket s = null;
public ClientConn(Socket s)
{
this.s = s;
(new Thread(this)).start();
}
public void send(String str) throws IOException
{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
public void dispose()//客户端下线
{
try {
if (s != null) s.close();
cClients.remove(this);
jtextarea.append(“A client out! \n“);
jtextarea.append(“client count: “ + cClients.size() + “\n\n“);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void run()
{
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while(str != null && str.length() !=0)
{
System.out.println(str);
for(Iterator it = cClients.iterator(); it.hasNext(); )
{
ClientConn cc = (ClientConn)it.next();
if(this != cc)
{
cc.send(str+“ “+s.getInetAddress().getHostName());
}
}
str = dis.readUTF();//少了这句话会无限输出
//send(str);
}
this.dispose();
}
catch (Exception e)
{
this.dispose();
}
}
}
public static void main(String args) {
try {
QLServer qlserver = new QLServer(8888,“QLServer“);
qlserver.startServer();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//客户端代码
public class QLClient extends JFrame implements ActionListener{
public JTextArea jtextarea1 = null;
public JTextArea jtextarea2 = null;
public JButton button = null;
Socket s =null;
public void launchFrame(String str){
this.setName(str);
init();
}
public QLClient(String str) throws IOException{
launchFrame(str);
s = new Socket(“127.0.0.1“,8888);//哪台电脑做服务器,IP地址改成那台机子的IP
(new Thread(new ServeConn())).start();
}
private void init() {
setLayout(new FlowLayout());
jtextarea1 =new JTextArea(17, 16);
jtextarea2 =new JTextArea(4, 16);
jtextarea1.setLineWrap(true);
jtextarea1.setEditable(false);
jtextarea2.setLineWrap(true);
button = new JButton(“发送“);
button.addActionListener(this);//绑定button事件
this.getContentPane().add(new JScrollPane(jtextarea1));
this.getContentPane().add(new JScrollPane(jtextarea2));
add(button);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,470);
setLocationRelativeTo(null);
setResizable(false);
}
public void send(String str) throws IOException{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button){
String sendStr = jtextarea2.getText();
if(sendStr.trim().length()==0){
return;
}
try {
this.send(sendStr);
jtextarea2.setText(““);
InetAddress a;
a = InetAddress.getLocalHost();
String hostname = a.getHostName();
jtextarea1.append(sendStr+“(“+hostname+“)“+“\n“);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class ServeConn implements Runnable{
public void run() {
if(s == null) return;
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while (str != null && str.length() != 0)
{
//System.out.println(str);
QLClient.this.jtextarea1.append(str + “\n“);//内部类用外类中的变量或方法加外类名
str = dis.readUTF();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
//main主函数入口
public static void main(String args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
QLClient qlclient = new QLClient(“QLClient“);
String str = br.readLine();
while(str!=null&&str.length()!=0){
qlclient.send(str);
str = br.readLine();//防止死循环
}
qlclient.s.close();
}
}
急需一个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); //滚动窗格
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;
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;
int length=bis.read(b);
String message=new String(b,0,length);
ta.append(“AA:“+message+“\n“);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
java编写的聊天窗口代码怎么布局,怎么任意改变Panel的大小
用netbeans编程平台,免费的,跟eclipse一样,用java语言,在新建类时选用java应用,会有一个视图界面,一个代码界面,在视图界面可以选择各种空间,不只是Panel,而且各类控件可以随意拖动,改变大小,因为是可视化界面,布局轻松加easy,切换到代码界面,你会发现它已经将布局部分的代码自动写好了,比你用eclipse利用简单的布局管理器省事也美观多了,当人,给力的人用eclipse也可以达到同样的效果,不过用不可视的界面做布局,个人觉得很累,效率很低
求助用java编写的简单的一对一聊天程序类似qq的需要界面谢谢
我原来有一个,但是现在没有了,大概需要用到的是socket 还有桌面级jar包 SWT ,你就搜这样的关键字就可以了。我又找到了,给你参考一下
求用Java编写的聊天室界面
jsp的
《%@ page language=“java“ contentType=“text/html; charset=gb2312“
pageEncoding=“gb2312“%》
《!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN“》
《html》
《head》
《title》欢乐聊天室《/title》
《/head》
《body》
《%--首先取出用户提交的代号名称,保存在变量chatid中--%》
《%
String chatid = new String();
chatid = request.getParameter(“inputid“);
%》
《%--使用变量flag来标记用户输入是否合法,如果合法,则flag为true --%》
《%
boolean flag;
flag = true;
if(chatid == null){
chatid = ““;
}
if(chatid.length() == 0){
flag = false;
}
%》
《%--比较用户所输入的id和目前聊天室中存在的所有id --%》
《%
for(int i=1; i《=6 && flag; i++){
String itemp = new String();
itemp = itemp.valueOf(i);
int num;
String numtemp = new String();
String temp = new String();
temp = “room“ + itemp + “usernum“;
numtemp = (String)application.getAttribute(temp);
if(numtemp == null){
numtemp = “0“;
application.setAttribute(temp ,numtemp);
}
num = Integer.parseInt(numtemp);
for(int j=1; j《=num && flag; j++){
String jtemp = new String();
jtemp = jtemp.valueOf(j);
%》
《%--从application对象中取出第i个聊天室中第j个用户的id,temp变量保存的是application对象用于保存第i个聊天室中第j个用户的id相应的变量名 --%》
《%
temp = “room“ + itemp + “user“ + jtemp;
String usertemp = new String();
usertemp = (String)application.getAttribute(temp);
if(usertemp.equalsIgnoreCase(chatid)){
flag = false;
}
}
}
int nnn = new int;
if(flag){
String temproom = new String();
temproom = (String)session.getValue(“chatroom“);
if(temproom == null){
session.putValue(“chatid“,chatid);
}
for(int i=1; i《=6; i++) {
String itemp = new String();
itemp = itemp.valueOf(i);
int num;
String numtemp = new String();
String temp = new String();
temp = “room“ + itemp + “usernum“;
numtemp = (String)application.getAttribute(temp);
if(numtemp == null){
numtemp = “0“;
}
num = Integer.parseInt(numtemp);
nnn[i-1] = num;
}
}
%》
《p align=“center“》《b》《font face=“隶书“ size=“6“ color=“#FF00FF“》欢乐聊天室《/font》《/b》《/p》
《%
if(flag){
%》
《p align=“center“》《font color=“red“》《%=chatid %》《/font》您好,请选择感兴趣的聊天室!《/p》
《center》《table border=“1“ width=“370“》
《tr》
《td width=“50%“》《a href=“JSPchat.jsp?chatroom=1“》今天我们相识(《%=nnn%》)《/a》《/td》
《td width=“50%“》《a href=“JSPchat.jsp?chatroom=2“》校园的那条小路(《%=nnn%》)《/a》《/td》
《/tr》
《tr》
《td width=“50%“》《a href=“JSPchat.jsp?chatroom=3“》职场淘金(《%=nnn%》)《/a》《/td》
《td width=“50%“》《a href=“JSPchat.jsp?chatroom=4“》网络技术交流(《%=nnn%》)《/a》《/td》
《/tr》
《tr》
《td width=“50%“》《a href=“JSPchat.jsp?chatroom=5“》世界体育大看台(《%=nnn%》)《/a》《/td》
《td width=“50%“》《a href=“JSPchat.jsp?chatroom=6“》新闻背后的故事(《%=nnn%》)《/a》《/td》
《/tr》
《/table》《center》
《%
}else {
%》
《center》《p》id不能为空,或者此id已经被使用,请重新选择!《/p》《center》
《p》《center》《a href=“login.html“》返回《/a》《center》《/p》
《%
}
%》
《/body》
《/html》
在java里编写聊天软件的图像界面代码怎么写
如果你问的是怎么写界面的代码,那么 如果不需要太复杂 这样就可以:
JFrame frame = new JFrame(name);//窗口
JTextField jtf; // 文本条
JTextArea jta; //文本域。
frame.setSize(400, 300); //大小
jta = new JTextArea(); //文本域
jta.setEditable(false); //不可编辑
jtf = new JTextField();//文件条
如果你问的是整个程序的代码 那就先把分加到100吧 而且就算100估计也没人会来给你写
因为很麻烦的
有谁知道怎样用java写一个简易的聊天对话框我这方面学的不好,马上要交作业了,现在急需大家的帮助~~
...又不给分...这么急还不给分—。算了,看你很急就把代码给你吧!声明:能运行的……
服务端:
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
boolean started = false;
ServerSocket ss = null;
List《Client》 clients = new ArrayList《Client》();
public static void main(String args) {
new ChatServer().start();
}
public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println(“端口使用中....“);
System.out.println(“请关掉相关程序并重新运行服务器!“);
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println(“a client connected!“);
new Thread(c).start();
clients.add(c);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
System.out.println(str);
for(int i=0; i《clients.size(); i++) {
Client c = clients.get(i);
c.send(str);
}
}
} catch (EOFException e) {
System.out.println(“Client closed!“);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
//s = null;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
客户端,开两个就能聊了……
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
Thread tRecv = new Thread(new RecvThread());
public static void main(String args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(400, 300);
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();
tRecv.start();
}
public void connect() {
try {
s = new Socket(“127.0.0.1“, 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println(“connected!“);
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
tfTxt.setText(““);
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable {
public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
taContent.setText(taContent.getText() + str + ’\n’);
}
} catch (SocketException e) {
System.out.println(“bye!“);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
如何使用java编写一个聊天小程序,要求使用图形用户界面,求高手!非常感谢!
首先,最快的法子是到网上下一个模板,照着学就是;
如果你要手动从零开始的话,那首先学习一项java如何用TCP或是UDP通信,放心,这不会很难,因为都是封装好的类;其次,就是学习一下java的图形界面设计,这个也不会很难因为eclipse有图形化编辑的插件,你几乎不用编码,当然这是指java的自带UI,那个长的实在不敢恭维;
然后呢?然后就好了,你就可以开始写了。