首页 > 后端开发 > 正文

java 如何模仿QQ聊天框那样可以显示文字与图片啊?

2024-05-25 06:48:17 | 我爱编程网

java 如何模仿QQ聊天框那样可以显示文字与图片啊?相关内容,小编在这里做了整理,希望能对大家有所帮助,关于java 如何模仿QQ聊天框那样可以显示文字与图片啊?信息,一起来了解一下吧!

本文目录一览:

java 如何模仿QQ聊天框那样可以显示文字与图片啊?

用java制作qq登录界面,只要界面,不要事件处理

package ibees.qq;

import java.awt.BorderLayout;

import java.net.URL;

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.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

/**

* 仿QQ登录界面,仅供学习参考,涉及到的有窗口居中、JPanel、LayoutManager的使用

* @author hhzxj2008

* */

public class QQLoginView extends JFrame {

/**

*

*/

private static final long serialVersionUID = -5665975170821790753L;

public QQLoginView() {

initComponent();

}

private void initComponent() {

setTitle("用户登录");

//设置LOGO

URL image = QQLoginView.class.getClassLoader().getResource("ibees/qq/images/year.jpg");//图片的位置

JLabel imageLogo = new JLabel(new ImageIcon(image));

add(imageLogo,BorderLayout.NORTH);

//QQ号和密码

JPanel jp = new JPanel();

JPanel jpAccount = new JPanel();

jpAccount.add(new JLabel("帐号"));

JTextField userTextField = new JTextField(15);

jpAccount.add(userTextField);

jpAccount.add(new JLabel("用户注册"));

jp.add(jpAccount);

JPanel jpPass = new JPanel();

jpPass.add(new JLabel("密码"));

JPasswordField passTextField = new JPasswordField(15);

jpPass.add(passTextField);

jpPass.add(new JLabel("找回密码"));

jp.add(jpPass);

//登录设置

JPanel jpstatus = new JPanel();

jpstatus.add(new JLabel("状态"));

JComboBox statusComboBox = new JComboBox();

statusComboBox.addItem("Q我");

statusComboBox.addItem("在线");

statusComboBox.addItem("隐身");

statusComboBox.addItem("离线");

jpstatus.add(statusComboBox);

jpstatus.add(new JCheckBox("记住密码"));

jpstatus.add(new JCheckBox("自动登录"));

jp.add(jpstatus);

add(jp);

//底部登录按钮

JPanel bottomPanel = new JPanel();

bottomPanel.setLayout(new BorderLayout());

bottomPanel.add(new JButton("设置"),BorderLayout.WEST);

bottomPanel.add(new JButton("登录"),BorderLayout.EAST);

add(bottomPanel,BorderLayout.SOUTH);

setSize(324,230);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

}

/**

* @param args

*/

public static void main(String[] args) {

java.awt.EventQueue.invokeLater(new Runnable(){

@Override

public void run() {

new QQLoginView().setVisible(true);

}

});

}

}

java 如何模仿QQ聊天框那样可以显示文字与图片啊?

java 如何模仿QQ聊天框那样可以显示文字与图片啊?

两种解决办法,

第一种使用纯java的方式,使用组件JTextPane 或者JEditorPane 来显示图片和文字

第二种使用JEditorPane,但是里面存放HTML代码. 用HTML来控制文字和图片的显示

java 如何模仿QQ聊天框那样可以显示文字与图片啊?

用Java编写类似QQ对话框程序

给你个Socket/ServerSocket写的小例子,看看能不能帮到你哦:

先运行ServerGUI,启动服务器端,再运行ClientGUI,双方就可以发送字符串了...

ServerGUI类:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.IOException;

@SuppressWarnings("serial")

