凯哥学堂

搜索

凯哥学堂

局域聊天-带照片 [复制链接]

查看: 7557|回复: 0
打印 上一主题 下一主题

72

主题

72

帖子

594

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
594
跳转到指定楼层
楼主
发表于 2017-8-24 22:24:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
package com.kaige123;
/**
* 消息页面
* @author 凯哥
*
*/
public class MessageFrame extends JFrame {
        //把消息分成两部分
        private JSpinner spinner = new JSpinner();
        //发送文本框
        public JTextArea textArea = new JTextArea();
        //网络名称
        private String uname="";
    //消息呈现
        public JEditorPane editorPane = new JEditorPane();
        public JPanel panel_2 = new JPanel();
        public JButton button_2 = new JButton();
        public JButton button_3 = new JButton();
        public JButton button = new JButton();
        public JPanel panel_1 = new JPanel();
        public JPanel panel = new JPanel();
        public JSplitPane splitPane = new JSplitPane();
        public JScrollPane scrollPane = new JScrollPane();
        public FlowLayout flowLayout = new FlowLayout();
        public JScrollPane scrollPane_1 = new JScrollPane();
        public JLabel label = new JLabel();
        public FlowLayout flowLayout_1 = new FlowLayout();
        //程序入口
        public static void main(String args[]) {
                                        MessageFrame frame = new MessageFrame();
                                        frame.setVisible(true);
        }
        public MessageFrame() {
                super();
                uname=javax.swing.JOptionPane.showInputDialog(this,"请输入你的昵称:");
                new UDPServer(this).start();
                setTitle("凯哥学堂-小案例-局域网聊天软件");
                setBounds(100, 100, 468, 412);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                splitPane.setDividerLocation(260);
                splitPane.setDividerSize(1);
                splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
                getContentPane().add(splitPane, BorderLayout.CENTER);   
                splitPane.setLeftComponent(scrollPane);
                scrollPane.setViewportView(editorPane);
                panel.setLayout(new BorderLayout());
                splitPane.setRightComponent(panel);  
                flowLayout.setAlignment(FlowLayout.LEFT);
                panel_1.setLayout(flowLayout);
                panel.add(panel_1, BorderLayout.NORTH);
                button.setText("图片");
                panel_1.add(button);
                label.setText("字体大小:");
                spinner.setPreferredSize(new Dimension(40, 20));
                spinner.setValue(12);
                panel_1.add(spinner);
                flowLayout_1.setAlignment(FlowLayout.RIGHT);
                panel_2.setLayout(flowLayout_1);
                panel.add(panel_2, BorderLayout.SOUTH);
                button_2.setText("震屏");
                panel_2.add(button_2);
                button_3.setText("发送");
                panel_2.add(button_3);
                panel.add(scrollPane_1, BorderLayout.CENTER);
                scrollPane_1.setViewportView(textArea);
                button_3.addActionListener(new ActionListener() {
                        public void actionPerformed(final ActionEvent e) {
                                try {
                                        String ip=InetAddress.getLocalHost().getHostAddress();
                                        ip=ip.substring(0, ip.lastIndexOf("."));
                                        byte[] b = (uname+"\t宋体\t"+spinner.getValue()+"\t"+textArea.getText()).getBytes();
                                        DatagramPacket data = new DatagramPacket(b, b.length,
                                                        InetAddress.getByName(ip+".255"), 8978);
                                        DatagramSocket client = new DatagramSocket();
                                        client.send(data);
                                        textArea.setText("");
                                } catch (Exception e2) {
                                        e2.printStackTrace();
                                }
                        }
                });
                button_2.addActionListener(new ActionListener() {
                        public void actionPerformed(final ActionEvent e) {
                                try {
                                        String ip=InetAddress.getLocalHost().getHostAddress();
                                        ip=ip.substring(0, ip.lastIndexOf("."));
                                        byte[] b = (uname+"\t宋体\t"+spinner.getValue()+"\tZPZL22234").getBytes();
                                        DatagramPacket data = new DatagramPacket(b, b.length,
                                                        InetAddress.getByName(ip+".255"), 8978);
                                        DatagramSocket client = new DatagramSocket();
                                        client.send(data);
                                        textArea.setText("");
                                } catch (Exception e2) {
                                        e2.printStackTrace();
                                }
                        }
                });
                button.addActionListener(new ActionListener() {
                        public void actionPerformed(final ActionEvent e) {
                                new SelectImageFrame(MessageFrame.this).setVisible(true);
                        }
                });
        }
}
package com.kaige123;
/**
* 照片选择
* @author 凯哥
*
*/
public class SelectImageFrame extends JFrame {
        class MyMouseListener extends MouseAdapter {
                String code = "";
                public MyMouseListener(String code) {
                        this.code = code;
                }
                public void mousePressed(MouseEvent e) {
                         frame.textArea.setText(frame.textArea.getText()+code);
                         SelectImageFrame.this.setVisible(false);
                }
        }

        private static MessageFrame frame;
       
        public SelectImageFrame(MessageFrame frame1) {
                super();
                this.frame = frame1;
                getContentPane().setLayout(new GridLayout(5, 0));
                setBounds(100, 100, 500, 500);
                setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                for (int i = 0; i < 18; i++) {
                        if (i >= 10) {
                                JLabel j1 = new JLabel(new ImageIcon("./image/1" + (i - 10)
                                                + ".jpg"));
                                j1.addMouseListener(new MyMouseListener("[1" + (i - 10) + "]"));
                                this.add(j1);
                        } else {
                                JLabel j1 = new JLabel(new ImageIcon("./image/0" + i + ".jpg"));
                                j1.addMouseListener(new MyMouseListener("[0" + (i) + "]"));
                                this.add(j1);
                        }
                }
        }
}
package com.kaige123;
/**
* 局域网数据处理
* @author 凯哥
*
*/
public class UDPServer extends Thread {

