
1package com.*.test; 2 3import java.awt.EventQueue; 4import java.awt.HeadlessException; 5import java.awt.Toolkit; 6import java.awt.datatransfer.DataFlavor; 7import java.awt.datatransfer.UnsupportedFlavorException; 8 9import javax.swing.JFrame; 10import javax.swing.text.EditorKit; 11import javax.swing.text.html.HTMLEditorKit; 12 13import org.apache.commons.io.FilenameUtils; 14import org.apache.commons.io.IOUtils; 15 16import javax.swing.JEditorPane; 17import javax.imageio.ImageIO; 18import javax.swing.JButton; 19import javax.swing.JComponent; 20import javax.swing.JScrollPane; 21import javax.swing.KeyStroke; 22import javax.swing.event.AncestorEvent; 23import javax.swing.event.AncestorListener; 24 25import java.awt.event.ActionListener; 26import java.awt.event.KeyEvent; 27import java.awt.image.BufferedImage; 28import java.io.File; 29import java.io.IOException; 30import java.io.InputStream; 31import java.util.Collection; 32import java.awt.event.ActionEvent; 33 34public class ChatForm { 35 36 private JFrame frame; 37 38 /** 39 * Launch the application. 40 */ 41 public static void main(String[] args) { 42 EventQueue.invokeLater(new Runnable() { 43 public void run() { 44 try { 45 ChatForm window = new ChatForm(); 46 window.frame.setVisible(true); 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } 50 } 51 }); 52 } 53 54 /** 55 * Create the application. 56 */ 57 public ChatForm() { 58 initialize(); 59 } 60 61 /** 62 * Initialize the contents of the frame. 63 */ 64 private void initialize() { 65 frame = new JFrame("与 XXX 对话"); 66 frame.setSize(766, 615); 67 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 68 frame.getContentPane().setLayout(null); 69 EditorKit k = new HTMLEditorKit(); 70 71 JScrollPane scrollPane_1 = new JScrollPane(); 72 scrollPane_1.setBounds(10, 10, 730, 350); 73 frame.getContentPane().add(scrollPane_1); 74 75 final JEditorPane editorPane = new JEditorPane(); 76 scrollPane_1.setViewportView(editorPane); 77 editorPane.setEditable(false); 78 editorPane.setEditorKit(k); 79 80 JScrollPane scrollPane = new JScrollPane(); 81 scrollPane.setBounds(10, 373, 730, 161); 82 frame.getContentPane().add(scrollPane); 83 84 final JEditorPane editorPane_1 = new JEditorPane(); 85 scrollPane.setViewportView(editorPane_1); 86 editorPane_1.setEditorKit(k); 87 88 // 注册剪贴板事件 89 KeyStroke aKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK, true); 90 editorPane_1.addAncestorListener(new AncestorListener() { 91 92 @Override 93 public void ancestorRemoved(AncestorEvent event) { 94 System.out.println("ancestorRemoved"); 95 } 96 97 @Override 98 public void ancestorMoved(AncestorEvent event) { 99 System.out.println("ancestorMoved"); 100 System.out.println(editorPane_1.getText()); 101 } 102 103 @Override 104 public void ancestorAdded(AncestorEvent event) { 105 System.out.println("ancestorAdded"); 106 107 } 108 }); 109 110 // 注册ctrl+v事件,每次添加都是在文档html末尾追加的方式,所以可以多次粘贴 111 editorPane_1.registerKeyboardAction(new ActionListener() { 112 113 @Override 114 public void actionPerformed(ActionEvent e) { 115 DataFlavor[] flavots = Toolkit.getDefaultToolkit().getSystemClipboard().getAvailableDataFlavors(); 116 for (DataFlavor dataFlavor : flavots) { 117 try { 118 119 // 获取剪贴板数据 120 Object obj = Toolkit.getDefaultToolkit().getSystemClipboard().getData(dataFlavor); 121 System.out.println(obj.getClass()); 122 // 如果是文字内容 123 if (obj instanceof InputStream) { 124 InputStream bts = (InputStream) obj; 125 editorPane_1.setText(editorPane_1.getText().replace("</body>", IOUtils.readLines(bts, "utf-8").toString().replaceAll("\n", "<br/></body>"))); 126 } else if (obj instanceof Collection<?>) { 127 // 多文件内容 128 Collection<?> c = (Collection<?>) obj; 129 String s = ""; 130 131 for (Object object : c) { 132 File f = (File) object; 133 if (FilenameUtils.getExtension(f.getName()).equals("jpg")) { 134 s += "<img style=\\\"height:300px;width:200px\\\" src='file:///" + f.getAbsolutePath() + "'/><br/>"; 135 } 136 } 137 editorPane_1.setText(s.replace("</body>", s + "</br></body>")); 138 } else if (obj instanceof BufferedImage) { 139 // 截图内容 140 BufferedImage img = (BufferedImage) obj; 141 String savePath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); 142 savePath = FilenameUtils.separatorsToUnix(savePath); 143 System.out.println(savePath); 144 File file = new File(savePath + "/" + System.currentTimeMillis() + ".png"); 145 ImageIO.write(img, "png", file); 146 147 editorPane_1.setText(editorPane_1.getText().replace("</body>", "<img style=\"height:300px;width:200px\" src='file:///" + file.getAbsolutePath() + "'/><br/></body>")); 148 } 149 } catch (HeadlessException e1) { 150 e1.printStackTrace(); 151 } catch (UnsupportedFlavorException e1) { 152 e1.printStackTrace(); 153 } catch (IOException e1) { 154 e1.printStackTrace(); 155 } 156 } 157 } 158 }, aKeyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW); 159 JButton button = new JButton("发送"); 160 161 // 点击发送按钮的事件 162 button.addActionListener(new ActionListener() { 163 public void actionPerformed(ActionEvent e) { 164 // 清空下面的内容,设置上面的内容 165 String s = editorPane_1.getText(); 166 editorPane_1.setText(""); 167 editorPane.setText(s); 168 } 169 }); 170 171 // 注册ctrl+回车事件 172 button.registerKeyboardAction(new ActionListener() { 173 public void actionPerformed(ActionEvent e) { 174 // 清空下面的内容,设置上面的内容 175 String s = editorPane_1.getText(); 176 editorPane_1.setText(""); 177 editorPane.setText(s); 178 } 179 }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, ActionEvent.CTRL_MASK, true), JComponent.WHEN_IN_FOCUSED_WINDOW); 180 button.setBounds(628, 544, 93, 23); 181 frame.getContentPane().add(button); 182 } 183}
View Code
