用户选择的图像可以直接插入到 JEditorPane 中吗?
我想做的是打开一个 JFilechooser 来过滤 jpeg、gif 和 png 图像,然后获取用户的选择并将其插入到 JEditorPane 中。这可以做到吗?或者我正在尝试一些不可能的事情?这是我的程序的示例。(插入是 JMenuItem,mainText 是 JEditorPane)
insert.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser imageChooser = new JFileChooser();
imageChooser.setFileFilter(new FileNameExtensionFilter("Image Format","jpg","jpeg","gif","png"));
int choice = imageChooser.showOpenDialog(mainText);
if (choice == JFileChooser.APPROVE_OPTION) {
mainText.add(imageChooser.getSelectedFile());
}
}
});
我尝试做的是使用 add 方法,我知道这是错误的,但只是为了让您了解我正在尝试做什么。 在你抱怨之前,我对代码格式感到抱歉,我真的不知道什么是好或坏风格的所有约定。 非常感谢。
这是我用来保存 html 文件的代码部分。
else if (e.getSource() == save) {
JFileChooser saver = new JFileChooser();
saver.setFileFilter(new FileNameExtensionFilter(".html (webpage format)" , "html"));
int option = saver.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(saver.getSelectedFile().getPath()));
out.write(mainText.getText());
out.close();
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
}
}
What I am trying to do is open up a JFilechooser that filters jpeg,gif and png images, then gets the user's selection and inserts it into the JEditorPane. Can this be done? or am i attempting something impossible? Here is a sample of my program.(insert is a JMenuItem and mainText is a JEditorPane)
insert.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser imageChooser = new JFileChooser();
imageChooser.setFileFilter(new FileNameExtensionFilter("Image Format","jpg","jpeg","gif","png"));
int choice = imageChooser.showOpenDialog(mainText);
if (choice == JFileChooser.APPROVE_OPTION) {
mainText.add(imageChooser.getSelectedFile());
}
}
});
What i tried to do is use the add method, i know it's wrong but just to give you an idea of what i'm trying to do.
Before you complain, i'm sorry about the code formatting, i don't really know all the conventions of what is considered good or bad style.
Thank you very much.
This is the part of the code i use to save the html file.
else if (e.getSource() == save) {
JFileChooser saver = new JFileChooser();
saver.setFileFilter(new FileNameExtensionFilter(".html (webpage format)" , "html"));
int option = saver.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(saver.getSelectedFile().getPath()));
out.write(mainText.getText());
out.close();
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 JTextPane 更容易。然后您可以在文本中的任何位置使用 insertIcon(...) 。
编辑:
我在尝试操作 HTML 方面从未有过太多运气,但我之前使用过如下代码:
因此,大概该代码与 IMG 标签类似。
Its easier to just use a JTextPane. Then you can use insertIcon(...) anywhere in the text.
Edit:
I have never had much luck trying to manipulate HTML but I've used code like the following before:
So presumably the code would be similiar for the IMG tag.
这应该可以做到:
This should do it: