我正在尝试使用CEF在每个选项卡上创建一个浏览器,但我发现只有在选择了一个选项卡后才会加载内容。是否有任何方法可以在没有选中标签的情况下在后台加载标签的内容。
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Application extends JFrame {
String[] testUrls = {"http://bbc.co.uk","http://reddit.com"};
private Collection<BrowserThread> browserThreads;
public Application(){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace(System.out);
}
browserThreads = new ArrayList<BrowserThread>();
TabManager tabManager = new TabManager();
for (String u : testUrls){
BrowserThread bt = new BrowserThread(u);
bt.run();
tabManager.add(u,bt.getUI());
browserThreads.add(bt);
}
getContentPane().add(tabManager.getTabbedPane(), BorderLayout.CENTER);
pack();
setSize(400,400);
setLocationByPlatform(true);
setDefaultLookAndFeelDecorated(true);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
for(BrowserThread brt : browserThreads){
brt.destroy();
}
}
});
}
public void run(){}
public static void main(String[] args){
final Application app = new Application();
}
}
下一课
import javax.swing.*;
import java.awt.*;
public class TabManager {
JTabbedPane tabs = null;
public TabManager(){
tabs = new JTabbedPane();
}
public void add(String name, Component browser){
tabs.addTab(name,browser);
}
public JTabbedPane getTabbedPane(){
return tabs;
}
}
下一课
import org.cef.CefApp;
import java.awt.*;
public class BrowserThread implements Runnable {
private CefApp cefApp_;
private String url;
private Component ui;
public BrowserThread(String url){
this.cefApp_ = CefApp.getInstance();
this.url = url;
}
public void run(){
ui = cefApp_.createClient().createBrowser(url,false,false).getUIComponent();
}
public Component getUI(){
return ui;
}
public void destroy(){
cefApp_.dispose();
}
}