获取JTextPane图片的路径及位置

  1. import java.awt.Container;
  2. import java.awt.Rectangle;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.Vector;
  6. import javax.swing.Icon;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JButton;
  9. import javax.swing.JFileChooser;
  10. import javax.swing.JFrame;
  11. import javax.swing.JScrollPane;
  12. import javax.swing.JTextPane;
  13. import javax.swing.text.BadLocationException;
  14. import javax.swing.text.StyleConstants;
  15. /**
  16.  * @author 李世貴 
  17.  * 來源:blog.csdn.net/lishigui
  18.  * 歡迎轉接,請保留作者和來源,謝謝!
  19.  */
  20. public class JTextPane2 extends JFrame{
  21.     private JTextPane mypane;
  22.     private JTextPane pane;
  23.     private JScrollPane scrollMypane;
  24.     private JScrollPane scrollPane;
  25.     
  26.     public JTextPane2() {
  27.         super("JTextPane Test");
  28.         Container container = getContentPane();
  29.         container.setLayout(null);
  30.         pane = new JTextPane();
  31.         pane.setEditable(false);
  32.         scrollPane = new JScrollPane(pane);
  33.         scrollPane.setBounds(new Rectangle(0,10,400,200));
  34.         JButton insertButton = new JButton("插入圖片");
  35.         insertButton.setBounds(new Rectangle(0,220,100,40));
  36.         
  37.         insertButton.addActionListener(new ActionListener(){
  38.             public void actionPerformed(ActionEvent e) {
  39.                 JFileChooser chooser = new JFileChooser();
  40.                 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  41.                 chooser.showOpenDialog(JTextPane2.this);
  42.                 mypane.insertIcon(new ImageIcon(chooser.getSelectedFile().toString()));
  43.             }
  44.         });
  45.         
  46.         mypane = new JTextPane();
  47.         scrollMypane = new JScrollPane(mypane);
  48.         scrollMypane.setBounds(new Rectangle(0,270,400,200));
  49.         
  50.         JButton sendButton = new JButton("發送");
  51.         sendButton.setBounds(new Rectangle(170,500,60,40));
  52.         sendButton.addActionListener(new ActionListener(){
  53.             public void actionPerformed(ActionEvent e) {
  54.                 Vector picVector = new Vector();
  55.                 for(int i = 0; i < mypane.getStyledDocument().getRootElements()[0].getElement(0).getElementCount(); i++){
  56.                     Icon icon = StyleConstants.getIcon(mypane.getStyledDocument().getRootElements()[0].getElement(0).getElement(i).getAttributes());
  57.                     if(icon != null){
  58.                         picVector.add(icon.toString());
  59.                     }
  60.                 }
  61.                 System.err.println("發送 JTextPane 的內容是 :/n"+mypane.getText());
  62.                 int k = 0;
  63.                 for(int i = 0; i < mypane.getText().length(); i++){
  64.                     if(mypane.getStyledDocument().getCharacterElement(i).getName().equals("icon")){
  65.                         System.err.println("你在第 " + i + " 位置插入了圖片,圖片的路徑是 :");
  66.                         pane.insertIcon(new ImageIcon(picVector.get(k).toString()));
  67.                         System.err.println(picVector.get(k++).toString());
  68.                     }else{
  69.                         try {
  70.                             pane.getStyledDocument().insertString(pane.getText().length(), mypane.getStyledDocument().getText(i,1), null);
  71.                         } catch (BadLocationException e1) {
  72.                             e1.printStackTrace();
  73.                         }
  74.                     }
  75.                 }
  76.                 picVector.clear();
  77.             }
  78.         });
  79.         container.add(scrollPane);
  80.         container.add(insertButton);
  81.         container.add(scrollMypane);
  82.         container.add(sendButton);
  83.         setSize(400,600);
  84.         setVisible(true);
  85.         
  86.     }
  87.     public static void main(String[] args) {
  88.         new JTextPane2().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  89.     }
  90. }

你可能感兴趣的:(原创java)