用户选择的图像可以直接插入到 JEditorPane 中吗?

发布于 2024-09-03 17:54:02 字数 1422 浏览 6 评论 0原文

我想做的是打开一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦明 2024-09-10 17:54:02

使用 JTextPane 更容易。然后您可以在文本中的任何位置使用 insertIcon(...) 。

编辑:

我在尝试操作 HTML 方面从未有过太多运气,但我之前使用过如下代码:

HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
text = "<a href=\"abc\">hyperlink</a>";
editorKit.insertHTML(doc, textPane.getCaretPosition(), text, 0, 0, HTML.Tag.A);

因此,大概该代码与 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:

HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
text = "<a href=\"abc\">hyperlink</a>";
editorKit.insertHTML(doc, textPane.getCaretPosition(), text, 0, 0, HTML.Tag.A);

So presumably the code would be similiar for the IMG tag.

浅忆 2024-09-10 17:54:02

这应该可以做到:

mainText.setContentType("text/html");
String image = String.format("<img src=\"%s\">", imageChooser.getSelectedFile());
mainText.setText(image);

This should do it:

mainText.setContentType("text/html");
String image = String.format("<img src=\"%s\">", imageChooser.getSelectedFile());
mainText.setText(image);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文