标题: 局域聊天-带照片 [打印本页] 作者: 可可 时间: 2017-8-24 22:24 标题: 局域聊天-带照片 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");