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("Talk to 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); // Register clipboard event 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"); } }); // Register the ctrl+v event, each addition is by appending at the end of the document html, so you can paste editorPane_1.registerKeyboardAction( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DataFlavor[] flavots = Toolkit.getDefaultToolkit().getSystemClipboard().getAvailableDataFlavors(); for (DataFlavor dataFlavor : flavots) { try { // Get clipboard data Object obj = Toolkit.getDefaultToolkit().getSystemClipboard().getData(dataFlavor); System.out.println(obj.getClass()); // If it is text content if (obj instanceof InputStream) { InputStream bts = (InputStream) obj; editorPane_1.setText(editorPane_1.getText().replace("</body>", IOUtils.readLines(bts, "utf-8").toString().replaceAll("\n", "<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;width: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;width: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("发送"); // Click the event of the send button button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // Clear the content below, set the content above String s = editorPane_1.getText(); editorPane_1.setText(""); editorPane.setText(s); } }); // Register ctrl+Enter event button.registerKeyboardAction( new ActionListener() { public void actionPerformed(ActionEvent e) { // Clear the content below, set the content above 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); } }