public class ServerGUI extends JFrame {

private JTextArea jta, jtaInput;

private JPanel jtaAreaPane;

private JPanel j;

private JButton buttonSubmit, buttonExit;

private String stringGet = null;

private OurServer os = null;

public ServerGUI(String s) {

super(s);

jtaAreaPane = new JPanel();

jtaAreaPane.setLayout(new GridLayout(2, 1));

jta = new JTextArea(7, 35);

jta.setLineWrap(true);

jta.setBackground(new Color(169, 255, 128));

JScrollPane jsp = new JScrollPane(jta);

jtaInput = new JTextArea(7, 35);

jtaInput.setLineWrap(true);

jtaInput.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == 10) {

String s = jtaInput.getText();

jtaInput.setText(null); // 输入框重新设为空

jta.append("\n" + "你发送了:" + s.trim());

stringGet = s;

if (stringGet != null) {

System.out.println("stringGet:" + stringGet);

try {

os.getOsw().write(stringGet + "\n");

os.getOsw().flush();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

});

jtaInput.setBackground(new Color(133, 168, 250));

JScrollPane jspIn = new JScrollPane(jtaInput);

jtaAreaPane.add(jsp);

jtaAreaPane.add(jspIn);

j = new JPanel();

buttonSubmit = new JButton("提交");

buttonSubmit.addActionListener(new SetButtonSubmit());

buttonExit = new JButton("关闭");

buttonExit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

});

j.add(buttonSubmit);

j.add(buttonExit);

setLayout(new BorderLayout());

add(jtaAreaPane, BorderLayout.CENTER);

add(j, BorderLayout.SOUTH);

setSize(new Dimension(400, 500));

setLocation(500, 300);

pack();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setResizable(false);

setVisible(true);

new Thread(new ServerOurRead()).start();

os = new OurServer();

}

public String GetString() {

return stringGet;

}

public void setStringGet(String s) {

this.stringGet = s;

}

