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

📄 chatmain.java

📁 一个基于java的局域网聊天程序
💻 JAVA
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.UnknownHostException;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;
import javax.swing.JOptionPane;
import java.sql.*;

import com.microsoft.jdbc.sqlserver.*;

public class ChatMain extends JFrame {
	private JList list;
	private String account;//本窗口的用户名
	DefaultListModel model = new DefaultListModel();
	private ClientSocket cs=null;
	
	public ChatMain(String account) {
		super("XX");
		pack();
		
		this.account=account;
		try {
			cs=new ClientSocket(account);
			cs.sendName();//将登录名发送到服务器,告之以登录
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		setSize(new Dimension(170, 360));
		
		
		getContentPane().setBackground(new Color(0, 255, 255));
		getContentPane().setLayout(null);
		

		final JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.setBorder(new LineBorder(Color.black, 1, false));
		panel.setBounds(15, 20, 40, 40);
		getContentPane().add(panel);

		final JLabel headiconLabel = new JLabel();
		panel.add(headiconLabel, BorderLayout.CENTER);
		headiconLabel.setIcon(new ImageIcon("head.jpg"));

		final JLabel label = new JLabel();//账户标签
		label.setBorder(new LineBorder(Color.black, 1, false));
		label.setText(account);
		label.setBounds(69, 29, 69, 18);
		getContentPane().add(label);
		
		this.setLocation(800, 50);
		setResizable(false);
		setVisible(true);

		final JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBounds(15, 81, 123, 178);
		getContentPane().add(panel_1);

		final JButton button = new JButton();
		button.setBounds(0, 0, 123, 19);
		panel_1.add(button);
		button.setText("我的好友");

		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 20, 123, 158);
		panel_1.add(scrollPane);

		list = new JList();
		list.setBorder(new LineBorder(Color.black, 1, false));
		list.setModel(model);
		scrollPane.setViewportView(list);
		list.addMouseListener(new ToChat());//双击进行聊天

		final JButton button_1 = new JButton();
		button_1.addActionListener(new AddFriend());

		button_1.setText("添加好友");
		button_1.setBounds(32, 279, 92, 28);
		getContentPane().add(button_1);
		
		model.addElement("张三");
		model.addElement("夏靖");
		
	}
	public String getAccount() {
		return this.account;
	}
	public ClientSocket getClientSocket() {
		return this.cs;
	}
	
	class AddFriend implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String friend=JOptionPane.showInputDialog(null, "请输入要添加的好友名字","添加好友...",JOptionPane.QUESTION_MESSAGE);
			try {
				Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
			} catch (ClassNotFoundException e2) {
				e2.printStackTrace();
			} 
			String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xiajing";  
			String user="sa"; 
			String password=""; 
			Connection con;
			try {
				con = DriverManager.getConnection(url,user,password);
				Statement state=con.createStatement();
				String sqlStr="select * from register where 帐号 = '"+friend+"'";
				ResultSet rs1=state.executeQuery(sqlStr);
				if(!rs1.next())
					JOptionPane.showMessageDialog(null, "该帐号不存在,添加好友失败...", "错误",JOptionPane.ERROR_MESSAGE);
				else {
					model.addElement(friend);
					JOptionPane.showMessageDialog(null, "恭喜,添加成功!!!", "友情提示", JOptionPane.INFORMATION_MESSAGE);
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			
		}
		
	}
	
	class ToChat extends MouseAdapter {//双击聊天的事件,未完成	
		public void mouseClicked(MouseEvent e) {
			if (e.getClickCount() == 2) {
					int index = list.locationToIndex(e.getPoint());
					System.out.println(index);
					String source=getAccount();
					PrintWriter pw=null;
					BufferedReader br=null;
					try {
						pw=new PrintWriter(getClientSocket().getSocket().getOutputStream(), true);
						br=new BufferedReader(new InputStreamReader(getClientSocket().getSocket().getInputStream()));
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					String chatto=(String) model.get(index);
					new ChatFrame(chatto,source,pw,br);//与chatto进行聊天
			}
		}
	}
	/*public static void main(String[] args) {
		new ChatMain("夏靖");
	}*/
}

⌨️ 快捷键说明

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