如何用java写一个聊天工具_用java写的一个聊天工具

这个Java程序是一个Swing实现的客户端框架,用于聊天应用。它包含了连接到服务器的逻辑,用户界面组件如文本框、滚动面板、选项框和按钮,以及发送和接收消息的功能。用户可以输入消息,选择接收者,并有弹窗提示功能。
摘要由CSDN通过智能技术生成

package mychat.client;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.Point;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.IOException;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollBar;

import javax.swing.JScrollPane;

import javax.swing.JTabbedPane;

import javax.swing.JTextPane;

import mychat.common.Constants;

import mychat.common.MessageFormat;

public class ClientFrame extends JFrame

implements ActionListener {

private ConnectionToServer

toServer;

protected StringBuffer sbfMessage = new

StringBuffer();

private JPanel pnlRecive =

new JPanel();

private JPanel pnlSend =

new JPanel();

private JPanel pnlInfo =

new JPanel();

protected JLabel lblInfo =

new JLabel();

private JTextPane

txtRecive = new JTextPane();

private JScrollPane

scpRecive = new JScrollPane();

private JTextPane txtSend

= new JTextPane();

private JTabbedPane

tabRecive = new JTabbedPane();

private JButton btnSend =

new JButton();

private JComboBox cmbSendToUser=new

JComboBox();

private JCheckBox chkDong=new

JCheckBox();

public ClientFrame()

{

ConnectionToServer server = new

ConnectionToServer(this);

Thread thread = new

Thread(server);

thread.start();

this.toServer =

server;

//

ClientFrame

this.setResizable(false);

JPanel contentPane = (JPanel)

this.getContentPane();

this.setTitle("Client,Chating...");

this.setSize(600, 450);

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

this.addWindowListener(new

WindowAdapter() {

public

void windowClosing(WindowEvent e) {

//

TODO Auto-generated method stub

try

{

toServer.dataOut.writeUTF(Constants.MSG_TYPE_EXIT+toServer.username);

}

catch (IOException e1) {

//

TODO Auto-generated catch block

e1.printStackTrace();

}

}

});

//

pnlInfo

pnlInfo.setLayout(new

BorderLayout());

lblInfo.setHorizontalAlignment(JLabel.CENTER);

lblInfo.setVerticalAlignment(JLabel.CENTER);

lblInfo.setBorder(BorderFactory.createEtchedBorder());

pnlInfo.add(lblInfo);

//

pnlSend

pnlSend.setLayout(new

BorderLayout());

final JPanel pnlBtnSend = new

JPanel();

chkDong.setText("dong");

cmbSendToUser.addItem("All");

btnSend.setText("Send");

pnlBtnSend.setLayout(new

GridLayout());

pnlBtnSend.add(chkDong);

pnlBtnSend.add(cmbSendToUser);

pnlBtnSend.add(btnSend);

pnlBtnSend.setBorder(BorderFactory.createEmptyBorder(10,

10, 10, 10));

btnSend.addActionListener(this);

txtSend.setText("Sending...");

txtSend.setBorder(BorderFactory.createEtchedBorder());

btnSend.addMouseListener(new

MouseAdapter() {

public

void mouseClicked(MouseEvent e) {

//

TODO Auto-generated method stub

setReciveTextBar();

sendMessage();

txtSend.setText("");

txtSend.requestFocus();

}

public

void mouseEntered(MouseEvent e) {

//

TODO Auto-generated method stub

btnSend.setForeground(Color.red);

}

public

void mouseExited(MouseEvent e) {

//

TODO Auto-generated method stub

btnSend.setForeground(Color.black);

}

});

pnlSend.add(new

JScrollPane(txtSend), BorderLayout.CENTER);

pnlSend.add(pnlBtnSend,

BorderLayout.EAST);

pnlSend.setBorder(BorderFactory.createEtchedBorder());

//

pnlRecive

pnlRecive.setLayout(new

BorderLayout());

txtRecive.setText("reciving...");

txtRecive.setEditable(false);

scpRecive.getViewport().add(txtRecive);

tabRecive.add("Content",

scpRecive);

pnlRecive.add(tabRecive);

pnlRecive.setBorder(BorderFactory.createEtchedBorder());

contentPane.setLayout(new

BorderLayout());

contentPane.add(pnlInfo,

BorderLayout.NORTH);

contentPane.add(pnlRecive,

BorderLayout.CENTER);

contentPane.add(pnlSend,

BorderLayout.SOUTH);

}

private void sendMessage() {

// TODO Auto-generated method

stub

StringBuffer sendMsg=new

StringBuffer();

String

message=this.txtSend.getText();

if(message==null||message.trim().length()<1){

JOptionPane.showMessageDialog(this,

"Message is null or spaces!\nPlease input message!");

return;

}

if(toServer.username.trim().equals(this.cmbSendToUser.getSelectedItem())){

JOptionPane.showMessageDialog(this,

"Can't send message to yourself!");

return;

}

String

messageTo=(String)this.cmbSendToUser.getSelectedItem();

sendMsg.append(getSendMessageHead(messageTo));

if(this.chkDong.isSelected()){

this.douDong(30);

sendMsg.append(Constants.FRAME_DONG);

}else{

sendMsg.append(Constants.FRAME_NOT_DONG);

}

sendMsg.append(message);

try

{

toServer.dataOut.writeUTF(sendMsg.toString());

} catch (IOException e) {

// TODO

Auto-generated catch block

JOptionPane.showMessageDialog(this,

"Message send is defeated!reason maybe is:"+e.getMessage());

e.printStackTrace();

} if(sbfMessage.length()>0){

sbfMessage.append("\n");

}

sbfMessage.append(MessageFormat.formatToShow(sendMsg.toString()));

setReciveMessage();

}

private String getSendMessageHead(String

messageTo){

StringBuffer head=new

StringBuffer();

head.append(Constants.MSG_TYPE_CHATMESSAGE);

head.append(MessageFormat.formatUsername(toServer.username));

if("All".equals(messageTo)){

head.append(Constants.MSG_MESSAGE_TO_ALL);

}else{

messageTo=MessageFormat.formatUsername(messageTo);

head.append(messageTo);

}

head.append(MessageFormat.getFormatNowDate());

return head.toString();

}

protected void setReciveMessage(){

this.txtRecive.setText(sbfMessage.toString());

setReciveTextBar();

}

public void

actionPerformed(ActionEvent e) {

// TODO Auto-generated method

stub

}

public String

inputUsername(int type) {

String username =

null;

if (type

== 1) {

while

(username == null) {

username

= JOptionPane.showInputDialog(this,

"Please

input your name:", "Inpu name",

JOptionPane.INFORMATION_MESSAGE);

}

} else if (type == 2) {

while

(username == null) {

username

= JOptionPane.showInputDialog(this,

"The

name is used!\nPlease input other name:",

"The

name is used", JOptionPane.INFORMATION_MESSAGE);

}

}

return username;

}

public String

inputServerIp(int type) {

String serverIp = null;

if(type==1){

while

(serverIp == null) {

serverIp

= JOptionPane.showInputDialog(this,

"Please

input server ip:", "Server",

JOptionPane.INFORMATION_MESSAGE);

}

}else if(type==2){

serverIp =

JOptionPane.showInputDialog(this,

"The

server is not exit!\nPlease input other server ip:", "Other

server",

JOptionPane.INFORMATION_MESSAGE);

if(serverIp==null||serverIp.trim().length()<1){

System.exit(0);

}

}

return serverIp;

}

public void

setOnlineUser() {

java.util.List userList =

toServer.userList;

StringBuffer sbfUserlist = new

StringBuffer("Online User:");

for (int i = 0; i <

userList.size(); i++) {

String user =

(String) userList.get(i);

sbfUserlist.append(user.trim());

if ((i + 1)

!= userList.size()) {

sbfUserlist.append("

,");

}

}

this.lblInfo.setText(sbfUserlist.toString());

}

public void setSendToUser() {

java.util.List userList =

toServer.userList;

this.cmbSendToUser.removeAllItems();

cmbSendToUser.addItem("All");

for (int i = 0; i <

userList.size(); i++) {

String user =

(String) userList.get(i);

cmbSendToUser.addItem(user.trim());

}

}

protected void douDong(int fanWei){

this.setAlwaysOnTop(true);

if(this.getExtendedState()==JFrame.ICONIFIED){

this.setExtendedState(JFrame.NORMAL);

}

Point

point=this.getLocation();

for(int

i=0;i<200;i++){

this.setLocation((int)(point.getX()+Math.random()*fanWei),

(int)(point.getY()+Math.random()*fanWei));

}

this.setAlwaysOnTop(false);

this.setLocation(point);

}

protected void setReciveTextBar(){

JScrollBar

jsb=scpRecive.getVerticalScrollBar();

if(jsb.getValue()!=jsb.getMaximum()){

jsb.setValue(jsb.getMaximum());

}

}

public static void

main(String[] args) {

ClientFrame cf = new

ClientFrame();

Dimension screen =

cf.getToolkit().getScreenSize();

cf.setLocation((int)

((screen.getWidth() - cf.getWidth()) / 2),

(int)

((screen.getHeight() - cf.getHeight()) / 2));

cf.setVisible(true);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值