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

📄 talkinterface.java

📁 这是一个使用Java编写的像QQ一样的即时通讯软件
💻 JAVA
字号:
package client;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.io.DataOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class TalkInterface extends JFrame {

    public DataOutputStream sendToServer;

    public JList list;

    public Integer talkingNumber;

    public JTextPane textPane;

    public TalkInterface talkInterface;

    public Vector<Integer> talkingFriends;

    public Integer inviterNumber;

    public Integer groupTalkInterfaceId;

    private static final long serialVersionUID = -5066272190141389129L;

    /**
     * Create the frame
     */
    public TalkInterface(final DataOutputStream sendToServer) {

	super();


	addWindowFocusListener(new WindowFocusListener() {
	    public void windowGainedFocus(final WindowEvent e) {

		talkInterface.setAlwaysOnTop(false);
	    }

	    public void windowLostFocus(final WindowEvent e) {
	    }
	});

	setResizable(false);

	this.sendToServer = sendToServer;

	talkingFriends = new Vector<Integer>();

	addWindowListener(new WindowAdapter() {
	    public void windowClosed(final WindowEvent e) {

		if (talkInterface.getTitle().equals("你在与多人聊天")) {
		    Client.grouptalkInterfaces.remove(groupTalkInterfaceId);
		    Client.groupTalkInterfaceIds.remove(groupTalkInterfaceId);
		    Client.tempGroupTalkInterfaceIds.add(groupTalkInterfaceId);
		    Client.mapGrouptalkInterfaces
		    .remove(talkInterface.groupTalkInterfaceId);
		} else {
		    Client.talkInterfaces.remove(talkingNumber);
		}

	    }
	});

	talkInterface = this;

	getContentPane().setLayout(
		new FormLayout("396px:grow(1.0)", "fill:313px:grow(1.0)"));

	setBounds(100, 100, 509, 375);
	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

	final JSplitPane splitPane_1 = new JSplitPane();
	splitPane_1.setAutoscrolls(true);
	splitPane_1.setContinuousLayout(true);
	splitPane_1.setOneTouchExpandable(true);
	getContentPane().add(splitPane_1, new CellConstraints());

	final JPanel panel_1 = new JPanel();
	panel_1.setLayout(new FormLayout("399px:grow(1.0)",
	"fill:189dlu:grow(1.0), fill:31px"));
	panel_1.setAutoscrolls(true);
	splitPane_1.setLeftComponent(panel_1);

	final JSplitPane splitPane = new JSplitPane();
	splitPane.setDividerSize(2);
	splitPane.setName("splitPane");
	panel_1.add(splitPane, new CellConstraints("1, 1, fill, fill"));
	splitPane.setBorder(new EmptyBorder(0, 0, 0, 0));
	splitPane.setAutoscrolls(true);
	splitPane.setContinuousLayout(true);
	splitPane.setDividerLocation(234);
	splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);

	final JScrollPane scrollPane = new JScrollPane();
	splitPane.setLeftComponent(scrollPane);

	textPane = new JTextPane();
	scrollPane.setViewportView(textPane);
	textPane.setEditable(false);
	textPane.setDragEnabled(true);

	final JScrollPane scrollPane_1 = new JScrollPane();
	scrollPane_1
	.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
	splitPane.setRightComponent(scrollPane_1);

	final JEditorPane editorPane = new JEditorPane();
	editorPane.setFont(new Font("", Font.PLAIN, 14));
	scrollPane_1.setViewportView(editorPane);

	final JPanel panel = new JPanel();
	panel
	.setLayout(new FormLayout(
		"default:grow(1.0), 45dlu, 63px, 25dlu, 43dlu, left:17dlu, 63px:grow(1.0)",
	"default:grow(1.0)"));
	panel.setBackground(new Color(159, 167, 236));
	panel_1.add(panel, new CellConstraints(1, 2, CellConstraints.FILL,
		CellConstraints.FILL));

	final JButton button = new JButton();

	panel.add(button, new CellConstraints("3, 1, 1, 1, fill, center"));
	button.setText("发 送");
	button.addMouseListener(new MouseAdapter() {

	    public void mouseClicked(MouseEvent arg0) {

		if (editorPane.getText().equals("")) {

		    JOptionPane.showMessageDialog(talkInterface, "不能发送空信息",
			    "QQ", JOptionPane.ERROR_MESSAGE);

		    return;
		}

		if (!editorPane.getText().equals("")){

		    try {
			
			textPane.setText(textPane.getText()
				+ Client.goodFriends.get(Client.number) + " "+DateFormat.getInstance().format(new Date())+"说:\n"
				+ editorPane.getText() + "\n");

		    } catch (Exception e) {

			e.printStackTrace();
		    }

		}

		textPane.setSelectionStart(textPane.getText().length());

		if (talkingFriends.size() == 1) {

		    try {

			sendToServer.writeUTF("Speakto@"

				+ talkingFriends.get(0) + "@" + Client.number + "@ "
				+ DateFormat.getInstance().format(new Date())+" 说:\n"+editorPane.getText());

		    } catch (IOException e) {

			e.printStackTrace();
		    }

		} else if (talkingFriends.size() > 1) {

		    for (int i = 0; i < talkingFriends.size(); i++) {
			try {
			    sendToServer.writeUTF("GroupMesseng@"

				    + talkingFriends.get(i) + "@"
				    + Client.number + "@"
				    + talkInterface.inviterNumber + "@"
				    + DateFormat.getInstance().format(new Date())+" 说:\n"+ editorPane.getText());

			} catch (IOException e) {

			    e.printStackTrace();
			}
		    }

		}

		editorPane.setText("");
	    }
	});

	final JButton button_1 = new JButton();
	button_1.addActionListener(new ActionListener() {
	    public void actionPerformed(final ActionEvent e) {

		if (talkInterface.getTitle().equals("你在与多人聊天")) {
		    Client.grouptalkInterfaces.remove(groupTalkInterfaceId);
		    Client.groupTalkInterfaceIds.remove(groupTalkInterfaceId);
		    Client.tempGroupTalkInterfaceIds.add(groupTalkInterfaceId);
		} else {
		    Client.talkInterfaces.remove(talkingNumber);
		}
		talkInterface.dispose();
	    }
	});

	panel.add(button_1, new CellConstraints("5, 1, 1, 1, center, center"));
	button_1.setText("关 闭");

	final JPanel rightPanel = new JPanel();
	rightPanel
	.setLayout(new FormLayout(
		"96px:grow(1.0)",
	"8dlu, fill:16dlu, fill:16dlu, 17dlu, 18dlu, 17dlu, fill:38dlu, 14dlu, fill:17px:grow(1.0)"));
	splitPane_1.setRightComponent(rightPanel);

	final JScrollPane friendsScrollPane = new JScrollPane();
	friendsScrollPane
	.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
	rightPanel.add(friendsScrollPane, new CellConstraints(
		"1, 9, 1, 1, fill, fill"));
	friendsScrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));

	MyCellRenderer myCellRenderer = new MyCellRenderer();
	list = new JList();
	list.setCellRenderer(myCellRenderer);
	list.setVisibleRowCount(1);
	list.setDragEnabled(true);

	list.setFont(new Font("宋体", Font.PLAIN, 12));
	list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	list.setValueIsAdjusting(true);
	friendsScrollPane.setViewportView(list);

	final JLabel label = new JLabel();
	label.addMouseListener(new MouseAdapter() {
	    public void mouseClicked(final MouseEvent e) {

		AddTalkingFriends addTalkingFriends = new AddTalkingFriends(
			talkInterface);

		Object[] keyArray = Client.goodFriends.keySet().toArray();
		Vector<String> friends = new Vector<String>();

		for (int i = keyArray.length - 1; i >= 0; i--) {

		    if (!((Integer) keyArray[i]).equals(Client.number))
			friends.add(keyArray[i] + "@"
				+ Client.goodFriends.get(keyArray[i]));
		}

		addTalkingFriends.goodFriendsList.setListData(friends);

		Vector<String> talking = new Vector<String>();
		for (int i = 0; i < talkingFriends.size(); i++) {

		    talking.add(talkingFriends.get(i) + "@"
			    + Client.goodFriends.get(talkingFriends.get(i)));
		}

		addTalkingFriends.talkingFriendsList.setListData(talking);
		addTalkingFriends.repaint();
		addTalkingFriends.setVisible(true);
	    }
	});
	label.setForeground(new Color(71, 99, 150));
	label.setFont(new Font("宋体", Font.PLAIN, 12));
	label.setText("添加聊天者");
	rightPanel.add(label, new CellConstraints(1, 3, CellConstraints.CENTER,
		CellConstraints.FILL));

	final JLabel label_2 = new JLabel();
	rightPanel.add(label_2, new CellConstraints(1, 7, CellConstraints.FILL,
		CellConstraints.FILL));

	final JLabel label_3 = new JLabel();
	label_3.setVerticalAlignment(SwingConstants.BOTTOM);
	label_3.setVerticalTextPosition(SwingConstants.BOTTOM);
	label_3.setOpaque(true);
	label_3.setBackground(new Color(128, 128, 255));
	label_3.setForeground(new Color(255, 255, 255));
	label_3.setText(" 和你聊天的同事");
	rightPanel.add(label_3, new CellConstraints(1, 8, CellConstraints.FILL,
		CellConstraints.FILL));

	final JScrollPane scrollPane_2 = new JScrollPane();
	scrollPane_2.setBorder(new EmptyBorder(0, 0, 0, 0));

    }

    class MyCellRenderer extends JLabel implements ListCellRenderer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8668388946915381179L;

	public Component getListCellRendererComponent(JList list, Object value,
		int index, // cell index
		boolean isSelected, // is the cell selected
		boolean cellHasFocus) // the list and the cell have the focus
	{
	    String s = value.toString();

	    setText(s.substring(s.indexOf("@") + 1));

	    setName(s.substring(0, s.indexOf("@") - 1));

	    if (isSelected) {
		setBackground(list.getSelectionBackground());
		setForeground(list.getSelectionForeground());
	    } else {
		setBackground(list.getBackground());
		setForeground(list.getForeground());
	    }

	    setEnabled(list.isEnabled());
	    setFont(list.getFont());
	    setOpaque(true);

	    return this;
	}

    }
}

⌨️ 快捷键说明

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