聊天室java

最近写了一段聊天室java代码

/*  dkplus专业搜集和编写实用电脑软件教程,搜集各种软件资源和计算机周边(java网络编程,seo网站优化,web开发,lnmp,java网络编程,毕业论文设计),独立制作视频和ppt和音频微信公众号,点击进入 dkplus官方博客http://dkplus.iteye.com 微信搜索dkplus关注公众号可获取海量计算机周边资源。 */  
package com.server;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
 
import javax.swing.*;
 
public class ServerWindow extends JFrame{
     
    @SuppressWarnings("unchecked")
    public ServerWindow()
    {
        Server ss = new Server();
        Thread t = new Thread(ss);
        t.start();
         
        dialogArea = new JTextArea(11,47);
        dialogArea.setLineWrap(true);
        dialogArea.setFont(messageFont);
        dialogScrollPane = new JScrollPane(dialogArea);
        dialogPanel = new JPanel();
        dialogPanel.add(dialogScrollPane);
         
        speedInput = new JComboBox(new String[]{
                "What can I do for you, my son?",
                "Oh,sorry,son,I cann't do that for you."
        });
        speedInput.setActionCommand("speedInput");
         
        Action action = new Action();
        speedInput.addActionListener(action);
         
        comboxPanel = new JPanel();
        comboxPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        comboxPanel.setBorder(BorderFactory.createEtchedBorder());
        comboxPanel.add(speedInput);
         
        panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());
        panel1.add(dialogPanel,BorderLayout.CENTER);
         
        inputArea = new JTextArea(11,47);
        inputArea.setLineWrap(true);//设置自动换行
        inputArea.setFont(messageFont);
        inputPanel = new JPanel();
        inputPanel.add(inputArea);
         
         
        enterButton = new JButton("Enter");
        enterButton.setActionCommand("enterButton");
        enterButton.addActionListener(action);
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(enterButton);
         
        panel2 = new JPanel();
        panel2.setLayout(new BorderLayout());
        panel2.add(comboxPanel,BorderLayout.NORTH);
        panel2.add(inputPanel,BorderLayout.CENTER);
        panel2.add(buttonPanel,BorderLayout.SOUTH);
         
        setLayout(new GridLayout(2,1));
        add(panel1);
        add(panel2);
         
    }
 
     
//  class Server extends Thread{
        class Server implements Runnable{
         
        public void run(){
        try {
        try {
            server = new ServerSocket(6666);
            client = server.accept();
             
            InputStream isr = client.getInputStream();
            @SuppressWarnings("resource")
            Scanner scanner = new Scanner(isr);
//          while(scanner.hasNextLine())
//          {
                inputMessage = scanner.nextLine();  
//          }
                 
//          InputStreamReader isr = new InputStreamReader(client.getInputStream());
//          BufferedReader bf = new BufferedReader(isr);
//          inputMessage = bf.readLine();
            dialogArea.append("Client:\n"+inputMessage);
            System.out.println("服务器在监听");
             
            pw = new PrintWriter(client.getOutputStream(),true);
//          pw.println(clientMessage);
//          outputMessage = inputArea.getText();
            System.out.println("Stream output "+outputMessage+"\n");
//          pw.println(outputMessage);
//          pw.write(outputMessage);
             
             
        }
        finally{
//          server.close();
//          client.close();
        }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
                     
         
        }
         
        private ServerSocket server = null;
        private Socket client = null;
    }
 
     
     
    class Action implements ActionListener
    {
         
        public void actionPerformed(ActionEvent e) {
             
            if(e.getActionCommand() == "enterButton"){
            outputMessage = inputArea.getText();
            System.out.println("outputMessageByenterButton"+outputMessage+"\n");//测试语句可以得到该语句
            dialogArea.append("Server:\n"+outputMessage+"\n");
            inputArea.setText(null);//将输入域清屏
             
            }else if(e.getActionCommand() == "speedInput"){
         
                outputMessage = (String)speedInput.getSelectedItem();
                inputArea.setText(outputMessage);
                System.out.println("inputAreaByspeedInput  outputMessage = "+outputMessage+"\n");
                pw.write(outputMessage);
            }
             
        }
         
    }
     
    private PrintWriter pw = null;
    private static String inputMessage = null;//显示到dialogArea的信息
    private static String outputMessage = null;
     
    JTextArea dialogArea = null;
    JTextArea inputArea = null;
    JScrollPane dialogScrollPane = null;
    JComboBox speedInput = null;
    JButton enterButton = null;
    JPanel dialogPanel = null,comboxPanel = null,inputPanel = null,buttonPanel = null;
    JPanel panel1 = null,panel2 = null;
    Font messageFont = new Font("仿宋",Font.BOLD,16);
//  Font nameFont = new Font("仿宋",Font.BOLD,14);
}

 

