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

📄 clientframe.java

📁 简易聊天工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;
import javax.swing.border.*;
import javax.media.*;
import javax.media.format.*;
public class ClientFrame extends JFrame
{
	Socket soc = null;
	Vector ChatFriends = new Vector();
	AudioFormat audioFormat = null;
	VideoFormat videoFormat = null;
	CaptureDeviceInfo audioDevice = null;
	CaptureDeviceInfo videoDevice = null;
	PrintWriter out = null; 
	ClientRecieveMsg listen = null;
	String name = null;
	String IP = null;
	String flag = "public";
	String sendToName = "所有人";
	String wordType = null;
	int wSize;
	String wFace;
	Color selectedColor = Color.BLACK;
	JLabel userName;
	JTextPane chatRecord;
	StyledDocument recordDoc;
	JLabel jlb1;
	JLabel sendToLabel;
	JComboBox sendTo;
	ButtonGroup group1;
	JRadioButton chatPublic;
	JRadioButton chatSecret;
	JButton wordColor;
	JLabel wordSizeLabel; 
	JComboBox wordSize;
	JLabel wordFaceLabel;
	JComboBox wordFace;
	ButtonGroup group2;
	JRadioButton bold;
	JRadioButton italic; 
	JLabel jlb2;
	JButton vedioChat;
	JButton audioChat;
	JButton netMeeting;
	JTextPane chatWords;
	
	JLabel jlb3;
	JButton sendMsg;
	JButton exit;
	
