将 html 框架集代码从内存加载到 Java SWT 浏览器

发布于 2024-12-01 15:51:00 字数 86 浏览 0 评论 0原文

我有一个用 SWT 编写的简单 html 编辑器,它是浏览器控件。我想知道是否有一种方法可以从内存加载html框架页面的代码,而不将html文件保存在硬盘上?

I have a simple html editor written with SWT and it's Browser control. I would like to know if there is a way to load the code for html frame pages from memory, without saving the html file on the hdd?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

为你鎻心 2024-12-08 15:51:01

您正在寻找的代码是 SWT 浏览器小部件片段之一。请参阅 从内存中渲染 HTML

import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet136 {
    public static void main(String [] args) {
        String html = "<HTML><HEAD><TITLE>HTML Test</TITLE></HEAD><BODY>";
        for (int i = 0; i < 100; i++) html += "<P>This is line "+i+"</P>";
        html += "</BODY></HTML>";

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        Browser browser;
        try {
            browser = new Browser(shell, SWT.NONE);
        } catch (SWTError e) {
            System.out.println("Could not instantiate Browser: " + e.getMessage());
            display.dispose();
            return;
        }
        browser.setText(html);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

还有一些其他片段可能对您来说很方便。检查 SWT 浏览器的小部件片段

The code which are you looking for is one of the SWT Browser widget snippets. See render HTML from memory.

import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet136 {
    public static void main(String [] args) {
        String html = "<HTML><HEAD><TITLE>HTML Test</TITLE></HEAD><BODY>";
        for (int i = 0; i < 100; i++) html += "<P>This is line "+i+"</P>";
        html += "</BODY></HTML>";

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        Browser browser;
        try {
            browser = new Browser(shell, SWT.NONE);
        } catch (SWTError e) {
            System.out.println("Could not instantiate Browser: " + e.getMessage());
            display.dispose();
            return;
        }
        browser.setText(html);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

There are few others snippets which may became handy for you.. Check SWT Browser's widget snippets.

只涨不跌 2024-12-08 15:51:01

在内存中的 HTML 中添加一行即可解决问题。

例如,假设您的文件 page1.html 等位于本地计算机的 C:/myHTML 中,那么您需要将其添加到内存中的 HTML 字符串中,如下所示,

String html="<HTML><HEAD><TITLE>HTML Test</TITLE>"
            +"<BASE href=\"file:///C:myHTML\\/\" >"
            +"<HEAD>"
            +"<BODY><A src=\"page1.html\">Page1</A></BODY></HTML>"

这对我在 Win 7 上有效。

Add one line in your in memory HTML will do the trick.

For example, assume your files page1.html etc. locate in your local computer at C:/myHTML, then you need add this to your in memory HTML string as below,

String html="<HTML><HEAD><TITLE>HTML Test</TITLE>"
            +"<BASE href=\"file:///C:myHTML\\/\" >"
            +"<HEAD>"
            +"<BODY><A src=\"page1.html\">Page1</A></BODY></HTML>"

This works for me on Win 7.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文