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

📄 server.java

📁 一个简单的socket聊天工具,实现在发送文件和语音聊天等功能,还能聊天的同时听音乐,分服务器端和客户端两部分
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on 2006-2-26
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author 之诸暇
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
//Java Document
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Window;
import javax.swing.*;

import java.util.*;

public class Server implements ActionListener,ItemListener,MouseListener{//,KeyListener{
	private JFrame myframe ;
	private JTextPane enterArea;
	private JTextArea displayArea;
	private JButton chat,mess,sendButton,receiveButton,retalkButton;
	private Box baseBox,box;
	private ObjectOutputStream output;
	private ObjectInputStream input;
	private ServerSocket server;
	private Socket connect,to;
	private String chatServer,client;
	private int counter=1;
	java.awt.Window windowSplash;
	
	/******************菜单项按钮***************/
	private JMenu fileMenu,mediaMenu,editMenu,fontMenu,colorMenu;
	private JPopupMenu popupMenu; 
	
	private JMenuItem exitItem,openItem,saveItem,sendItem,connectItem;
	private JMenuItem talkItem,receiveItem;
	private JMenuItem mouseItem1,mouseItem2;
	public JCheckBoxMenuItem boldItem,italicItem;
	private JRadioButtonMenuItem redItem,greenItem,blueItem;
	private ButtonGroup group;
	private JMenuBar menuBar;
	/*****************************/
	private OpenFile openFile;
	
	
	/****************条件变量*****************/
	public static StringBuffer nickName;
	public static String myShow;
	public static String otherShow;
	public static Color mycolor;
	public static boolean state = false;
	
	
	public Server(){//prepareSplash();startSplash();
		myframe = new JFrame ("聊天器");
		Container container=myframe.getContentPane();
		Box baseBox=Box.createHorizontalBox();//先产生水平排列方式的Box组件,当作基底容器(BaseBox)
	    container.add(baseBox);
	    
		enterArea=new JTextPane();
		enterArea.setSize(5,15);
		enterArea.setEnabled(false);
	
		sendButton=new JButton("发送");
		sendButton.setEnabled(true);
		sendButton.addActionListener(
			new ActionListener(){
			public void actionPerformed(ActionEvent event){
			if(enterArea.getText().equals("")){
				JOptionPane.showMessageDialog(myframe,"消息不能为空!","警告",JOptionPane.WARNING_MESSAGE);
				return;
			}
			else{
				sendData(enterArea.getText(),0);
				enterArea.setText("");
			}
		}//action
			public void keyPressed (KeyEvent e){
				int code = e.getKeyCode();
				if(code == e.VK_ENTER&&e.isControlDown())
				if(enterArea.getText().equals("")){
					JOptionPane.showMessageDialog(myframe,"消息不能为空!","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				else{
					sendData(enterArea.getText(),0);
					enterArea.setText("");
				}
			}
	});
		
		receiveButton = new JButton("接收文件");
		receiveButton.addActionListener(this);
		receiveButton.setVisible(false);
		retalkButton = new JButton("接收聊天");
		retalkButton.addActionListener(this);
		retalkButton.setVisible(false);
		
		/************************播放器部分***************/
		Media media = new Media(myframe);
		JMenuItem mi = new JMenuItem ("打开");
		mi.addActionListener (media);
		JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem ("循环",false);
		cbmi.addItemListener (media); 
		JMenuItem ex = new JMenuItem ("退出");
		ex.addActionListener (media);
		
		/***********************建立菜单项*********************/
		//创建菜单
		fileMenu = new JMenu ("文件(F)");
		fileMenu.setMnemonic('F');
		mediaMenu = new JMenu ("影音(M)");
		
		editMenu =new JMenu ("编辑(O)");
		editMenu.setMnemonic('O');
		fontMenu = new JMenu ("字体(S)");
		fontMenu.setMnemonic('S');
		colorMenu = new JMenu ("颜色(C)");
		colorMenu.setMnemonic('S');
		popupMenu = new JPopupMenu();
		
		//创建菜单项
		exitItem = new JMenuItem ("退出");
		exitItem.setMnemonic('E');
		openItem = new JMenuItem("打开");
		saveItem = new JMenuItem("保存聊天记录");
		sendItem = new JMenuItem("发送文件");
		connectItem = new JMenuItem("连接");
		
		talkItem = new JMenuItem("发送聊天");
		//receiveItem = new JMenuItem("接受聊天");
		
		boldItem = new JCheckBoxMenuItem ("粗体(B)");
		boldItem.setMnemonic('B');
		italicItem = new  JCheckBoxMenuItem ("斜体(I)");
		italicItem.setMnemonic('I');
		redItem = new JRadioButtonMenuItem ("红色");
		greenItem = new JRadioButtonMenuItem ("绿色");
		blueItem = new JRadioButtonMenuItem ("蓝色");
		group = new ButtonGroup ();
		group.add(redItem);
		group.add(greenItem);
		group.add(blueItem);
		
		mouseItem1 = new JCheckBoxMenuItem("离开",false);
		mouseItem2 = new JMenuItem("隐身");
		
		//对菜单进行监听
		//MenuAction action = new MenuAction(myColor,myFont,displayArea,boldItem);
		
		//openItem.addActionListener (new Mycolor(mycolor));
		saveItem.addActionListener (this);
		sendItem.addActionListener (this);
		connectItem.addActionListener (this);
		
		talkItem.addActionListener(this);
		//receiveItem.addActionListener(this);
		
		exitItem.addActionListener (this);
		boldItem.addItemListener(this);
		italicItem.addItemListener(this);
		redItem.addActionListener(this);
		greenItem.addActionListener(this);
		blueItem.addActionListener(this);
		
		mouseItem1.addActionListener(this);
		mouseItem2.addActionListener(this);
		
		//将菜单项添加到菜单中
		fileMenu.add(exitItem);
		fileMenu.add(openItem);
		fileMenu.add(saveItem);
		fileMenu.add(sendItem);
		fileMenu.add(connectItem);
		
		mediaMenu.add(talkItem);
		//mediaMenu.add(receiveItem);
		mediaMenu.add(mi);
		mediaMenu.add (cbmi);
		mediaMenu.add(ex);
		
		editMenu.add(boldItem);
		editMenu.add(italicItem);
		editMenu.add(fontMenu);
		fontMenu.add(boldItem);
		fontMenu.add(italicItem);
		editMenu.add(colorMenu);
		colorMenu.add(redItem);
		colorMenu.add(greenItem);
		colorMenu.add(blueItem);
		
		popupMenu.add(mouseItem1);
		popupMenu.add(mouseItem2);
		
		//建立菜单栏
		menuBar = new JMenuBar ();
		menuBar.add(fileMenu);
		menuBar.add(mediaMenu);
		menuBar.add(editMenu);
		myframe.setJMenuBar(menuBar);
		/*******************菜单项结束***************/
		
		
		nickName = new StringBuffer ("none");
				
		displayArea=new JTextArea(10,15);
		displayArea.add(popupMenu);
		displayArea.addMouseListener(this);
		displayArea.setBorder(BorderFactory.createLineBorder(Color.black));
		displayArea.setFont(new Font("Serif",Font.BOLD,14));
		displayArea.setForeground(Color.blue);
		displayArea.setEnabled(true);
		displayArea.setEditable(false);

		
		/******************选择昵称按钮*******************/
		JButton nickButton = new JButton("选择昵称");
		Myaction change = new Myaction(myframe,nickName);
		nickButton.addActionListener(change);
		/**********************************************/
		
		/********************聊天模式选择**************/
		chat = new JButton("聊天模式");
		mess = new JButton("消息模式");
		chat.setVisible(false);
		mess.setVisible(true);
		chat.addActionListener(this);
		mess.addActionListener(this);
		
		box=Box.createHorizontalBox();
		box.add(chat);
		box.add(mess);
		//box.setBorder(BorderFactory.createTitledBorder("请输入聊天信息"));
		box.add(nickButton);
		//box.add(new JScrollPane(enterArea));
		box.add(sendButton);
		/*产生垂直排列方式的Box组件来安置第一与第三个按钮
	       */
	    Box vBox1 = Box.createVerticalBox();
	    vBox1.add(new JScrollPane(displayArea));
	    vBox1.add(new JScrollPane(enterArea));
	    vBox1.add(box);
	    baseBox.add(vBox1);

	    Box vBox2=Box.createVerticalBox();
	    baseBox.add(vBox2);
	    vBox2.add(new JLabel("对方形象",SwingConstants.LEFT));
	    vBox2.add(new ImagePanel ("b.gif"));
	    vBox2.add(receiveButton);
	    vBox2.add(retalkButton);
	    vBox2.add(media);
	    media.setVisible(false);
	    vBox2.add(new JLabel("我的形象",SwingConstants.LEFT));
	    vBox2.add(new ImagePanel ("b.gif"));
	    
		myframe.setSize(500,400);
		myframe.setLocation(300,200);
		myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myframe.setVisible(true);//stopSplash();
	}
	
	
	
	
	
	
	
	

⌨️ 快捷键说明

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