	public ClientFrame(String name)
	{
		this.name = name;
		userName = new JLabel("姓名:    " + name);
		userName.setBounds(new Rectangle(15, 5, 500, 20));
		
		chatRecord = new JTextPane();
		chatRecord.setDragEnabled(true);
		chatRecord.setEditable(false);
		JScrollPane cr = new JScrollPane();
		cr.getViewport().add(chatRecord);
		cr.setBounds(new Rectangle(10, 30, 500, 195));
		recordDoc = (StyledDocument)chatRecord.getDocument();
		//将预定义的字体格式加入格式化文档对象中   
        Style style = recordDoc.addStyle("bobo", null);
        StyleConstants.setItalic(style, false); 
        StyleConstants.setBold(style, true);   
        StyleConstants.setFontFamily(style, "Dialog");   
        StyleConstants.setFontSize(style, 12);
        StyleConstants.setForeground(style, Color.RED);
        style = recordDoc.addStyle("zhuzhu", null);
		StyleConstants.setForeground(style, Color.BLUE);
		StyleConstants.setBold(style, true);
		StyleConstants.setFontFamily(style, "Dialog");
		StyleConstants.setFontSize(style, 12);
		StyleConstants.setItalic(style, false);
		
		style = recordDoc.addStyle("haha", null);
		StyleConstants.setForeground(style, Color.BLACK);
		StyleConstants.setBold(style, true);
		StyleConstants.setFontFamily(style, "Dialog");
		StyleConstants.setFontSize(style, 12);
		StyleConstants.setItalic(style, true);
		
		style = recordDoc.addStyle("xitong", null);
		StyleConstants.setForeground(style, new Color(255, 155,55));
		StyleConstants.setBold(style, true);
		StyleConstants.setFontFamily(style, "Dialog");
		StyleConstants.setFontSize(style, 12);
		StyleConstants.setItalic(style, false);
		try
		{
			recordDoc.insertString(recordDoc.getLength(), "                     Welcome to BITI-Chat!" + "\n", style);
    	} catch (BadLocationException e) {}

		
		jlb1 = new JLabel();
		jlb1.setBorder(BorderFactory.createEtchedBorder());
		jlb1.setBounds(new Rectangle(10, 235, 500, 60));
		sendToLabel = new JLabel("发送至:");
		sendToLabel.setBounds(new Rectangle(20, 240, 60, 20));
		sendTo = new JComboBox(new String[]
		{
			"所有人", "服务器"	
		});;
		sendTo.setBounds(new Rectangle(80, 240, 100, 20));
		sendTo.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				select_ActionPerformed(event);	
			}	
		});
		group1 = new ButtonGroup();
		chatPublic = new JRadioButton("公开", true);
		chatPublic.setBounds(new Rectangle(30, 270, 70, 20));
		chatPublic.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				flag = "public";	
			}	
		});
		group1.add(chatPublic);
		chatSecret = new JRadioButton("密聊", false);
		chatSecret.setBounds(new Rectangle(100, 270, 70, 20));
		chatSecret.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				flag = "secret";	
			}	
		});
		group1.add(chatSecret);
		wordColor = new JButton("字体颜色...");
		wordColor.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				select_ActionPerformed(event);	
			}	
		});
		wordColor.setBounds(new Rectangle(200, 240, 100, 20));
		wordSizeLabel = new JLabel("字体大小:"); 
		wordSizeLabel.setBounds(new Rectangle(320, 240, 80, 20));
		String[] s = new String[100];
		for(int i = 0; i < 88; i++)
		{
			s[i] = "" + (i + 12);	
		}
		wordSize = new JComboBox(s);
		wordSize.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				select_ActionPerformed(event);	
			}	
		});
		wordSize.setBounds(new Rectangle(400, 240, 100, 20));
		wordFaceLabel = new JLabel("字体类型:");
		wordFaceLabel.setBounds(new Rectangle(320, 270, 80, 20));
		s = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
		wordFace = new JComboBox(s);
		wordFace.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				select_ActionPerformed(event);	
			}	
		});
		wordFace.setBounds(new Rectangle(400, 270, 100, 20));
		group2 = new ButtonGroup();
		bold = new JRadioButton("粗体", true);
		bold.setBounds(new Rectangle(170, 270, 70, 20));
		group2.add(bold);
		italic = new JRadioButton("斜体", false);
		italic.setBounds(new Rectangle(240, 270, 70, 20));
		group2.add(italic);
		
		jlb2 = new JLabel();
		jlb2.setBorder(BorderFactory.createEtchedBorder());
		jlb2.setBounds(new Rectangle(10, 300, 500, 40));
		vedioChat = new JButton("视频聊天...");
		vedioChat.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				vedioChat_ActionPerformed(event);	
			}		
		});				
		vedioChat.setBounds(new Rectangle(50, 310, 120, 20));
		audioChat = new JButton("语音聊天...");
		audioChat.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				audioChat_ActionPerformed(event);	
			}		
		});								
		audioChat.setBounds(new Rectangle(200, 310, 120, 20));
		netMeeting = new JButton("网络会议...");
		netMeeting.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				netMeeting_ActionPerformed();	
			}		
		});
		netMeeting.setBounds(new Rectangle(350, 310, 120, 20));			
		chatWords = new JTextPane();
		chatWords.setDragEnabled(true);
		chatWords.setEditable(true);
		JScrollPane	cw = new JScrollPane(chatWords);
		cw.setBounds(new Rectangle(10, 350, 500, 150));
		
		jlb3 = new JLabel();
		jlb3.setBorder(BorderFactory.createEtchedBorder());
		jlb3.setBounds(new Rectangle(10, 510, 500, 40));
		SendAction sendlistener = new SendAction("发  送");
    	sendMsg = new JButton(sendlistener);
    	InputMap imap = sendMsg.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    	imap.put(KeyStroke.getKeyStroke("ctrl ENTER"), "send-message");
    	ActionMap amap = sendMsg.getActionMap();
    	amap.put("send-message", sendlistener);								
		sendMsg.setBounds(new Rectangle(100, 520, 100, 20));
		exit = new JButton("退出聊天室");
		exit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				exit_ActionPerformed(event);	
			}		
		});										
		exit.setBounds(new Rectangle(300, 520, 100, 20));
		
		Container cp = getContentPane();
		cp.setLayout(null);
		cp.add(userName, null);
		cp.add(jlb1, null);
		cp.add(sendToLabel, null);
		cp.add(sendTo, null);
		cp.add(chatPublic, null);
		cp.add(chatSecret, null);
		cp.add(jlb2, null);
		cp.add(wordColor, null);
		cp.add(wordSizeLabel, null);
		cp.add(wordSize, null);
		cp.add(wordFaceLabel, null);
		cp.add(wordFace, null);
		cp.add(bold, null);
		cp.add(italic, null);
		cp.add(jlb3, null);
		cp.add(vedioChat, null);
		cp.add(audioChat, null);
		cp.add(netMeeting, null);
		cp.add(sendMsg, null);
		cp.add(exit, null);
		cp.add(cr, null);
		cp.add(cw, null);
		
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
      	int screenHeight = screenSize.height;
      	int screenWidth = screenSize.width;
		setLocation(screenWidth / 4, screenHeight / 10);
		setSize(530, 600);
		setTitle("聊天客服端");
		
		connect();		
	}
	void connect()
	{
		try
		{
			soc = new Socket(InetAddress.getLocalHost(), 8080);  // 使用端口8080实例化本地套接字
			System.out.println(soc);					   // 打印实例化结果
			out = new PrintWriter(
					new BufferedWriter(
						new OutputStreamWriter(soc.getOutputStream())));	   // 将out指向soc的输出流
			String inetAddress = InetAddress.getLocalHost().toString();
			StringTokenizer IAst = new StringTokenizer(inetAddress, "/");
			IAst.nextToken();
			String ip = IAst.nextToken();
			String userinfo = "INFO:" + name + ":" + ip;
			System.out.println(userinfo);                                                         // 将name和ip用":"分开
			sendMsg(userinfo);
			listen = new ClientRecieveMsg(this, name, soc);		// 实例化客户端线程
			listen.start();								// 启动线程
		}
		catch(IOException e)
		{
			 System.out.println("Error:" + e);
			 disconnect();
		}
	}
		
	void select_ActionPerformed(ActionEvent event)
	{
		if(event.getSource() == wordColor)
		{
			selectedColor = JColorChooser.showDialog(null,"选择字体颜色",Color.BLACK);
		}
		chatWords.setForeground(selectedColor);
		wSize = Integer.parseInt((String)wordSize.getSelectedItem());

⌨️ 快捷键说明

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