当前位置: 技术问答>java相关
请问如何在 JTextPane 中嵌入图片实现多媒体文档?
来源: 互联网 发布时间:2014-12-30
本文导语: 我的想法是在一纯文本中加入一个插入图片的标记(和html的标记有点类似),而我的文本编辑器读到这个标记后把图片和文本一起显示在 JTextPane中。请问一下如何可以实现? | //不太明白...
我的想法是在一纯文本中加入一个插入图片的标记(和html的标记有点类似),而我的文本编辑器读到这个标记后把图片和文本一起显示在 JTextPane中。请问一下如何可以实现?
|
//不太明白你的意思,不过希望这个小程序can help you.
import java.io.File;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InsertPic extends JFrame {
private JFileChooser chooser = new JFileChooser();
private JTextPane textPane = new JTextPane();
public InsertPic() {
addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
JMenuBar menuBar = new JMenuBar();
JMenu insertMenu = new JMenu("Insert");
JMenuItem imageItem = new JMenuItem("image");
JMenuItem chooserItem = new JMenuItem("color chooser");
insertMenu.add(imageItem);
insertMenu.add(chooserItem);
menuBar.add(insertMenu);
setJMenuBar(menuBar);
contentPane.add(textPane, BorderLayout.CENTER);
chooserItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JColorChooser chooser = new JColorChooser();
chooser.setMaximumSize(chooser.getPreferredSize());
textPane.insertComponent(chooser);
}
});
imageItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int option = chooser.showDialog(InsertPic.this,"Pick An Image");
if(option == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
if(file != null) {
textPane.insertIcon(new ImageIcon(file.getPath()));
}
}
}
});
}
public static void main(String args[]) {
new InsertPic().show();
}
}
import java.io.File;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InsertPic extends JFrame {
private JFileChooser chooser = new JFileChooser();
private JTextPane textPane = new JTextPane();
public InsertPic() {
addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
JMenuBar menuBar = new JMenuBar();
JMenu insertMenu = new JMenu("Insert");
JMenuItem imageItem = new JMenuItem("image");
JMenuItem chooserItem = new JMenuItem("color chooser");
insertMenu.add(imageItem);
insertMenu.add(chooserItem);
menuBar.add(insertMenu);
setJMenuBar(menuBar);
contentPane.add(textPane, BorderLayout.CENTER);
chooserItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JColorChooser chooser = new JColorChooser();
chooser.setMaximumSize(chooser.getPreferredSize());
textPane.insertComponent(chooser);
}
});
imageItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int option = chooser.showDialog(InsertPic.this,"Pick An Image");
if(option == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
if(file != null) {
textPane.insertIcon(new ImageIcon(file.getPath()));
}
}
}
});
}
public static void main(String args[]) {
new InsertPic().show();
}
}