java swing 浏览网页
来源:互联网 发布:单片机一秒延迟怎么写 编辑:程序博客网 时间:2024/06/11 06:32
目前来说,java swing 中嵌入网页,使用DJNativeSwing是最有效地方法。DJNativeSwing其实是通过内部桥来调用SWT的Browser控件。
附上代码:
需要导入的jar包有:
DJNativeSwing-SWT.jar
DJNativeSwing.jar
swt.jar
运行时如果swt的版本不够,会报以下错误
附上swt4.3的jar包
附上代码:
public class Mains extends JPanel {private static final long serialVersionUID = 1L;final static public String LS = System.getProperty("line.separator", "/n");final static public String FS = System.getProperty("file.separator", "//");final static StringBuffer jsDimension;static {jsDimension = new StringBuffer();jsDimension.append("var width = 0;").append(LS);jsDimension.append("var height = 0;").append(LS);jsDimension.append("if(document.documentElement) {").append(LS);jsDimension.append(" width = Math.max(width, document.documentElement.scrollWidth);").append(LS);jsDimension.append(" height = Math.max(height, document.documentElement.scrollHeight);").append(LS);jsDimension.append("}").append(LS);jsDimension.append("if(self.innerWidth) {").append(LS);jsDimension.append(" width = Math.max(width, self.innerWidth);").append(LS);jsDimension.append(" height = Math.max(height, self.innerHeight);").append(LS);jsDimension.append("}").append(LS);jsDimension.append("if(document.body.scrollWidth) {").append(LS);jsDimension.append(" width = Math.max(width, document.body.scrollWidth);").append(LS);jsDimension.append(" height = Math.max(height, document.body.scrollHeight);").append(LS);jsDimension.append("}").append(LS);jsDimension.append("return width + ':' + height;");}public Mains(final String url, final int maxWidth, final int maxHeight) {super(new BorderLayout());JPanel webBrowserPanel = new JPanel(new BorderLayout());final String fileName = System.currentTimeMillis() + ".jpg";final JWebBrowser webBrowser = new JWebBrowser(null);webBrowser.setBarsVisible(false);webBrowser.navigate(url);webBrowserPanel.add(webBrowser, BorderLayout.CENTER);add(webBrowserPanel, BorderLayout.CENTER);JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));webBrowser.addWebBrowserListener(new WebBrowserAdapter() {public void loadingProgressChanged(WebBrowserEvent e) {//判断是否加载完成if (e.getWebBrowser().getLoadingProgress() == 100) {//进行网页截图//String result = (String) webBrowser//.executeJavascriptWithResult(jsDimension.toString());//int index = result == null ? -1 : result.indexOf(":");//NativeComponent nativeComponent = webBrowser//.getNativeComponent();//Dimension originalSize = nativeComponent.getSize();//Dimension imageSize = new Dimension(Integer.parseInt(result//.substring(0, index)), Integer.parseInt(result//.substring(index + 1)));//imageSize.width = Math.max(originalSize.width,//imageSize.width + 50);//imageSize.height = Math.max(originalSize.height,//imageSize.height + 50);//nativeComponent.setSize(imageSize);//BufferedImage image = new BufferedImage(imageSize.width,//imageSize.height, BufferedImage.TYPE_INT_RGB);//nativeComponent.paintComponent(image);//nativeComponent.setSize(originalSize);//// //if (imageSize.width > maxWidth//|| imageSize.height > maxHeight) {//// //image = image.getSubimage(0, 0, maxWidth, maxHeight);///*// * int width = image.getWidth(), height =// * image .getHeight(); AffineTransform tx = new// * AffineTransform(); tx.scale((double) maxWidth /// * width, (double) maxHeight / height);// * AffineTransformOp op = new AffineTransformOp(tx,// * AffineTransformOp.TYPE_NEAREST_NEIGHBOR); //锟斤拷小 image// * = op.filter(image, null);// *///}//try {//ImageIO.write(image, "jpg", new File(fileName));//} catch (IOException ex) {//ex.printStackTrace();//}}}});add(panel, BorderLayout.SOUTH);}public static void main(String[] args) {NativeInterface.open();SwingUtilities.invokeLater(new Runnable() {public void run() {JFrame frame = new JFrame("java swing 浏览器");frame.getContentPane().add(new Mains("http://www.baidu.com", 1024, 768),BorderLayout.CENTER);frame.setSize(800, 600);//frame.invalidate();//frame.pack();frame.setVisible(true);}});NativeInterface.runEventPump();}
需要导入的jar包有:
DJNativeSwing-SWT.jar
DJNativeSwing.jar
swt.jar
运行时如果swt的版本不够,会报以下错误
附上swt4.3的jar包
阅读全文
0 0
- java swing 浏览网页
- 浏览网页
- java web 纯前段实现浏览网页下载
- 浏览VB6网页类
- T39+Bluetooth浏览网页
- 简单限制浏览网页
- 清网页浏览痕迹
- 浏览网页出现乱码
- 加快网页浏览速度
- 用键盘浏览网页
- w3m 进行网页浏览
- powerpoint2007中浏览网页
- SAP浏览网页
- C#浏览网页源代码
- 使用WebView浏览网页
- 用SAP浏览网页
- 3 AsyncTask 浏览网页
- 易语言浏览网页
- swing JPanel设置背景图 拉伸、平铺、居中
- SAP UI5初步结构分析研究(二)
- swing JPanel画渐变的背景色
- 最实用的 Linux 命令行使用技巧
- java swing MenuItem乱码
- java swing 浏览网页
- 安卓开发经验——Android Studio的project中两个build.gradle配置的区别
- java提高篇(二四)-----HashSet
- 进度条弹窗
- Java Swing中JFrame设置自定义图标
- pyhs2搭建详细过程
- pip不是内部或外部命令
- 将jar文件打包成exe安装文件(一)
- 开篇总结