/*  dkplus专业搜集和编写实用电脑软件教程,搜集各种软件资源和计算机周边(java网络编程,seo网站优化,web开发,lnmp,java网络编程,毕业论文设计),独立制作视频和ppt和音频微信公众号,点击进入 dkplus官方博客http://dkplus.iteye.com 微信搜索dkplus关注公众号可获取海量计算机周边资源。 */  
package com.server;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
 
import javax.swing.*;
 
public class ServerWindow extends JFrame{
     
    @SuppressWarnings("unchecked")
    public ServerWindow()
    {
        Server ss = new Server();
        Thread t = new Thread(ss);
        t.start();
         
        dialogArea = new JTextArea(11,47);
        dialogArea.setLineWrap(true);
        dialogArea.setFont(messageFont);
        dialogScrollPane = new JScrollPane(dialogArea);
        dialogPanel = new JPanel();
        dialogPanel.add(dialogScrollPane);
         
        speedInput = new JComboBox(new String[]{
                "What can I do for you, my son?",
                "Oh,sorry,son,I cann't do that for you."
        });
        speedInput.setActionCommand("speedInput");
         
        Action action = new Action();
        speedInput.addActionListener(action);
         
        comboxPanel = new JPanel();
        comboxPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        comboxPanel.setBorder(BorderFactory.createEtchedBorder());
        comboxPanel.add(speedInput);
         
        panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());
        panel1.add(dialogPanel,BorderLayout.CENTER);
         
        inputArea = new JTextArea(11,47);
        inputArea.setLineWrap(true);//设置自动换行
        inputArea.setFont(messageFont);
        inputPanel = new JPanel();
        inputPanel.add(inputArea);
         
         
        enterButton = new JButton("Enter");
        enterButton.setActionCommand("enterButton");
        enterButton.addActionListener(action);
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(enterButton);
         
        panel2 = new JPanel();
        panel2.setLayout(new BorderLayout());
        panel2.add(comboxPanel,BorderLayout.NORTH);
        panel2.add(inputPanel,BorderLayout.CENTER);
        panel2.add(buttonPanel,BorderLayout.SOUTH);
         
        setLayout(new GridLayout(2,1));
        add(panel1);
        add(panel2);
         
    }
 
     
//  class Server extends Thread{
        class Server implements Runnable{
         
        public void run(){
        try {
        try {
            server = new ServerSocket(6666);
            client = server.accept();
             
            InputStream isr = client.getInputStream();
            @SuppressWarnings("resource")
            Scanner scanner = new Scanner(isr);
//          while(scanner.hasNextLine())
//          {
                inputMessage = scanner.nextLine();  
//          }
                 
//          InputStreamReader isr = new InputStreamReader(client.getInputStream());
//          BufferedReader bf = new BufferedReader(isr);
//          inputMessage = bf.readLine();
            dialogArea.append("Client:\n"+inputMessage);
            System.out.println("服务器在监听");
             
            pw = new PrintWriter(client.getOutputStream(),true);
//          pw.println(clientMessage);
//          outputMessage = inputArea.getText();
            System.out.println("Stream output "+outputMessage+"\n");
//          pw.println(outputMessage);
//          pw.write(outputMessage);
             
             
        }
        finally{
//          server.close();
//          client.close();
        }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
                     
         
        }
         
        private ServerSocket server = null;
        private Socket client = null;
    }
 
     
     
    class Action implements ActionListener
    {
         
        public void actionPerformed(ActionEvent e) {
             
            if(e.getActionCommand() == "enterButton"){
            outputMessage = inputArea.getText();
            System.out.println("outputMessageByenterButton"+outputMessage+"\n");//测试语句可以得到该语句
            dialogArea.append("Server:\n"+outputMessage+"\n");
            inputArea.setText(null);//将输入域清屏
             
            }else if(e.getActionCommand() == "speedInput"){
         
                outputMessage = (String)speedInput.getSelectedItem();
                inputArea.setText(outputMessage);
                System.out.println("inputAreaByspeedInput  outputMessage = "+outputMessage+"\n");
                pw.write(outputMessage);
            }
             
        }
         
    }
     
    private PrintWriter pw = null;
    private static String inputMessage = null;//显示到dialogArea的信息
    private static String outputMessage = null;
     
    JTextArea dialogArea = null;
    JTextArea inputArea = null;
    JScrollPane dialogScrollPane = null;
    JComboBox speedInput = null;
    JButton enterButton = null;
    JPanel dialogPanel = null,comboxPanel = null,inputPanel = null,buttonPanel = null;
    JPanel panel1 = null,panel2 = null;
    Font messageFont = new Font("仿宋",Font.BOLD,16);
//  Font nameFont = new Font("仿宋",Font.BOLD,14);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值