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

📄 clientframenew.java

📁 用java编译的一个qq程序。。。。。。。。
💻 JAVA
字号:
package com.chat.client;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.List;


import javax.swing.JButton;
import javax.swing.JColorChooser;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;



import com.chat.constants.ClientConstants;
import com.chat.dto.MsgDTO;
import com.chat.io.TxtManager;
import com.chat.util.FontDialog;


@SuppressWarnings("serial") 
public class ClientFramenew extends JFrame implements Runnable {
	

	//private DefaultComboBoxModel cbxModel =new DefaultComboBoxModel();


	// private InputStream in;
	// private OutputStream out; 

	private JTextPane txtDownMsg;

	private JTextPane txtUpMsg;
	
	private MsgDTO tempMsgDTO=new MsgDTO();

	private StyledDocument doc;


	
	private MainFramtest Main;

	public static void main(String args[]) {
		try {
			//ClientFramenew frame = new ClientFramenew(args[0],args[1]);
			//frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame
	 */
	public ClientFramenew() {
		super();
	}

	public ClientFramenew(String myuserId,String towhomid,MainFramtest mainframe) {
		super();
		
		this.tempMsgDTO.setUserId(myuserId);
		this.tempMsgDTO.setToWhere(towhomid);
		
		Main=mainframe;
		System.out.println(myuserId+"与"+towhomid+"对话");
		
		getContentPane().setLayout(null);
		setTitle("用户  "+this.tempMsgDTO.getUserId()+"  与用户: "+this.tempMsgDTO.getToWhere()+" 聊天中");
		setBounds(100, 100, 479, 471);
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		final JMenu menu = new JMenu();
		menu.setText("文件(F)");
		menuBar.add(menu);

		final JMenuItem menuItem = new JMenuItem();
		menuItem.setText("Menu Item");
		menu.add(menuItem);

		final JMenu menu_1 = new JMenu();
		menu_1.setText("编辑(E)");
		menuBar.add(menu_1);

		final JMenuItem menuItem_1 = new JMenuItem();
		menuItem_1.setText("Menu Item");
		menu_1.add(menuItem_1);

		final JMenu menu_2 = new JMenu();
		menu_2.setText("动作(V)");
		menuBar.add(menu_2);

		final JMenuItem menuItem_2 = new JMenuItem();
		menuItem_2.setText("Menu Item");
		menu_2.add(menuItem_2);

		final JMenu menu_3 = new JMenu();
		menu_3.setText("设置(T)");
		menuBar.add(menu_3);

		final JMenuItem menuItem_3 = new JMenuItem();
		menuItem_3.setText("Menu Item");
		menu_3.add(menuItem_3);

		final JMenu menu_4 = new JMenu();
		menu_4.setText("帮助(H)");
		menuBar.add(menu_4);

		final JMenuItem btnAbout = new JMenuItem();
		btnAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//About.main(new String[0]);
			}
		});
		btnAbout.setText("关于...");
		menu_4.add(btnAbout);

		final JDesktopPane desktopPane = new JDesktopPane();
		desktopPane.setBounds(0, 0, 512, 27);
		getContentPane().add(desktopPane);

		final JToolBar toolBar = new JToolBar();
		toolBar.setBounds(0, 0, 502, 27);
		desktopPane.add(toolBar);

		final JButton button = new JButton();
		button.setText("功能1");
		toolBar.add(button);

		toolBar.addSeparator();

		final JButton button_1 = new JButton();
		button_1.setText("功能2");
		toolBar.add(button_1);

		toolBar.addSeparator();

		final JButton button_2 = new JButton();
		toolBar.add(button_2);
		button_2.setText("功能3");

		toolBar.addSeparator();

		final JButton button_3 = new JButton();
		button_3.setText("功能4");
		toolBar.add(button_3);

		toolBar.addSeparator();

		final JButton button_5 = new JButton();
		button_5.setText("功能5");
		toolBar.add(button_5);

