当前位置: 首页 > 工具软件 > JxBrowser > 使用案例 >

在SWING中使用JxBrowser浏览网页

漆雕奇
2023-12-01

import java.awt.*;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigInteger;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
 
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.be;
import com.teamdev.jxbrowser.chromium.swing.BrowserView; 
 /*
  * jxbrowser-6.24.3.jar
  * jxbrowser-win64-6.24.3.jar
  * 创建文本文件:META-INF/teamdev.licenses
内容:
Product: JxBrowser
Version: 6.x
Licensed to: Kagura.me
License type: Enterprise
License info: JxBrowser License
Expiration date: 01-01-9999
Support expiration date: NO SUPPORT
Generation date: 01-01-1970
Platforms: win32/x86;win32/x64;mac/x86;mac/x64;linux/x86;linux/x64
Company name: TeamDev Ltd.
SigB: 1
SigA: 1
* */
  
public class indexMain extends JFrame
{ 
	static String  Jdir = ""; // Jdir = getDir();
    static String  PAGEURL = "http://www.baidu.com";
	static {
	    try {
	        Field e = be.class.getDeclaredField("e");
	        e.setAccessible(true);
	        Field f = be.class.getDeclaredField("f");
	        f.setAccessible(true);
	        Field modifersField = Field.class.getDeclaredField("modifiers");
	        modifersField.setAccessible(true);
	        modifersField.setInt(e, e.getModifiers() & ~Modifier.FINAL);
	        modifersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
	        e.set(null, new BigInteger("1"));
	        f.set(null, new BigInteger("1"));
	        modifersField.setAccessible(false);
	    } catch (Exception e1) {
	        e1.printStackTrace();
	    }
	}

    public static String getCFG() {
   	    String ccc= "";
   	    String jpath = getDir();
   	    Jdir = jpath;
   	    jpath = jpath + "\\cfg.txt"; 
   	    ccc = Util.getText(jpath); 
   	    return ccc;
    }

    public static String getDir() {
 	   String jpath = jarTools.getJarPath();
   	    if(jpath.indexOf(".jar")==(jpath.length()-4)) {
   	    	 //打包后地址
              jpath = jpath.substring(0,jpath.lastIndexOf("\\"));
   	    }else {
   	         //打包前地址	
   	         jpath = jpath.replace("\\build\\classes", "");	
   	    } 
   	    return jpath ;
    }
    
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);
        JFrame frame = new JFrame("JxBrowser"); 
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        String cfg = getCFG();
    	System.out.println("Jdir="+Jdir);
    	System.out.println("cfg="+cfg);
    	PAGEURL = Util.getMYXML(cfg, "homeurl", "<>");
    	System.out.println("PAGEURL="+PAGEURL);
        browser.loadURL(PAGEURL);
    }  
}

 类似资料: