Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


JxBrowser is based on Chromium engine. There are lots of command lines (switches) which can be used with Chromium. Some change behavior of features, others are for debugging or experimenting.(JxBrowser基于Chromium引擎。 Chromium可以使用许多命令行(开关)。一些用来更改功能的行为,其他用于调试或试验。)


JxBrowser provides API that allows configuring Chromium with command line switches. The list of command line switches you can provide before you create any Browser instance. The following code demonstrates how to do it:  (JxBrowser提供的API允许使用命令行开关配置Chromium。创建任何浏览器实例之前,可以提供的命令行开关列表。以下代码演示了如何执行此操作:)

BrowserPreferences.setChromiumSwitches(
        "--disable-web-security",
        "--allow-file-access-from-files");

 The following sample demonstrates how to disable web security and allow file access from HTML via appropriate Chromium switches: (以下示例演示了如何通过适当的Chromium开关禁用Web安全并允许通过HTML访问文件:)

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserPreferences;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;<span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span>

import javax.swing.*;
import java.awt.*;

/**
 * The sample demonstrates how to enable access to local files from a web page.
 */
public class LoadLocalFilesSample {
    public static void main(String[] args) {
        BrowserPreferences.setChromiumSwitches("--disable-web-security", "--allow-file-access-from-files");

        Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browserView, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.loadHTML("<html><body>Image:<img src='file:///C:\\image.jpg'></body></html>");
    }
}