java swing 网站_java swing 浏览网页

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的版本不够,会报以下错误

9c1aaf494ee9ce1d8b90b69de5501c3e.png

附上swt4.3的jar包

8fe5965d3fc3f22c35ca7f6eee8d4325.png

大小: 246.8 KB

swt.jar (1.8 MB)

下载次数: 168

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2014-06-22 10:17

浏览 6179

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值