	/*	final JButton button_4 = new JButton();
		button_4.setText("New JButton");
		button_4.setBounds(283, 0, 99, 23);
		desktopPane.add(button_4);*/

		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 43, 442, 191);
		getContentPane().add(scrollPane);

		txtUpMsg = new JTextPane();
		txtUpMsg.setEditable(false);
		scrollPane.setViewportView(txtUpMsg);

		final JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 273, 442, 98);
		getContentPane().add(scrollPane_1);

		txtDownMsg = new JTextPane();
		scrollPane_1.setViewportView(txtDownMsg);

		final JButton btnFont = new JButton();
		btnFont.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getSelectedFont();
			}
		});
		btnFont.setText("字体");
		btnFont.setBounds(20, 240, 73, 23);
		getContentPane().add(btnFont);

		final JButton btnColor = new JButton();
		btnColor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getSelectedColor();
			}
		});
		btnColor.setText("颜色");
		btnColor.setBounds(360, 240, 81, 23);
		getContentPane().add(btnColor);

		final JButton button_8 = new JButton();
		button_8.setText("交谈记录");
		button_8.setBounds(10, 382, 109, 23);
		getContentPane().add(button_8);

		final JButton button_9 = new JButton();
		button_9.setText("消息模式");
		button_9.setBounds(125, 382, 109, 23);
		getContentPane().add(button_9);

		final JButton btnClose = new JButton();
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//Main.send(new String(ClientConstants.LOGOUT+ClientConstants.LOGIN_SPLIT+tempMsgDTO.getUserId()));
				dispose();
				//System.exit(0);
			}
		});
		btnClose.setText("关闭(C)");
		btnClose.setBounds(281, 382, 81, 23);
		getContentPane().add(btnClose);
		

		final JButton btnSend = new JButton();
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String content = txtDownMsg.getText();
				if (content.trim().equals(""))
					return;

				
				
				tempMsgDTO.setContent(content);
				//点击发送时把数据名发送到服务器
				if(Main.send(transferMsgDTO())){
					txtDownMsg.setText("");
					setMessageText(tempMsgDTO);
				}
			}
		});
		btnSend.setText("发送(S)");
		btnSend.setBounds(368, 382, 84, 23);
		getContentPane().add(btnSend);

		final JLabel label = new JLabel();
		label.setText("与 "+towhomid+" 对话");
		label.setBounds(178, 240, 109, 27);
		getContentPane().add(label);

		/*cbxUserId = new JComboBox();
		cbxUserId.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if   (e.getStateChange()   ==   ItemEvent.SELECTED)   {   //这里控制为只处理一次   
					tempMsgDTO.setToWhere(cbxModel.getSelectedItem().toString());
		          }   
			}
		});
		cbxUserId.setBounds(46, 244, 142, 23);
		getContentPane().add(cbxUserId);*/

		// my methods
 
		new Thread(this).start();
		this.setVisible(true);
	}

	

	/*private boolean cbxContains(String item){//!!
		int count=this.cbxModel.getSize();
		for(int i=0;i<count;i++){
			if(this.cbxModel.getElementAt(i).equals(item)){
				return true;
			}
		}
		return false;
	}*/
	@SuppressWarnings("unchecked")
	
	//要重写
	public void run() {
		while(true){
			try{
				Thread.sleep(500);
			}catch(Exception e){System.out.println(e.getMessage());}
		for(int i=0;i<MainFramtest.ms.size();i++){
			if(MainFramtest.ms.get(i).getUserId().equals(this.tempMsgDTO.getToWhere())){
				setMessageText(MainFramtest.ms.get(i));
				MainFramtest.ms.remove(i);
				
				
			}
				System.out.println("当前链表大小"+MainFramtest.ms.size());
				System.out.println("本对话面板的towhere为"+this.tempMsgDTO.getToWhere());
				//System.out.println("当前链表项 towhere:"+MainFramtest.ms.get(i).getToWhere()+
						//";\nid:"+MainFramtest.ms.get(i).getUserId());
				System.out.println("本对话面板的towhere yu 当前链表项 id");
		}
		}

	}

	private void getSelectedFont() {
		FontDialog dialog = new FontDialog(this, "请选择一种字体", this.tempMsgDTO
				.getFont(), true);
		dialog.showDialog();
		Font tempFont = null;
		if (FontDialog.DialogResult.Ok == dialog.getDialogResult()) {
			tempFont = dialog.getSelectedFont();
//			System.out.println("选择的字体是:"+tempFont.toString());
		}
		dialog.Dispose(); 
		if (tempFont != null) {
			this.tempMsgDTO.setFont(tempFont);
		}
	}

	private void getSelectedColor() {
		Color color = JColorChooser.showDialog(this, "请选择一种颜色", this.tempMsgDTO
				.getColor());
		if (color != null) {
			this.tempMsgDTO.setColor(color);
		}
	}
	private MsgDTO transferMsgDTO(){
		MsgDTO msgDTO=new MsgDTO();
		msgDTO.setUserId(this.tempMsgDTO.getUserId());
		msgDTO.setContent(this.tempMsgDTO.getContent());
		msgDTO.setToWhere(this.tempMsgDTO.getToWhere());
		msgDTO.setFont(this.tempMsgDTO.getFont());
		msgDTO.setColor(this.tempMsgDTO.getColor());
		return msgDTO;
	}
	private void setMessageText(MsgDTO tempMsgDTO) {
		String msg=tempMsgDTO.getUserId()
		+ " 对 "
		+ tempMsgDTO.getToWhere() + " 说: "
		+ tempMsgDTO.getContent() + '\n';
		// StyledDocument doc = txtMsg.getStyledDocument();
		try {
			addStylesToDocument(tempMsgDTO);
			doc.insertString(doc.getLength(),msg, doc.getStyle("style1"));
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		// 让文本框的焦点始终在最下面
		txtUpMsg.setCaretPosition(doc.getLength());
	}// 初始化10种颜色的字体
	private void addStylesToDocument(MsgDTO tempMsgDTO) {
		doc = txtUpMsg.getStyledDocument();
		StyledDocument doc = txtUpMsg.getStyledDocument();
		// 试试简单方法
		Style def = StyleContext.getDefaultStyleContext().getStyle(
				StyleContext.DEFAULT_STYLE);
		Font tempFont=tempMsgDTO.getFont();
		Style tempStyle=doc.addStyle("style1", def);
		StyleConstants.setBold(tempStyle,tempFont.isBold());
		StyleConstants.setFontSize(tempStyle, tempFont.getSize());
		StyleConstants.setItalic(tempStyle,tempFont.isItalic());
		StyleConstants.setForeground(tempStyle,tempMsgDTO.getColor());
	}
}

⌨️ 快捷键说明

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