聊天室java版

  • 我要取一个特别特别特别特别特别长的名字
    了解作者
  • Java
    开发工具
  • 532.1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 5 积分
    下载积分
  • 0
    下载次数
  • 2022-05-29 13:50
    上传日期
聊天室
java.zip
内容介绍
package cat.client; import java.applet.Applet; import java.applet.AudioClip; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.Graphics; 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.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.URL; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Vector; import javax.swing.AbstractListModel; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.ListCellRenderer; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.Border; import javax.swing.border.TitledBorder; import cat.function.CatBean; import cat.util.CatUtil; class CellRenderer extends JLabel implements ListCellRenderer { CellRenderer() { setOpaque(true); } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));// 加入宽度为5的空白边框 if (value != null) { setText(value.toString()); setIcon(new ImageIcon("images//1.jpg")); } if (isSelected) { setBackground(new Color(255, 255, 153));// 设置背景色 setForeground(Color.black); } else { // 设置选取与取消选取的前景与背景颜色. setBackground(Color.white); // 设置背景色 setForeground(Color.black); } setEnabled(list.isEnabled()); setFont(new Font("sdf", Font.ROMAN_BASELINE, 13)); setOpaque(true); return this; } } class UUListModel extends AbstractListModel{ private Vector vs; public UUListModel(Vector vs){ this.vs = vs; } @Override public Object getElementAt(int index) { return vs.get(index); } @Override public int getSize() { return vs.size(); } } public class CatChatroom extends JFrame { private static final long serialVersionUID = 6129126482250125466L; private static JPanel contentPane; private static Socket clientSocket; private static ObjectOutputStream oos; private static ObjectInputStream ois; private static String name; private static JTextArea textArea; private static AbstractListModel listmodel; private static JList list; private static String filePath; private static JLabel lblNewLabel; private static JProgressBar progressBar; private static Vector onlines; private static boolean isSendFile = false; private static boolean isReceiveFile = false; // 声音 private static File file, file2; private static URL cb, cb2; private static AudioClip aau, aau2; /** * Create the frame. */ public CatChatroom(String u_name, Socket client) { // 赋值 name = u_name; clientSocket = client; onlines = new Vector(); SwingUtilities.updateComponentTreeUI(this); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } catch (UnsupportedLookAndFeelException e1) { e1.printStackTrace(); } setTitle(name); setResizable(false); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); setBounds(200, 100, 688, 510); contentPane = new JPanel() { private static final long serialVersionUID = 1L; @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(new ImageIcon("images\\聊天室1.jpg").getImage(), 0, 0, getWidth(), getHeight(), null); } }; setContentPane(contentPane); contentPane.setLayout(null); // 聊天信息显示区域 JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(10, 10, 410, 300); getContentPane().add(scrollPane); textArea = new JTextArea(); textArea.setEditable(false); textArea.setLineWrap(true);//激活自动换行功能 textArea.setWrapStyleWord(true);//激活断行不断字功能 textArea.setFont(new Font("sdf", Font.BOLD, 13)); scrollPane.setViewportView(textArea); // 打字区域 JScrollPane scrollPane_1 = new JScrollPane(); scrollPane_1.setBounds(10, 347, 411, 97); getContentPane().add(scrollPane_1); final JTextArea textArea_1 = new JTextArea(); textArea_1.setLineWrap(true);//激活自动换行功能 textArea_1.setWrapStyleWord(true);//激活断行不断字功能 scrollPane_1.setViewportView(textArea_1); // 关闭按钮 final JButton btnNewButton = new JButton("离开"); btnNewButton.setBounds(184, 448, 90, 30); getContentPane().add(btnNewButton); // 发送按钮 JButton btnNewButton_1 = new JButton("发送"); btnNewButton_1.setBounds(350, 448, 90, 30); getRootPane().setDefaultButton(btnNewButton_1); getContentPane().add(btnNewButton_1); // 在线客户列表 listmodel = new UUListModel(onlines) ; list = new JList(listmodel); list.setCellRenderer(new CellRenderer()); list.setOpaque(false); Border etch = BorderFactory.createEtchedBorder(); list.setBorder(BorderFactory.createTitledBorder(etch, "<"+u_name+">" + "在线客户:", TitledBorder.LEADING, TitledBorder.TOP, new Font( "sdf", Font.BOLD, 20), Color.green)); JScrollPane scrollPane_2 = new JScrollPane(list); scrollPane_2.setBounds(430, 10, 245, 375); scrollPane_2.setOpaque(false); scrollPane_2.getViewport().setOpaque(false); getContentPane().add(scrollPane_2); // 文件传输栏 progressBar = new JProgressBar(); progressBar.setBounds(430, 390, 245, 15); progressBar.setMinimum(1); progressBar.setMaximum(100); getContentPane().add(progressBar); // 文件传输提示 lblNewLabel = new JLabel( "\u6587\u4EF6\u4F20\u9001\u4FE1\u606F\u680F:"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 12)); lblNewLabel.setBackground(Color.WHITE); lblNewLabel.setBounds(430, 410, 245, 15); getContentPane().add(lblNewLabel); try { oos = new ObjectOutputStream(clientSocket.getOutputStream()); // 记录上线客户的信息在catbean中,并发送给服务器 CatBean bean = new CatBean(); bean.setType(0); bean.setName(name); bean.setTimer(CatUtil.getTimer()); oos.writeObject(bean); oos.flush(); // 消息提示声音 file = new File("sounds\\呃欧.wav"); cb = file.toURL(); aau = Applet.newAudioClip(cb); // 上线提示声音 file2 = new File("sounds\\叮.wav"); cb2 = file2.toURL(); aau2 = Applet.newAudioClip(cb2); // 启动客户接收线程 new ClientInputThread().start(); } catch (IOException e) { e.printStackTrace(); } // 发送按钮 btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String info = textArea_1.getText(); List to = list.getSelectedValuesList(); if (to.size() < 1) { JOptionPane.showMessageDialog(getContentPane(), "请选择聊天对象"); return; } if (to.toString().contains(name+"(我)")) { JO
评论
    相关推荐
    • Java
      Java 对于Java练习
    • java
      Java Java基础
    • Java
      Java 我创建的Java项目
    • JAVA教程
      一本非常不错的清华大学java教程,讲解非常详细,看了就知道。
    • Java Cipher
      Java Cipher 加密和解密工具 附带源码 Java Cipher 加密和解密工具 附带源码 Java Cipher 加密和解密工具 附带源码 Java Cipher 加密和解密工具 附带源码
    • javabank
      Java银行 Java com的模拟操作将在bancárias上发布! 要求:Java 8。
    • java 李兴华
      java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java ...
    • Java204
      支持中文,支持动态注释的超强Java语言反编译器。 注意:Java204的工作目录中不能包含中文字符!! 支持现有所有Java环境产生的Class代码,而最重要的是它生成的源代码可读性极好,又充分考虑了国人的Java编码习惯,...
    • java程序
      java程序-吃点点java程序-吃点点java程序-吃点点java程序-吃点点java程序-吃点点java程序-吃点点
    • Java
      Java 2021/02/19 .java Main.java 学生.java Bmi.java 。班级 Bmi类 主类 MyBmi.class 学生班 学生样本类 2021/02/20 .java DeleteFile.java WriteFile.java MyDataPrint.java 。班级 DeleteFile.class ...