⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chat_server.java

📁 一个用java编写的网络聊天软件 里边含有设计报告和源代码等可用的文件!! 开发环境是eclips
💻 JAVA
字号:
package chat1;


import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

import java.io.*;
import java.net.*;

 public class Chat_server extends JFrame implements ActionListener
 {
	
	private static final long serialVersionUID = 1L;

	
	private ServerSocket toserver;
	private Socket server;
	private ObjectInputStream in;
	private ObjectOutputStream out;
	private int counter = 1;
	private String ss[]={"宋体","楷体","华文行楷","新宋体"};
	
	//static Chat_server  chat; 
	
	
	JMenuBar jmb1;
	JToolBar jtb1;
	JToolBar jtb2;
	JButton jm1;
	JButton jm2;
	JButton jm3;
	JButton jm4;
	JButton selfout;
    JButton selcolor;
    JButton back1;
    JButton back2;
    JButton selface;
    JButton selbg;
    JButton selsound;
	JPanel jp1;
	JPanel jp2;
	JList jl;
	JLabel label;
	JTextArea jta1;
	JTextField jtf;
	Container con;
	JSeparator js1;
	JSeparator js2;
	Color color;
	BufferedImage bufimage;
	Icon bg1;
    Icon bg2;
    Icon bg3;
    Icon bg4;
    Dimension size;
    Font font;
    JComboBox jcb;
    
	public  Chat_server()
	{
	   con =  this.getContentPane();
	  
	   jp1 = new JPanel();
	   jp2 = new JPanel();
	   jp1.setLayout(new BorderLayout());
	   jp2.setLayout(new BorderLayout());
	   
	
	   
	   
	   jta1=new JTextArea();
	   jtf = new JTextField("Please input:");
	   jtf.setFont(font);
	   jta1.setBackground(Color.LIGHT_GRAY);
	   jtf.setBackground(Color.LIGHT_GRAY);
	   jta1.setEditable(false);
	   //jta1.setEnabled(true);
	  // jtf.setEditable(true);
	   jtf.addActionListener(this);
	   
	   
	   
	   jmb1 = new JMenuBar();
	   jmb1.setBackground(Color.pink);
	   jtb1 = new JToolBar();
	   jtb1.setBackground(Color.pink);
       js1 = new JSeparator();
       js2 = new JSeparator();
       jl  = new JList();
	   label = new JLabel("制作人:Jimmy");
       label.setForeground(Color.BLUE);
       
       jtb2 = new JToolBar();
       jtb2.setBackground(Color.pink);
       jtb2.add(label);
	   
       jp2.add(jtf);
       jp2.add(jtb1,"North");
       jp2.add(jtb2,"South");
	   
	   jm1 = new JButton("上线");
	   jm1.addActionListener(this);
	    
	   jm1.setBackground(Color.orange);
	   jmb1.add(jm1);
	   
	   jm2 = new JButton("离线");
	   jm2.addActionListener(this);
	   jm2.setBackground(Color.orange);
	   
	   jmb1.add(jm2);
	   jm3 = new JButton("隐身");
	   jm3.setBackground(Color.orange);
	   jmb1.add(jm3);
	   jm4 = new JButton("帮助");
	   jm4.setBackground(Color.orange);
	   jm4.addActionListener(this);
	   jmb1.add(jm4);
	   
	   
	   jcb = new JComboBox(ss);
	   jcb.addItemListener(new ItemListener(){

		public void itemStateChanged(ItemEvent e) {
			// TODO 自动生成方法存根
			String st = e.getSource().toString();
			font = new Font(st,Font.PLAIN,12);
		}
		   
	   });
	   jcb.setBackground(Color.yellow);
	   
	   selface = new JButton("表情选择");
	   selface.setBackground(Color.orange);
	   selface.addActionListener(this);
	   selbg = new JButton("情景选择");
	   selbg.setBackground(Color.orange);
	   selbg.addActionListener(this);
	   selfout = new JButton("字体选择");
	   selfout.setBackground(Color.orange);
	   selfout.addActionListener(this);
	   selcolor = new JButton("字体颜色");
	   selcolor.setBackground(Color.orange);
	   selcolor.addActionListener(this);
	   selsound = new JButton("选择音乐");
	   selsound.addActionListener(this);
	   selsound.setBackground(Color.orange);
	   jtb1.add(selcolor);
	   jtb1.add(jl);
	   jtb1.add(selface);
	   jtb1.add(selbg);
	   jtb1.add(selsound);
	   jtb1.add(selfout);
	   jtb1.add(jcb);
	  
	
	   
	   bg1 = new ImageIcon("micky.jpg");
	   bg2 = new ImageIcon("23.jpg");
	   back1 = new JButton();
	   back2 = new JButton();
	   back1.setIcon(bg1);
	   back2.setIcon(bg2);
	   back1.setBackground(Color.pink);
	   back2.setBackground(Color.pink);
	   jp1.add(back1,"North");
	   jp1.add(js2);
	   jp1.add(back2,"South");
        	
	   
	   
	   con.setLayout(new BorderLayout());
	   con.add(jmb1,"North");
	   con.add(jp2,"South");
	   con.add(jp1,"East"); 
	   con.add( new JScrollPane(jta1));
	   
	   
	   size = this.getToolkit().getScreenSize();
	   this.setJMenuBar(jmb1);
	   this.setIconImage(getToolkit().getImage("qq.jpg"));
	   this.setBackground(Color.orange);
	   this.setSize(size.width - 300, size.height /2 +27);
	   this.setResizable(false);
	   this.setLocation(100,220);
	   this.setTitle("Welcome to the chat room !");
	   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	   this.setVisible(true);
	   
	 
    }  
	
	
	
	public static void main(String[] args) 
	{
		Chat_server aa=new Chat_server();
		// TODO Auto-generated method stub
		aa.connectServer();

	}
	
	
	public void actionPerformed(ActionEvent evt) {
		
		
		if(evt.getActionCommand().equals("字体颜色") ){
              color = JColorChooser.showDialog(null, "请选择颜色", null);		   	
			
		}
		
		if(evt.getActionCommand().equals("字体选择")){
		      	
			  JOptionPane.showMessageDialog(null,"Please choose the style in the right list !"); 
			 
			
		}
		
		if(evt.getActionCommand().equals("情景选择")){
			  JOptionPane.showMessageDialog(null, "Sorry,you should use the tolerant background !");
			
		}
		
		if(evt.getActionCommand().equals("选择音乐")){
			
			  JOptionPane.showMessageDialog(null, "Sorry,the function is down");
			  
		}
		
		if(evt.getSource()==selface){
			  
			  JOptionPane.showMessageDialog(null, "Sorry ");
			
		}
		
		if(evt.getSource()==jm4){
			JOptionPane.showMessageDialog(null, "这是一个基于局域网的聊天软件," +"\n"+
					"所以请在同一局域网中运行。" +"\n"+
					"如果出现不能连接的情况,请关闭Client端," +"\n"+
					"在打开server端的情况下运行客户端!" +"\n"+
					"如果还不能连接,请查看源程序!");
		}
		
		if(evt.getSource()==jtf){
			if(jtf.getText().trim().length()>0)
			{
			   //String st=jtf.getText();
			   //font = new Font(st,100 ,100);
			   
			  // jta1.append("\n" + st);
			   jta1.setForeground(color);
			   
			  // jtf.setText("");
			   jtf.setForeground(color);
			   
		    }
			sendMsg(jtf.getText());
		}

		if (evt.getSource()==jm1) {
			
			try {
				waitConnection();
			} catch (IOException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			} 
			
			 
		}
		
		if (evt.getSource() == jm2) {
			jm1.setEnabled(false);
			jm2.setEnabled(true);
			
			//try 
			//{
			//	server.close();
			//}
			//catch (IOException e) 
			//{
			//   jta1.append("Can't close the ServerSocket!\n");
			//}
			
			try {
				closeConnection();
			} catch (IOException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		// TODO 自动生成方法存根
		
	}

	

	
	
	 public void connectServer(){
	    	try{
	    		toserver = new ServerSocket(6789);
	    		while(true){
	    			waitConnection();
	    			getStreams();
	    			processConnection();
	    			closeConnection();
	    			++counter;
	    		}
	    	}
	    	
	    	catch(EOFException eofException){
	    		System.out.println("Client terminated connection");
	    	}
	    	catch(IOException ioException){
	    		ioException.printStackTrace();
	    	}
	    	
	   }
	 
	 private void waitConnection() throws IOException{
	    	jta1.append("等待连接....");
	    	server = toserver.accept();
	    	
	    	jta1.append("Connection"+counter+"from:"+server.getInetAddress().getHostName());
	    	
	    	
	    }
	 
	 private void getStreams() throws IOException{
	    	
	    	out = new ObjectOutputStream(server.getOutputStream());
	    	//out.flush();
	    	in = new ObjectInputStream(server.getInputStream());
	    	
	    	jta1.append("\nPlease input ...\n");
	    }
	
	 
	 private void processConnection()throws IOException{
	    	String message = "";
	    	String message1=null;
	    	out.writeObject(message);
	    	
	    	out.flush();
	    	
	    	//jtf.setEnabled(true);
	    	do{
	    		try{
	    			message1 = (String)in.readObject();
	    			jta1.append("\n"+message1);
	    			jta1.setCaretPosition(jta1.getText().length());
	    		}
	    		catch (ClassNotFoundException classNotFoundException){
	    			jta1.append("\nUnknow object type received");
	    		}
	    	}
	    	while(!message1.equals("客户端>>TERMINATE"));
	    	
	    	}
	 
	 private void closeConnection() throws IOException{
	    	jta1.append("\nUser lose the connection");
	    	jtf.setEnabled(false);
	    	out.close();
	    	in.close();
	    	server.close();
	    }
	 
	 private void sendMsg(String message){
    	try{
    		out.writeObject("Server:  \n"+message);
    		out.flush();
    		jta1.append("\nServer:  \n"+message);
    		jtf.setText("");
    		
    	}
    	catch(IOException ioExcepotion){
    		jta1.append("\nError writing object");
    	}
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -