package com.*.test; import java.awt.EventQueue; import java.awt.HeadlessException; import java.awt.Toolkit; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.UnsupportedFlavorException; import javax.swing.JFrame; import javax.swing.text.EditorKit; import javax.swing.text.html.HTMLEditorKit; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import javax.swing.JEditorPane; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JScrollPane; import javax.swing.KeyStroke; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Collection; import java.awt.event.ActionEvent; public class ChatForm { private JFrame frame; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ChatForm window = new ChatForm(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public ChatForm() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame("与 XXX 对话"); frame.setSize(766, 615); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); EditorKit k = new HTMLEditorKit(); JScrollPane scrollPane_1 = new JScrollPane(); scrollPane_1.setBounds(10, 10, 730, 350); frame.getContentPane().add(scrollPane_1); final JEditorPane editorPane = new JEditorPane(); scrollPane_1.setViewportView(editorPane); editorPane.setEditable(false); editorPane.setEditorKit(k); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(10, 373, 730, 161); frame.getContentPane().add(scrollPane); final JEditorPane editorPane_1 = new JEditorPane(); scrollPane.setViewportView(editorPane_1); editorPane_1.setEditorKit(k); // 注册剪贴板事件 KeyStroke aKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK, true); editorPane_1.addAncestorListener(new AncestorListener() { @Override public void ancestorRemoved(AncestorEvent event) { System.out.println("ancestorRemoved"); } @Override public void ancestorMoved(AncestorEvent event) { System.out.println("ancestorMoved"); System.out.println(editorPane_1.getText()); } @Override public void ancestorAdded(AncestorEvent event) { System.out.println("ancestorAdded"); } }); // 注册ctrl+v事件,每次添加都是在文档html末尾追加的方式,所以可以多次粘贴 editorPane_1.registerKeyboardAction(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DataFlavor[] flavots = Toolkit.getDefaultToolkit().getSystemClipboard().getAvailableDataFlavors(); for (DataFlavor dataFlavor : flavots) { try { // 获取剪贴板数据 Object obj = Toolkit.getDefaultToolkit().getSystemClipboard().getData(dataFlavor); System.out.println(obj.getClass()); // 如果是文字内容 if (obj instanceof InputStream) { InputStream bts = (InputStream) obj; editorPane_1.setText(editorPane_1.getText().replace("</body>", IOUtils.readLines(bts, "utf-8").toString().replaceAll(" ", "<br/></body>"))); } else if (obj instanceof Collection<?>) { // 多文件内容 Collection<?> c = (Collection<?>) obj; String s = ""; for (Object object : c) { File f = (File) object; if (FilenameUtils.getExtension(f.getName()).equals("jpg")) { s += "<img style=\"height:300px;200px\" src='file:///" + f.getAbsolutePath() + "'/><br/>"; } } editorPane_1.setText(s.replace("</body>", s + "</br></body>")); } else if (obj instanceof BufferedImage) { // 截图内容 BufferedImage img = (BufferedImage) obj; String savePath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); savePath = FilenameUtils.separatorsToUnix(savePath); System.out.println(savePath); File file = new File(savePath + "/" + System.currentTimeMillis() + ".png"); ImageIO.write(img, "png", file); editorPane_1.setText(editorPane_1.getText().replace("</body>", "<img style="height:300px;200px" src='file:///" + file.getAbsolutePath() + "'/><br/></body>")); } } catch (HeadlessException e1) { e1.printStackTrace(); } catch (UnsupportedFlavorException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } } }, aKeyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW); JButton button = new JButton("发送"); // 点击发送按钮的事件 button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 清空下面的内容,设置上面的内容 String s = editorPane_1.getText(); editorPane_1.setText(""); editorPane.setText(s); } }); // 注册ctrl+回车事件 button.registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent e) { // 清空下面的内容,设置上面的内容 String s = editorPane_1.getText(); editorPane_1.setText(""); editorPane.setText(s); } }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, ActionEvent.CTRL_MASK, true), JComponent.WHEN_IN_FOCUSED_WINDOW); button.setBounds(628, 544, 93, 23); frame.getContentPane().add(button); } }