加载jcef浏览器窗口后无法切换JPanel

时间:2015-06-22 13:09:53

标签: swing webbrowser-control chromium chromium-embedded

我正在尝试在我的swing应用程序中使用jcef浏览器但遇到问题。 首先,我无法在jFrame上添加jcef浏览器作为JPanel组件。然后我尝试直接在jframe上添加 [code] getContentPane()。add(browser.getUIComponent(),BorderLayout.CENTER); [/ code]

现在当浏览器窗口在JFrame中加载时,如果我想与其他Jpanel切换,那么它无法以任何方式工作 加载CEF浏览器后我无法切换屏幕。任何人都可以指出我需要做什么。这是我的测试jframe。

import org.cef.CefApp;
import org.cef.CefClient;
import org.cef.browser.CefBrowser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CardLayoutTst extends JFrame {
static CefBrowser browser = null;
static CefClient client = null;
private static final long serialVersionUID = 1L;
private JPanel cardPanel, jp1, jp2, buttonPanel;
private JLabel jl1, jl2;
private JButton btn1, btn2;
private CardLayout cardLayout = new CardLayout();

public CardLayoutTst() {
    setTitle("Test med CardLayout");
    setSize(400, 300);
    cardPanel = new JPanel();
    buttonPanel = new JPanel();
    cardPanel.setLayout(cardLayout);
    jp1 = new JPanel();
    jp2 = new JPanel();
    jl1 = new JLabel("Card 1");
    jl2 = new JLabel("Card 2");
    jp1.add(jl1);
    jp2.add(jl2);
    cardPanel.add(jp1, "1");
    cardPanel.add(browser.getUIComponent(), "2");
    btn1 = new JButton("Show Card 1");
    btn1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            cardLayout.show(cardPanel, "1");
        }
    });
    btn2 = new JButton("Show Card 2");
    btn2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            cardLayout.show(cardPanel, "2");
        }
    });
    buttonPanel.add(btn1);
    buttonPanel.add(btn2);
    add(cardPanel, BorderLayout.NORTH);
    add(buttonPanel, BorderLayout.SOUTH);

    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            CefApp.getInstance().dispose();
            dispose();
        }
    });

}

public static void main(String[] args) {
    client = CefApp.getInstance().createClient();
    browser = client.createBrowser("http://www.google.com", false, false);

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            CardLayoutTst frame = new CardLayoutTst();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });
}

}

1 个答案:

答案 0 :(得分:1)

有点晚了,但我遇到了同样的问题。我的一个朋友解决了这个问题。

您必须创建CefSettings,将windowless_rendering_enabled设置为false并将此设置对象传递给getInstance调用:

files:
  "/tmp/application.properties" :
    mode: "000666"
    owner: webapp
    group: webapp
    source: 'https://s3.amazonaws.com/com.mycompany.mybucket/`{"Ref": "AWSEBEnvironmentName" }`/application-`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "ENVIRONMENT ", "DefaultValue": "dev"}}`.properties'
    authentication: S3Auth