java - 如何将JxBrowser背景设置为透明

标签 java swing jxbrowser

我使用 JxBrowser 在 View 中加载一些内容。

我愿意:

//create frame
JFrame frame = new JFrame("Some title");

//create browser
Browser browser = new Browser();
BrowserView view = new BrowserView(browser);

//remove default window controls
frame.setUndecorated(true);

//add browser view to frame
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

//load url
browser.loadURL("http://example.com");

当我运行时,我的浏览器 View 是白色的。我尝试将框架背景设置为透明:

frame.setBackground(new Color(0, 0, 0, 0.0f));

但它隐藏了整个窗口。

有什么想法吗?如何为浏览器 View 设置透明背景?

最佳答案

刚刚找到这个例子,这就是设置透明背景的方法:

BrowserPreferences preferences = browser.getPreferences();
preferences.setTransparentBackground(true);
browser.setPreferences(preferences);

在 JxBrowser 6.10 及更高版本中,有一些功能允许在网页上启用透明背景支持。下面的例子演示了如何启用透明背景的支持:

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserPreferences;
import com.teamdev.jxbrowser.chromium.BrowserType;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

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

/**
 * This sample demonstrates how to create Browser instance with transparent background.
 */
public class TransparentPageSample {
    public static void main(String[] args) {
        Browser browser = new Browser(BrowserType.LIGHTWEIGHT);

        // Enable support of transparent background
        BrowserPreferences preferences = browser.getPreferences();
        preferences.setTransparentBackground(true);
        browser.setPreferences(preferences);

        BrowserView view = new BrowserView(browser);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBackground(new Color(0, 150, 255, 255));
        panel.add(view, BorderLayout.CENTER);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(300, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.loadHTML("<html><body>"
                + "<div style='background: yellow; opacity: 0.7;'>\n"
                + "    This text is in the yellow half-transparent div and should "
                + "    appear as in the green due to the blue JPanel behind."
                + "</div>\n"
                + "<div style='background: red;'>\n"
                + "    This text is in the red opaque div and should appear as is."
                + "</div>\n"
                + "<div>\n"
                + "    This text is in the non-styled div and should appear as in "
                + "    the blue due to the blue JPanel behind."
                + "</div>\n"
                + "</body></html>");
    }
}

https://jxbrowser.support.teamdev.com/support/solutions/articles/9000104967-transparent-background

关于java - 如何将JxBrowser背景设置为透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36629842/

相关文章:

java - JFXTextField 不适用于 JxBrowser

java - JxBrowser 6.1 JavaScript Java Bridge API 不起作用

java - 从字节数组中删除多余的 "empty"字符并转换为字符串

java - 一个类的引用?它是如何锻炼内存力的?

java - Jpanel倒计时1小时?

java - 使用 BoxLayout 时 JPanel 不居中

java - 捕获重量级java组件?

java - 如何增加 Azure 应用服务中托管的 Java Spring Boot 应用程序的最大 HTTP header 大小(部署为 .WAR)?

java - 使用自定义类加载器将类转换为对象

java - 如何正确地将 JPanel 放置到 JFrame 上?