        private MessageFrame frame = null;

        public UDPServer(MessageFrame frame) {
                this.frame = frame;
        }

        // 处理对方发送过来的数据
        public void run() {
                try {
                        DatagramSocket server = new DatagramSocket(8978);
                        while (true) {
                                try {
                                        byte[] b = new byte[1024 * 100];
                                        DatagramPacket data = new DatagramPacket(b, b.length);
                                        server.receive(data);
                                        String message = new String(data.getData(), 0,
                                                        data.getLength());
                                        String name = message.split("\t")[0];// 昵称
                                        String font = message.split("\t")[1];// 字体
                                        String size = message.split("\t")[2];// 大小
                                        String text = message.split("\t")[3];// 内容
                                        // 设置我里面放入的是网页代码 请解析页面

                                        try {
                                                File html = new File("msg.html");
                                                FileInputStream in = new FileInputStream(html);
                                                byte[] b1 = new byte[(int) html.length()];
                                                in.read(b1);
                                                in.close();

                                                String newHtmlText = new String(b1);
                                                newHtmlText = newHtmlText.replace("{name}", name);
                                                newHtmlText = newHtmlText.replace("{time}",
                                                                new Date().toLocaleString());
                                                if (text.trim().equals("ZPZL22234")) {
                                                        newHtmlText = newHtmlText
                                                                        .replace("{text}", "震屏ZZZ");

                                                        new Thread() {

                                                                public void run() {

                                                                        Point last = frame.getLocation();
                                                                        try {

                                                                                for (int i = 0; i < 5; i++) {
                                                                                        frame.setLocation(last.x - 10,
                                                                                                        last.y - 10);
                                                                                        Thread.sleep(30);
                                                                                        frame.setLocation(last.x - 10,
                                                                                                        last.y + 10);
                                                                                        Thread.sleep(30);
                                                                                        frame.setLocation(last.x + 10,
                                                                                                        last.y - 10);
                                                                                        Thread.sleep(30);
                                                                                        frame.setLocation(last.x + 10,
                                                                                                        last.y + 10);
                                                                                        Thread.sleep(30);
                                                                                }
                                                                        } catch (Exception e) {
                                                                                // TODO: handle exception
                                                                        }
                                                                        frame.setLocation(last);

                                                                };
                                                        }.start();

                                                } else {
                                                        newHtmlText = newHtmlText.replace("{text}", text);
                                                }

                                                newHtmlText = newHtmlText.replace("{font}", size);
                                                // {font}

                                                for (String[] image : images) {
                                                        if (newHtmlText.indexOf(image[0]) >= 0) {
                                                                newHtmlText = newHtmlText.replace(image[0],
                                                                                "<img  src='" + image[1] + "'>");
                                                        }
                                                }

                                                htmlText += newHtmlText;

                                                frame.editorPane.setContentType("text/html");
                                                frame.editorPane.setText(htmlText);
                                               
                                                frame.editorPane.setSelectionStart(htmlText.length());
                                                frame.editorPane.setSelectionEnd(htmlText.length());

                                        } catch (Exception e) {
                                                e.printStackTrace();
                                        }

                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }

                } catch (SocketException e) {
                        javax.swing.JOptionPane.showMessageDialog(frame, "软件已经打开过了!");

                }

        }

        // 照片对应的网络地址
        String[][] images = {
                        { "[00]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/00.jpg" },
                        { "[01]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/01.jpg" },
                        { "[02]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/02.jpg" },
                        { "[03]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/03.jpg" },
                        { "[04]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/04.jpg" },
                        { "[05]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/05.jpg" },
                        { "[06]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/06.jpg" },
                        { "[07]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/07.jpg" },
                        { "[08]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/08.jpg" },
                        { "[09]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/09.jpg" },
                        { "[10]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/10.jpg" },
                        { "[11]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/11.jpg" },
                        { "[12]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/12.jpg" },
                        { "[13]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/13.jpg" },
                        { "[14]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/14.jpg" },
                        { "[15]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/15.jpg" },
                        { "[16]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/16.jpg" },
                        { "[17]", "http://pic2.sc.chinaz.com/Files/pic/faces/3529/17.jpg" } };

        String htmlText = "";
}



Msg.html
<table width="100%" border="0">
<tr>
   <td>{name}</td>
   <td align="right">时间:{time} </td>
</tr>
</table>

<pre style="font-size:{font}px">{text}
</pre>
<hr>

工程目录
/aaa/src
com.kaige123
/aaa/src/com/kaige123/MessageFrame.java
/aaa/src/com/kaige123/SelectImageFrame.java
/aaa/src/com/kaige123/UDPServer.java
/aaa/image
/aaa/image/00.jpg
/aaa/image/01.jpg
/aaa/image/02.jpg
/aaa/image/03.jpg
/aaa/image/04.jpg
/aaa/image/05.jpg
/aaa/image/06.jpg
/aaa/image/07.jpg
/aaa/image/08.jpg
/aaa/image/09.jpg
/aaa/image/10.jpg
/aaa/image/11.jpg
/aaa/image/12.jpg
/aaa/image/13.jpg
/aaa/image/14.jpg
/aaa/image/15.jpg
/aaa/image/16.jpg
/aaa/image/17.jpg
回复

使用道具 举报