实例介绍
【实例简介】使用了线程、实现了多人聊天室。
【实例截图】
【核心代码】
package com.wanggensheng.chatClient;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JScrollPane;
public class ClientMainWindow extends JFrame {
private static final long serialVersionUID = 1L;
private JScrollPane scrollPane_1;
JTextArea chatWindow;
JTextArea userList;
JTextArea SendMsg;
JButton chatCancelButton;
JButton chatSendButton;
String userName;
String sex;
String userInputName;
/**
* Create the frame.
*/
public ClientMainWindow() {
setAlwaysOnTop(false);
addWindowListener(new WindowAdapter() {
@Override
//关闭窗口动作响应,发送下线信息
public void windowClosing(WindowEvent arg0) {
try {
ChatManager.getCM().send("下线了:" LoginWindow.userInputName ":" LoginWindow.sex);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
setTitle("聊天客户端~~" LoginWindow.userInputName);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 520, 360);
JLabel label = new JLabel("消息窗口");
JLabel label_1 = new JLabel("在线用户列表");
chatSendButton = new JButton("发送");
chatSendButton.addMouseListener(new MouseAdapter() { //发送按钮的事件响应
@Override
public void mouseClicked(MouseEvent arg0) {
try {
ChatManager.getCM().send(LoginWindow.userInputName ":" SendMsg.getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SendMsg.setText("");
}
});
chatCancelButton = new JButton("取消");
chatCancelButton.addMouseListener(new MouseAdapter() { //设定取消按钮的事件响应,清空输入框
@Override
public void mouseClicked(MouseEvent e) {
SendMsg.setText("");
}
});
SendMsg = new JTextArea();
SendMsg.setText("你好");
SendMsg.setColumns(10);
JScrollPane scrollPane = new JScrollPane();
/*
* 窗体参数设置,自动生成
*/
scrollPane_1 = new JScrollPane();
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(SendMsg, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
.addComponent(label)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1))
.addGap(10))
.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
.addComponent(chatSendButton, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
.addGap(96)
.addComponent(chatCancelButton, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)))
.addGap(0))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(label_1))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
.addGap(18)
.addComponent(SendMsg, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addComponent(scrollPane_1, GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE))
.addGap(11)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(chatCancelButton)
.addComponent(chatSendButton, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
userList = new JTextArea();
userList.setEditable(false);
scrollPane_1.setViewportView(userList);
userList.setLineWrap(true);
chatWindow = new JTextArea();
chatWindow.setEditable(false);
scrollPane.setViewportView(chatWindow);
chatWindow.setLineWrap(true);
chatWindow.setText("欢迎进入聊天室......" "\n");
getContentPane().setLayout(groupLayout);
}
/*
* 以下为给文本域追加消息的方法
* append2ChatWindow(String in)
* append2UserList(String in)
*/
public void append2ChatWindow(String in) {
chatWindow.append(in);
chatWindow.setCaretPosition(chatWindow.getText().length());
}
public void append2UserList(String in) {
userList.append(in);
userList.setCaretPosition(userList.getText().length());
}
public void clearUserList() {
userList.setText(null);
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论