1

JxBrowserビューにいくつかのコンテンツをロードするために使用します。

そうです:

//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");

実行すると、ブラウザの表示が白くなります。フレームの背景を透明に設定しようとしました:

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

しかし、それはウィンドウ全体を隠します。

何か案は?ブラウザビューに透明な背景を設定する方法は?

4

1 に答える 1

1

この例を見つけたところ、透明な背景を設定する方法は次のとおりです。

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

JxBrowser 6.10 以降では、Web ページで透明な背景のサポートを有効にする機能があります。次の例は、透明な背景のサポートを有効にする方法を示しています。

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

于 2017-01-23T10:45:46.180 に答える