「java模仿qq」java模仿多人群聊
今天给各位分享java模仿qq的知识,其中也会对java模仿多人群聊进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、请问用java程序模拟qq登录界面的代码怎么写啊?
- 2、java 如何模仿QQ聊天框那样可以显示文字与图片啊?
- 3、写一个java程序,类似QQ聊天,但功能简单得多,要求己方为服务器端,提供端口让好友来连接,连接上
- 4、求一个用java编写的套接字实现类似于QQ对话的程序,急用
- 5、用java怎么实现QQ登录界面?
- 6、哪位大侠用java做个类似QQ的程序
请问用java程序模拟qq登录界面的代码怎么写啊?
太简单了!你看看! package dyno.swing.beans.qq; import javax.swing.*; import javax.swing.event.MouseInputListener; import org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel; /*import org.jvnet.substance.skin.SubstanceModerateLookAndFeel; import org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel;*/ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.io.IOException; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class QQLogin extends JFrame implements MouseInputListener,ActionListener{ JLabel guanggao,beijing,wenzi,shezhi,zhanghaowb,qq1,dengluzhuangtai; // JTextField zhanghao; JPopupMenu haoma; JComboBox zhanghao; JPasswordField mima; JCheckBox jizhumima,zidongdenglu; JButton denglu,chashamuma; JProgressBar jpb; SimThread activity; Timer activityMonitor; String name,qq; Socket s; public QQLogin() { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (ClassNotFoundException e1) { // TODO 自动生成 catch 块 e1.printStackTrace(); } catch (InstantiationException e1) { // TODO 自动生成 catch 块 e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO 自动生成 catch 块 e1.printStackTrace(); } catch (UnsupportedLookAndFeelException e1) { // TODO 自动生成 catch 块 e1.printStackTrace(); } chashamuma = new JButton("查杀木马"); chashamuma.setBounds(240, 155,85, 20); this.add(chashamuma); jpb = new JProgressBar(); jpb.setStringPainted(true); jpb.setBounds(100, 240, 200, 15); this.add(jpb); chashamuma.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ jpb.setMaximum(1000);//设置进度栏的最大值 activity=new SimThread(1000); activity.start();//启动线程 activityMonitor.start();//启动定时器 chashamuma.setEnabled(false);//禁止按钮 } }); activityMonitor=new Timer(100,new ActionListener(){//每0.5秒执行一次 public void actionPerformed(ActionEvent e){//以下动作将在事件调度线程中运行,十分安全 int current=activity.getCurrent();//得到线程的当前进度 jpb.setValue(current);//更新进度栏的值 if(current==activity.getTarget()){//如果到达目标值 activityMonitor.stop();//终止定时器 chashamuma.setEnabled(true);//激活按钮 } } }); dengluzhuangtai = new JLabel(new ImageIcon("zaixianzhuangtai.jpg")); dengluzhuangtai.setBounds(75, 145, 35, 30); this.add(dengluzhuangtai); dengluzhuangtai.addMouseListener(this); denglu = new JButton("登录"); denglu.setBounds(140, 155, 80, 20); this.add(denglu); this.setAlwaysOnTop(true); zidongdenglu = new JCheckBox("自动登录"); zidongdenglu.setBounds(200, 190, 100, 30); this.add(zidongdenglu); jizhumima = new JCheckBox("记住密码"); jizhumima.setBounds(100, 190, 100, 30); // jizhumima.setBackground(new Color(228, 244, 255)); this.add(jizhumima); haoma = new JPopupMenu(); /* zhanghao = new JTextField(20); zhanghao.setBounds(120, 78, 135, 20); zhanghao.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.WHITE)); zhanghao.setFont(new Font("宋体",Font.PLAIN,13)); this.add(zhanghao);*/ // zhanghaowb = new JLabel(new ImageIcon("2.png")); // zhanghaowb.setBounds(90, 73, 194, 31); // jiantou = new JLabel(new ImageIcon("baijiantou.png")); // jiantou.setBounds(256, 78, 23, 21); // jiantou.addMouseListener(this); // this.add(jiantou); // this.add(zhanghaowb); chashamuma.addActionListener(this); mima = new JPasswordField(); mima.setEchoChar('*'); mima.setFont(new Font("宋体",Font.PLAIN,13)); mima.setBounds(100, 113, 150, 20); this.add(mima); zhanghao = new JComboBox(); zhanghao.setEditable(true); zhanghao.setBounds(100, 78, 150, 20); zhanghao.setFont(new Font("宋体",Font.PLAIN,13)); this.add(zhanghao); guanggao = new JLabel(new ImageIcon("guanggao.gif")); guanggao.setBounds(0, 0, 334, 64); beijing = new JLabel(new ImageIcon("beijing.jpg")); beijing.setBounds(0, 64, 334, 154); wenzi = new JLabel(new ImageIcon("wenzi.jpg")); wenzi.setBounds(30, 75, 50, 100); denglu.addActionListener(this); // zhanghaowb.addMouseListener(this); // zhanghao.addMouseListener(this); this.add(wenzi); this.add(beijing); this.setLayout(null); this.add(guanggao); this.setVisible(true); this.setDefaultCloseOperation(3); this.setSize(340, 250); this.setLocationRelativeTo(null); } public static void main(String[] args) { /*JFrame.setDefaultLookAndFeelDecorated(true); try { UIManager.setLookAndFeel(new SubstanceOfficeBlue2007LookAndFeel()) ; UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel"); } catch (Exception e) { System.out.println("Substance Raven Graphite failed to initialize"); } SwingUtilities.invokeLater(new Runnable() { public void run() { QQLogin w = new QQLogin(); w.setVisible(true); } });*/ new QQLogin(); } public void mouseClicked(MouseEvent e) { // TODO 自动生成方法存根 } public void mouseEntered(MouseEvent e) { if(e.getSource() == dengluzhuangtai) { dengluzhuangtai.setIcon(new ImageIcon("zaixianzhuangtaidian.jpg")); } } public void mouseExited(MouseEvent e) { if(e.getSource() == dengluzhuangtai) { dengluzhuangtai.setIcon(new ImageIcon("zaixianzhuangtai.jpg")); } } public void mousePressed(MouseEvent e) { // TODO 自动生成方法存根 } public void mouseReleased(MouseEvent e) { // TODO 自动生成方法存根 } public void mouseDragged(MouseEvent e) { // TODO 自动生成方法存根 } public void mouseMoved(MouseEvent e) { // TODO 自动生成方法存根 } public class liaotianchuangkou { } class SimThread extends Thread{//线程类 private int current;//进度栏的当前值 private int target;//进度栏的最大值 public SimThread(int t){ current=0; target=t; } public int getTarget(){ return target; } public int getCurrent(){ return current; } public void run(){//线程体 try{ while (currenttarget !interrupted()){//如果进度栏的当前值小于目标值并且线程没有被中断 sleep(10); current++; if(current == 700) { sleep(3000); } else if(current == 730) { sleep(1000); } } }catch (InterruptedException e){} } } public void actionPerformed(ActionEvent e) { if(e.getSource() == chashamuma) { this.setBounds(300, 300, 340, 300); } else if(e.getSource() == denglu) { String zh = (String) zhanghao.getSelectedItem(); System.out.println(zhanghao.getSelectedItem()); // System.out.println(zhanghao.getItemAt(0)); char [] str = mima.getPassword(); String mima = String.valueOf(str);; System.out.println(mima); // Sql login = new Sql(); // if(login.login(zh,mima)) // { try { s = new Socket("127.0.0.1",8888); System.out.println(s); PrintWriter pw; Scanner sc; pw = new PrintWriter(s.getOutputStream(),true); sc = new Scanner(s.getInputStream()); String str2 = "login#289872400198724#"+zh+"#289872400198724#"+mima; System.out.println(str2); pw.println(str2); String str3 = sc.nextLine(); String yanzheng[] = str3.split("#"); System.out.println(str3); if(yanzheng[0].equals("true")) { System.out.println("登陆成功!"); name = yanzheng[1]; qq = yanzheng[2]; // this.setVisible(false); // Thread.sleep(5000); System.out.println("woao"+name); System.out.println("woai"+qq); Logined logined = new Logined(name,qq); this.setVisible(false); } else { JOptionPane.showMessageDialog(this, "用户名或密码错误!", "用户名或密码错误!", 0); } } catch (UnknownHostException e2) { // TODO 自动生成 catch 块 e2.printStackTrace(); } catch (IOException e2) { // TODO 自动生成 catch 块 e2.printStackTrace(); } /*try { login.rs = login.stat.executeQuery("select * from qquser where username='"+zh+"' and password = '"+mima+"'"); boolean flag = login.rs.next(); if(flag == true) { name = login.rs.getString("name"); qq = login.rs.getString("username"); } else { }*/ // } catch (SQLException e1) { // TODO 自动生成 catch 块 // e1.printStackTrace(); // } } else { JOptionPane.showMessageDialog(this, "用户名或密码错误", "输入错误", 0); } // this.setVisible(false); //new Logined(); } }
java 如何模仿QQ聊天框那样可以显示文字与图片啊?
1、swing的界面可以直接用netbeans画出来嘛。
2、可以把输出的聊天内容都放在一个StringBuffer里,每打出一句话,就把这句话追加在StringBuffer,然后把StringBuffer里的内容输出到Textarea中。
3、好友列表可以用JList
写一个java程序,类似QQ聊天,但功能简单得多,要求己方为服务器端,提供端口让好友来连接,连接上
本来就要先开服务器啊;
首先,开启服务器;
客户端连接服务器,一连上服务器,说明客户上线上;
通过客户端查看好友是否上线,如果上线,可以直接进行聊天,如果没上线,也可以给好友留言;
两个客户端进行聊天的原理:客户(A与B):A要发消息给B,首先,要把A的账号(发送者),A要发送的内容和B的账号(接收者),发送时间打包通过线程发给服务器;
服务器进行解包,查看B的账号是否上线,如果上线,再把这个包发给B,B进行解包,显示在界面上;B要发给A也是同理。。
你说的两个客户端直接聊天,其实相当于服务器与客户端的聊天而已,这样的聊天程序是不完整的。
希望能帮到你。
求一个用java编写的套接字实现类似于QQ对话的程序,急用
有两个类,服务器和客户端、
服务器类代码:
package chat;
import java.io.*;
import java.net.*;
import java.util.*;
/*服务器类*/
public class Server {
public ListSocket socketList = new ArrayListSocket();//存放所有连接的客户端的集合
public ServerSocket server;//服务器
public int portNum;//端口号
public Server(int portNum){
this.portNum = portNum;
}
public void innit(){
try {
server = new ServerSocket(portNum);
System.out.println("服务器开启成功!");
int socketNum = 0;
while(true){
Socket socket = server.accept();//被动等待客户端的连接
socketNum++;
System.out.println("第"+socketNum+"个客户端连接成功!!");
socketList.add(socket);//连接的客户端存放到集合里面
new RWThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class RWThread extends Thread{//接收和发送消息的线程
public Socket socket;
public RWThread(Socket socket){
this.socket = socket;
}
public void run() {
super.run();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String message = br.readLine();
System.out.println(message);//接收客户端发来的消息
for(int i=0;isocketList.size();i++){//发送给所有连接的客户端
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socketList.get(i).getOutputStream()));
pw.println(message);
pw.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
int portNum = 8888;//创建服务器的端口号
Server server = new Server(portNum);
server.innit();
}
}
客户端类:
package chat;
import java.io.*;
import java.net.*;
import java.util.*;
/*客户端类*/
public class Client {
public Socket socket;
public Client(){
Scanner sca = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1",8888);//创建客户端
new ReadThread(socket).start();//开启读取从服务器端发来的信息
while(true){
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
String message = sca.nextLine();
pw.println(message);//向服务器发送信息
pw.flush();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("错误:服务器未开启!!!");
}
}
class ReadThread extends Thread{//读取服务器发来信息的线程
public Socket socket;
public ReadThread(Socket socket){
this.socket = socket;
}
public void run() {
super.run();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String message = br.readLine();
System.out.println(message);//输出信息
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client();
}
}
现打开服务器类,然后可以打开多个客户端进行聊天
用java怎么实现QQ登录界面?
用java做QQ登录界面的写法如下:
package ch10;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
1、//定义该类继承自JFrame,实现ActionListener接口
public class LoginTest extends JFrame implements ActionListener
{
2、//创建JPanel对象
private JPanel jp=new JPanel();
3、//创建3个标并加入数组
JLabel name = new JLabel("请输入用户名");
JLabel password = new JLabel("请输入密码");
JLabel show = new JLabel("");
private JLabel[] jl={name,password,show};
4、//创建登陆和重置按扭并加入数组
JButton login = new JButton("登陆");
JButton reset = new JButton("重置");
private JButton[] jb={login,reset};
5、//创建文本框以及密码框
private JTextField jName=new JTextField();
private JPasswordField jPassword =new JPasswordField();
public LoginTest()
{
6、//设置布局管理器为空布局,这里自己摆放按钮、标签和文本框
jp.setLayout(null);
for(int i=0;i2;i++)
{
7、//设置标签和按扭的位置与大小
jl[i].setBounds(30,20+40*i,180,20);
jb[i].setBounds(30+110*i,100,80,20);
8、//添加标签和按扭到JPanel容器中
jp.add(jl[i]);
jp.add(jb[i]);
//为2个按钮注册动作事件监听器
jb[i].addActionListener(this);
}
9、//设置文本框的位置和大小,注意满足美观并足够用户名的长度
jName.setBounds(130,15,100,20);
10、//添加文本框到JPanel容器中
jp.add(jName);
11、//为文本框注册动作事件监听器
jName.addActionListener(this);
12、//设置密码框的位置和大小,注意满足美观和足够密码的长度
jPassword.setBounds(130,60,100,20);
13、//添加密码框到JPanel容器中
jp.add(jPassword);
14、//设置密码框中的回显字符,这里设置美元符号
jPassword.setEchoChar('$');
15、//为密码框注册动作事件监听器
jPassword.addActionListener(this);
16、//设置用于显示登陆状态的标签大小位置,并将其添加进JPanel容器
jl[2].setBounds(10,180,270,20);
jp.add(jl[2]);
17、//添加JPanel容器到窗体中
this.add(jp);
18、//设置窗体的标题、位置、大小、可见性及关闭动作
this.setTitle("登陆窗口");
this.setBounds(200,200,270,250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
19、//实现动作监听器接口中的方法actionPerformed
public void actionPerformed(ActionEvent e)
{
20、//如果事件源为文本框
if(e.getSource()==jName)
{
21、//切换输入焦点到密码框
jPassword.requestFocus();
}
22、//如果事件源为重置按扭
else if(e.getSource()==jb[1])
{
23、//清空姓名文本框、密码框和show标签中的所有信息
jl[2].setText("");
jName.setText("");
jPassword.setText("");
24、//让输入焦点回到文本框
jName.requestFocus();
}
25、//如果事件源为登陆按钮,则判断登录名和密码是否正确
else
{
26、//判断用户名和密码是否匹配
if(jName.getText().equals("lixiangguo")
String.valueOf(jPassword.getPassword()).equals("19801001"))
{
27、jl[2].setText("登陆成功,欢迎您的到来!");
}
else
{
28、jl[2].setText("对不起,您的用户名或密码错误!");
}
}
}
public static void main(String[] args)
{
29、//创建LoginTest窗体对象
new LoginTest();
}
}
哪位大侠用java做个类似QQ的程序
//无语音和视频,只有聊天和文件收发,不知能否能帮你
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class test
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class DrawFrame extends JFrame
{
public DrawFrame()
{
setLocation(100,100);
setTitle("我的QQ");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
//添加菜单项……
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu edit = new JMenu("菜单");
menuBar.add(edit);
JMenuItem sset = new JMenuItem("设置");
JMenuItem eexit = new JMenuItem("退出");
edit.add(sset);
edit.addSeparator();
edit.add(eexit);
final DrawPanel pan = new DrawPanel();
//添加设置对话框
sset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
final JFrame jf = new JFrame("设置");
jf.setSize(300,150);
jf.setLocation(200,200);
jf.setResizable(false);
jf.setVisible(true);
JPanel jp = new JPanel();
final JTextField jt1 = new JTextField(20);
final JTextField jt2 = new JTextField(20);
final JTextField jt3 = new JTextField(20);
jp.add(new JLabel("聊天端口:"));
jf.add(jp);
jp.add(jt1);
jp.add(new JLabel("目标地址:"));
jp.add(jt2);
jp.add(new JLabel("文件端口:"));
jp.add(jt3);
JButton jb1 = new JButton("确定");
JButton jb2 = new JButton("取消");
jp.add(jb1);
jp.add(jb2);
//设置“设置”按钮
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
try{
int cat1 = Integer.parseInt(jt1.getText());
pan.setCat1(cat1);//设置对话端口号
int cat2 = Integer.parseInt(jt3.getText());
pan.setCat2(cat2);//设置文件传输端口号
String s = jt2.getText();
pan.setIP(s);//设置IP地址
pan.ls();
}
catch(Exception e)
{
e.printStackTrace();
}
jf.dispose();//关闭对话框
}
});
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
jf.dispose();
}
});
}
});
eexit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
add(pan);
pack();
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
}
class DrawPanel extends JPanel
{
DatagramSocket ds;
DatagramPacket dp;
DatagramSocket ds1;
DatagramPacket dp1;
Thread thread;
Thread thread1;
TextArea textArea1 = new TextArea(10,50);
TextArea textArea2 = new TextArea(10,50);
JTextField textField = new JTextField(50);
static int cat1,cat2;
static String IP;
public void setCat1(int cat)
{
cat1 = cat;
}
public void setCat2(int cat)
{
cat2 = cat;
}
public void setIP(String ip)
{
IP=ip;
}
//重置端口号
public void ls()
{
try
{
ds = new DatagramSocket(cat1);
ds1 = new DatagramSocket(cat2);
}
catch(Exception e)
{
e.printStackTrace();
}
thread = new Thread(new Runnable()
{
public void run()
{
byte buf[] = new byte[1024];//聊天每次发送文字不能超过1024b
DatagramPacket dp = new DatagramPacket(buf,buf.length);
while(true)
{
try
{
ds.receive(dp);
textArea1.setText(textArea1.getText()+ new String(buf,0,dp.getLength()) + "\r\n");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
});
thread.start();
}
public DrawPanel()
{
setLayout(new BorderLayout());
JTabbedPane card = new JTabbedPane();
textArea1.setEditable(false);
card.add("聊天",textArea1);
add(card,BorderLayout.NORTH);
JPanel center = new JPanel();
center.setLayout(new FlowLayout());
center.add(new JLabel("昵称:"));
center.add(textField);
add(center,BorderLayout.CENTER);
JPanel south = new JPanel();
south.setLayout(new BorderLayout());
south.add(textArea2,BorderLayout.NORTH);
JPanel south2 = new JPanel();
south2.setLayout(new FlowLayout());
JButton sent = new JButton("传送文件");
JButton receive = new JButton("接收文件");
//receive.setEnabled(false);
JButton sentMessage = new JButton("发送");
south2.add(sent);
south2.add(receive);
south2.add(sentMessage);
add(south,BorderLayout.SOUTH);
south.add(south2,BorderLayout.SOUTH);
//发送信息的按钮的监听器:
sentMessage.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
textArea1.append(textField.getText()+":"+textArea2.getText()+"\n");
String s;
try
{
s = textField.getText() + ":" + textArea2.getText();
byte[] buf = s.getBytes();
dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(IP),cat1);
ds.send(dp);
textArea2.setText("");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
//发送文件的按钮的监听器
sent.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JFrame f = new JFrame("打开");
f.setSize(400,300);
JFileChooser chooser = new JFileChooser();
f.add(chooser);
int returnVal = chooser.showOpenDialog(f);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
textArea1.append("正在传送文件:"+chooser.getCurrentDirectory()+"\\"
+chooser.getSelectedFile().getName()+"\n");
try
{
byte[] b = new byte[10248];
FileInputStream fis = new FileInputStream(chooser.getSelectedFile());
DataInputStream dos =new DataInputStream(fis);
dos.read(b);
dp1 = new DatagramPacket(b,b.length,InetAddress.getByName(IP),cat2);
ds1.send(dp1);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
});
//接收文件的按钮的监听器:
receive.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JFrame f1 = new JFrame("保存");
f1.setSize(400,300);
byte buf[] = new byte[10248];
DatagramPacket dp1 = new DatagramPacket(buf,buf.length);
JFileChooser ch = new JFileChooser();
f1.add(ch);
int a = ch.showSaveDialog(f1);
if(a ==JFileChooser.APPROVE_OPTION)
{
String fileName = ch.getSelectedFile().getPath();
try
{
ds1.receive(dp1);
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(buf);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
});
}
}
java模仿qq的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java模仿多人群聊、java模仿qq的信息别忘了在本站进行查找喔。