class SetButtonSubmit implements ActionListener {

@SuppressWarnings("static-access")

public void actionPerformed(ActionEvent e) {

String s = jtaInput.getText();

jtaInput.setText(null);

jta.append("\n" + "你发送了:" + s.trim());// jta.setText("你发送了:"+oc.s);

stringGet = s;

if (stringGet != null) {

System.out.println("stringGet:" + stringGet);

try {

os.getOsw().write(stringGet + "\n");

os.getOsw().flush();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

// 接收来自客户端的线程

class ServerOurRead implements Runnable {

public void run() {

while (true) {

try {

jta.append("\n来自客户端:" + os.getBr().readLine());

} catch (Exception e1) {

e1.printStackTrace();

}

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

public static void main(String args[]) {

new ServerGUI("服务器对话框");

}

}

OurServer类:

import java.util.*;

import java.io.*;

import java.net.*;

public class OurServer {

private ServerSocket serverSocket = null;

private OutputStream os = null;

private InputStream is = null;

private OutputStreamWriter osw = null;

private InputStreamReader isr = null;

private BufferedReader br = null;

private ArrayList socketList = null;

private Scanner console = new Scanner(System.in);

public OurServer() {

try {

serverSocket = new ServerSocket(22222);

System.out.println("serverSocket is waiting...");

Socket soc = serverSocket.accept();

os = soc.getOutputStream();

is = soc.getInputStream();

osw = new OutputStreamWriter(os);

isr = new InputStreamReader(is);

br = new BufferedReader(isr);

} catch (IOException e) {

e.printStackTrace();

}

}

// 从客户端读信息的线程

public static void main(String args[]) {

}

public BufferedReader getBr() {

return br;

}

public void setBr(BufferedReader br) {

this.br = br;

}

public OutputStreamWriter getOsw() {

return osw;

}

public void setOsw(OutputStreamWriter osw) {

this.osw = osw;

}

}

ClientGUI类:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.IOException;

@SuppressWarnings("serial")

public class ClientGUI extends JFrame {

private JTextArea jta, jtaInput;

private JPanel jtaAreaPanel;

private JPanel j;

private JButton buttonSubmit, buttonExit;

private String stringGet = null;

private OurClient oc = null;

public ClientGUI(String s) {

super(s);

jtaAreaPanel = new JPanel();

jtaAreaPanel.setLayout(new GridLayout(2, 1));

jta = new JTextArea(7, 35);

jta.setLineWrap(true);

jta.setBackground(new Color(169, 255, 128));

JScrollPane jsp = new JScrollPane(jta);

jtaInput = new JTextArea(7, 35);

jtaInput.setLineWrap(true);

jtaInput.setBackground(new Color(133, 168, 250));

jtaInput.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == 10) {

String s = jtaInput.getText();

jtaInput.setText(null); // 输入框重新设为空

jta.append("\n" + "你发送了:" + s.trim());

stringGet = s;

if (stringGet != null) {

System.out.println("stringGet:" + stringGet);

try {

oc.getOsw().write(stringGet + "\n");

oc.getOsw().flush();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

});

JScrollPane jspIn = new JScrollPane(jtaInput);

jtaAreaPanel.add(jsp);

jtaAreaPanel.add(jspIn);

j = new JPanel();

buttonSubmit = new JButton("提交");

buttonSubmit.addActionListener(new SetButtonSubmit());

buttonExit = new JButton("关闭");

buttonExit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

});

j.add(buttonSubmit);

j.add(buttonExit);

setLayout(new BorderLayout());

add(jtaAreaPanel, BorderLayout.CENTER);

add(j, BorderLayout.SOUTH);

setSize(new Dimension(400, 500));

setLocation(500, 300);

pack();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setResizable(false);

setVisible(true);

// 启动读取服务端信息线程

new Thread(new ServerOurRead()).start();

oc = new OurClient();

}

public String GetString() {

return stringGet;

}

public void setStringGet(String s) {我爱编程网

this.stringGet = s;

}

class SetButtonSubmit implements ActionListener {

@SuppressWarnings("static-access")

public void actionPerformed(ActionEvent e) {

String s = jtaInput.getText();

jtaInput.setText(null); // 输入框重新设为空

jta.append("\n" + "你发送了:" + s.trim());

stringGet = s;

if (stringGet != null) {

System.out.println("stringGet:" + stringGet);

try {

oc.getOsw().write(stringGet + "\n");

oc.getOsw().flush();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

// 接收来自客户端的线程

class ServerOurRead implements Runnable {

public void run() {

while (true) {

try {

// oc.getBr().readLine()此方法一直在读,直到流中有数据

jta.append("\n来自客户端:" + oc.getBr().readLine());// readLine()

} catch (Exception e1) {

}

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

public static void main(String args[]) {

new ClientGUI("对话框");

}

}

OurClient 类:

import java.io.*;

import java.net.*;

import java.util.*;

public class OurClient {

private Socket socket = null;

private OutputStream os = null;

private InputStream is = null;

private OutputStreamWriter osw = null;

private InputStreamReader isr = null;

private BufferedReader br = null;

private Scanner console = null;

private static String s = null;

private static String In = null;

public OurClient() {

console = new Scanner(System.in);

try {

socket = new Socket("127.0.0.1", 22222);

os = socket.getOutputStream();

is = socket.getInputStream();

osw = new OutputStreamWriter(os);

isr = new InputStreamReader(is);

br = new BufferedReader(isr);

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

// 从服务端读信息的线程

public BufferedReader getBr() {

return br;

}

public void setBr(BufferedReader br) {

this.br = br;

}

public OutputStreamWriter getOsw() {

return osw;

}

public void setOsw(OutputStreamWriter osw) {

this.osw = osw;

}

public static void main(String args[]) {

}

}

以上就是java 如何模仿QQ聊天框那样可以显示文字与图片啊?全部内容了,了解更多相关信息,关注我爱编程网。

免责声明:文章内容来自网络,如有侵权请及时联系删除。
与“java 如何模仿QQ聊天框那样可以显示文字与图片啊?”相关推荐