Java实现的c/s的聊天室[Java编程]
本文“Java实现的c/s的聊天室[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
//Client
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.util.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.*;
//登录窗口
public class Frame2 extends JFrame {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
ButtonGroup buttonGroup1 = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JLabel jLabel2 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
String sex="男";
String name=null;
boolean bool=false;
Socket socket;
DataInputStream in;
DataOutputStream out;
public Frame2() {
try {
// setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
}
);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
this.setResizable(false);
setSize(new Dimension(400, 300));
setTitle("登录界面");
jLabel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel1.setText("请输入您的昵称:");
jLabel1.setBounds(new Rectangle(53, 70, 113, 44));
jTextField1.setBounds(new Rectangle(182, 75, 154, 33));
jTextField1.addKeyListener(new Frame2_jTextField1_keyAdapter(this));
jRadioButton1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jRadioButton1.setSelected(true);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(186, 114, 91, 25));
jRadioButton1.addActionListener(new Frame2_jRadioButton1_actionAdapter(this));
jRadioButton2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jRadioButton2.setText("女");
jRadioButton2.setBounds(new Rectangle(187, 140, 91, 25));
jRadioButton2.addActionListener(new Frame2_jRadioButton2_actionAdapter(this));
jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
jLabel2.setToolTipText("");
jLabel2.setText("性别:");
jLabel2.setBounds(new Rectangle(136, 131, 34, 16));
jButton1.setBounds(new Rectangle(182, 218, 71, 25));
jButton1.setText("肯定");
jButton1.addActionListener(new Frame2_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(264, 218, 71, 25));
jButton2.setText("撤消");
jButton2.addActionListener(new Frame2_jButton2_actionAdapter(this));
contentPane.add(jLabel1);
contentPane.add(jTextField1);
contentPane.add(jRadioButton1);
contentPane.add(jRadioButton2);
contentPane.add(jLabel2);
contentPane.add(jButton2);
contentPane.add(jButton1);
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
setVisible(true);
}
public void setClection(Socket soc,DataInputStream din,DataOutputStream dout ){
socket=soc;
in=din;
out=dout;
}
public boolean getYesOrNo(){
return bool;
}
public String getName(){
return name;
}
public String getSex(){
return sex;
}
public void jRadioButton1_actionPerformed(ActionEvent e) {
sex="男";
}
public void jRadioButton2_actionPerformed(ActionEvent e) {
sex="女";
}
public void jButton1_actionPerformed(ActionEvent e) {
if(jTextField1.getText().equals("")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals("请输入昵称")){
jTextField1.setText("请输入昵称");
return;
}
name=jTextField1.getText();
try{
out.writeUTF("新的朋友:" + name +"/"+ sex);
}
catch(Exception ee){
return;
}
}
public void jButton2_actionPerformed(ActionEvent e) {
jTextField1.setText("");
}
public void jTextField1_keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER ){
if(jTextField1.getText().equals("")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals("请输入昵称")){
jTextField1.setText("请输入昵称");
return;
}
name=jTextField1.getText();
try{
out.writeUTF("新的朋友:" + name +"/"+ sex);
if(in.readUTF().startsWith("可以") ){
bool=true;
}
else{
jTextField1.setText("请重新输入昵称") ;
return;
}
}
catch(Exception ee){
return;
}
setVisible(false);
}
}
}
class Frame2_jTextField1_keyAdapter extends KeyAdapter {
private Frame2 adaptee;
Frame2_jTextField1_keyAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.jTextField1_keyPressed(e);
}
}
class Frame2_jButton2_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jButton2_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Frame2_jButton1_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jButton1_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Frame2_jRadioButton2_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jRadioButton2_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton2_actionPerformed(e);
}
}
class Frame2_jRadioButton1_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jRadioButton1_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton1_actionPerformed(e);
}
}
//聊天界面
public class Frame1 extends JFrame implements Runnable{
Thread thread;
JPanel contentPane;
Frame2 frame=new Frame2();
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextArea jTextArea1 = new JTextArea();
JTextArea jTextArea2 = new JTextArea();
JScrollPane jScrollPane3 = new JScrollPane();
JList jList1 = new JList();
Socket socket;
int port =10000;
DataInputStream in;
DataOutputStream out;
Vector vector=new Vector();
String wenBen;
String str;
String xinxi;
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try{
out.writeUTF("下线了:"+frame.name+"/"+frame.sex);
out.close() ;
in.close() ;
socket.close();
}
catch(Exception ee){
}
finally{
frame.dispose() ;
dispose();
}
}
}
);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setTitle("聊天界面");
this.setResizable(false);
setSize(new Dimension(602, 515));
frame.setVisible(true);
jScrollPane1.setBounds(new Rectangle(2, 2, 445, 320));
jScrollPane2.setBounds(new Rectangle(1, 333, 448, 95));
jButton1.setBounds(new Rectangle(93, 441, 78, 31));
jButton1.setText("发送");
jButton2.setBounds(new Rectangle(185, 441, 76, 31));
jButton2.setText("清空");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jTextArea1.setEditable(false);
jScrollPane3.setBounds(new Rectangle(458, 6, 139, 424));
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jTextArea2.addKeyListener(new Frame1_jTextArea2_keyAdapter(this));
contentPane.add(jScrollPane2);
jScrollPane2.getViewport().add(jTextArea2);
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jTextArea1);
contentPane.add(jScrollPane3);
contentPane.add(jButton1);
contentPane.add(jButton2);
jScrollPane3.getViewport().add(jList1);
this.setVisible(false);
try {
socket = new Socket("liu.dhcn.net", port);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
setVisible(true);
jTextArea2.setText("无法与服务器联接,请退出...") ;
}
if (thread == null) {
thread = new Thread(this);
thread.start();
}
frame.setClection(socket, in, out);
}
public boolean setClection1() {
frame.setClection(socket, in, out);
return frame.bool;
}
public void setClection2() {
wenBen = jTextArea2.getText();
if (wenBen.equals("")) {
jTextArea2.setText("please say a word:");
return;
}
try {
out.writeUTF(frame.name + " : " + wenBen);
jTextArea2.setText("") ;
} catch (Exception ee) {
}
}
public void run() {
String sl = null;
String ss = null;
String s = null;
while (true) {
try {
sl = in.readUTF();
} catch (Exception ee) {
vector.removeAllElements();
jList1.setListData(vector);
jTextArea2.setText("巳经与服务器断开...");
return;
}
if(sl.startsWith("可以") ){
frame.setVisible(false);
setVisible(true);
setTitle("昵称..." + frame.getName());
continue;
}
else if(sl.startsWith("不可以")){
frame.jTextField1.setText("请重新输入昵称") ;
continue;
}
else if (sl.startsWith("新的朋友")) {
StringTokenizer st = new StringTokenizer(sl, ":");
ss = null;
s = null;
ss = st.nextToken();
s = st.nextToken();
vector.addElement(s);
jList1.setListData(vector);
continue;
}
else if (sl.startsWith("下线了")) {
StringTokenizer st = new StringTokenizer(sl, ":");
ss = null;
s = null;
ss = st.nextToken();
s = st.nextToken();
vector.removeElement(s);
jList1.setListData(vector);
continue;
}
jTextArea1.append(sl + "\n");
}
}
public void jButton1_actionPerformed(ActionEvent e) {
setClection2();
}
public void jButton2_actionPerformed(ActionEvent e) {
jTextArea1.setText("");
}
public void jTextArea2_keyPressed(KeyEvent e) {
if(e.isControlDown() &&e.getKeyCode()==KeyEvent.VK_ENTER ){
setClection2();
}
}
}
class Frame1_jTextArea2_keyAdapter extends KeyAdapter {
private Frame1 adaptee;
Frame1_jTextArea2_keyAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.jTextArea2_keyPressed(e);
}
}
class Frame1_jButton2_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
//主程序
public class Application1 {
boolean packFrame = false;
/**
* Construct and show the application.
*/
public Application1() {
Frame1 frame = new Frame1();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
} else {
frame.validate();
}
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
/**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
new Application1();
}
});
}
}
//
服务器
import java.io.*;
import java.net.*;
import java.util.*;
public class Server extends ServerSocket
{
private static final int SERVER_PORT = 10000;
Vector vector1 =new Vector();
Vector vector2 =new Vector();
public Server() throws IOException
{
super(SERVER_PORT);
try
{
while (true)
{
Socket socket = accept();
new CreateServerThread(socket,vector1,vector2);
}
}
catch (IOException e)
{}
finally
{
close();
}
}
public static void main(String[] args) throws IOException
{
new Server();
}
}
//--- CreateServerThread
class CreateServerThread extends Thread
{
Socket client;
DataInputStream in;
DataOutputStream out;
Vector vector1;
Vector vector2;
public boolean bool=false;
String ss=null;
String string=null;
int iii;
String str=null;
Enumeration enu;
public CreateServerThread(Socket s,Vector vec1,Vector vec2) throws IOException
{
client = s;
vector1=vec1;
vector2=vec2;
out=new DataOutputStream(client.getOutputStream());
in=new DataInputStream(client.getInputStream());
start();
}
public void run()
{
StringTokenizer st;
StringTokenizer stc;
try
{
while (true)
{
ss=in.readUTF();
if(ss.startsWith("新的朋友")){
if(vector1.contains(str)){ iii=vector1.indexOf(str);
vector1.remove(iii);vector2.removeElement(this);}
if(vector1.contains(ss) ){
out.writeUTF("不可以");
}
else{
out.writeUTF("可以");
str=ss;
vector1.add(ss);
Enumeration enu=vector1.elements();
while(enu.hasMoreElements()){
out.writeUTF((String)enu.nextElement());
}
bool=true;
Enumeration enuc=vector2.elements();
while(enuc.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enuc.nextElement();
th.out.writeUTF(ss);
}
stc=new StringTokenizer(ss,":");
string=stc.nextToken();
string=stc.nextToken();
Enumeration enuc1=vector2.elements();
while(enuc1.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enuc1.nextElement();
th.out.writeUTF(string+"...上线了");
}
vector2.add(this);
}
}
else if(ss.startsWith("下线了")||client.isClosed()){
st=new StringTokenizer(ss,":");
string=st.nextToken();
string=st.nextToken();
iii=vector1.indexOf(str);
vector1.remove(iii);
Enumeration enu=vector2.elements();
while(enu.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enu.nextElement();
if(th!=this&&th.isAlive())
th.out.writeUTF("下线了:"+str.substring(str.indexOf(":")+1));
th.out.writeUTF(str.substring(str.indexOf(":")+1)+"...下线了");
}
vector2.remove(this);
break;
}
else{
enu=vector2.elements();
while(enu.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enu.nextElement();
th.out.writeUTF(ss);
}
}
}
}
catch (IOException e)
{
try{
iii=vector1.indexOf(str);
vector1.remove(iii);
Enumeration enun=vector2.elements();
while(enun.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enun.nextElement();
if(th!=this&&th.isAlive())
th.out.writeUTF("下线了:"+str.substring(str.indexOf(":")+1));
th.out.writeUTF(str.substring(str.indexOf(":")+1)+"...下线了");
}
vector2.remove(this);
return;
}catch(Exception ee){return;}
}
}
以上是“Java实现的c/s的聊天室[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |