Java Swing嵌入浏览器.docx
《Java Swing嵌入浏览器.docx》由会员分享,可在线阅读,更多相关《Java Swing嵌入浏览器.docx(14页珍藏版)》请在冰豆网上搜索。
JavaSwing嵌入浏览器
importjava.awt.BorderLayout;
importjava.awt.Toolkit;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.RandomAccessFile;
import.HttpURLConnection;
import.URL;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Date;
importjava.util.List;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.SwingUtilities;
importmon.UIUtils;
importchrriis.dj.nativeswing.swtimpl.NativeInterface;
importponents.JWebBrowser;
importponents.WebBrowserCommandEvent;
importponents.WebBrowserEvent;
importponents.WebBrowserListener;
importponents.WebBrowserNavigationEvent;
importponents.WebBrowserWindowOpeningEvent;
importponents.WebBrowserWindowWillOpenEvent;
importcom.jacob.activeX.ActiveXComponent;
import.ComThread;
import.Dispatch;
import.Variant;
importcom.mohe.utils.system.L;
/**
*主窗体
*
*@authordxm
*
*/
publicclassMainFrameextendsJFrame{
privatestaticfinallongserialVersionUID=-6348199501339114346L;
//最后操作时间
publiclonglastTime=newDate().getTime();
//浏览器窗体
privateJWebBrowserwebBrowser=null;
privateJLabellbl=null;
privateJPanelpanel=null;
publicintpageFlag=1;
/**
*构造器
*/
publicMainFrame(){
initData();
initGUI();
addListance();
}
privatevoidinitData(){
Toolkittoolkit=Toolkit.getDefaultToolkit();
this.setUndecorated(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,500);
this.setLocationRelativeTo(null);
this.setSize(toolkit.getScreenSize());
this.setLocation(0,0);
webBrowser=newJWebBrowser();
webBrowser.navigate(Config.local+Config.index);
webBrowser.setBarsVisible(false);
webBrowser.setMenuBarVisible(false);
webBrowser.setLocationBarVisible(false);
webBrowser.setButtonBarVisible(false);
webBrowser.setStatusBarVisible(false);
ImageIconicon=newImageIcon(System.getProperty("user.dir")+"\\城市.gif");
Fileimg=newFile(System.getProperty("user.dir")+"\\"+Config.img);
if(img.exists()){
icon=newImageIcon(img.getAbsolutePath());
}
lbl=newJLabel(icon);
panel=newJPanel(newBorderLayout());
}
privatevoidinitGUI(){
panel.add(webBrowser,BorderLayout.CENTER);
this.setContentPane(panel);
}
privatevoidaddListance(){
webBrowser.addWebBrowserListener(newWebBrowserListener(){
@Override
publicvoidwindowWillOpen(WebBrowserWindowWillOpenEventarg0){
lastTime=newDate().getTime();
L.info("___________windowWillOpen");
}
@Override
publicvoidwindowOpening(WebBrowserWindowOpeningEventarg0){
lastTime=newDate().getTime();
L.info("___________windowOpening");
}
@Override
publicvoidwindowClosing(WebBrowserEventarg0){
lastTime=newDate().getTime();
L.info("___________windowClosing");
}
@Override
publicvoidtitleChanged(WebBrowserEventarg0){
lastTime=newDate().getTime();
L.info("___________titleChanged");
}
@Override
publicvoidstatusChanged(WebBrowserEventarg0){
lastTime=newDate().getTime();
L.info("___________statusChanged");
}
@Override
publicvoidlocationChanging(WebBrowserNavigationEventarg0){
lastTime=newDate().getTime();
L.info("___________locationChanging");
}
@Override
publicvoidlocationChanged(WebBrowserNavigationEventarg0){
lastTime=newDate().getTime();
L.info("___________locationChanged");
//判断当前页面等级
StringlocationURL=arg0.getNewResourceLocation();
intindex=locationURL.indexOf("pageFlag=");
if(index!
=-1){
pageFlag=Integer.parseInt(locationURL.substring(index+9,index+10));
}else{
pageFlag=1;
}
System.out.println("____________________pageFlag:
"+pageFlag);
//判断下载文件进行打印
index=locationURL.indexOf("fileName=");
System.out.println("____________________fileIndex:
"+index);
if(index!
=-1){
Stringfs=locationURL.substring(index+9);
L.info("______________________fileName="+fs);
String[]fns=fs.split(",");
try{
Listflist=downNetFile(Arrays.asList(fns));
doPrintDoc(flist);
}catch(IOExceptione){
e.printStackTrace();
}
}
SwingUtilities.invokeLater(newRunnable(){
publicvoidrun(){
webBrowser.setVisible(false);
webBrowser.setVisible(true);
webBrowser.updateUI();
webBrowser.repaint();
panel.updateUI();
panel.repaint();
panel.validate();
}
});
}
@Override
publicvoidlocationChangeCanceled(WebBrowserNavigationEventarg0){
lastTime=newDate().getTime();
L.info("___________locationChangeCanceled");
}
@Override
publicvoidloadingProgressChanged(WebBrowserEventarg0){
lastTime=newDate().getTime();
L.info("___________loadingProgressChanged");
}
@Override
publicvoidcommandReceived(WebBrowserCommandEventarg0){
lastTime=newDate().getTime();
L.info("___________commandReceived");
}
});
lbl.addMouseListener(newMouseListener(){
@Override
publicvoidmouseReleased(MouseEventarg0){
}
@Override
publicvoidmousePressed(MouseEventarg0){
}
@Override
publicvoidmouseExited(MouseEventarg0){
}
@Override
publicvoidmouseEntered(MouseEventarg0){
}
@Override
publicvoidmouseClicked(MouseEventarg0){
change
(2);
}
});
}
/**
*屏保切换
*/
publicvoidchange(finalinttype){
lastTime=newDate().getTime();
SwingUtilities.invokeLater(newRunnable(){
publicvoidrun(){
if(type==1){
//二级,三级页面跳首页
if(pageFlag==2||pageFlag==3){
webBrowser.navigate(Config.local+Config.index);
}
//else{
//webBrowser.setVisible(false);
//panel.add(lbl,BorderLayout.CENTER);
//}
}else{
panel.remove(lbl);
webBrowser.setVisible(true);
}
panel.validate();
}
});
}
/**
*下载网络文件
*
*@paramnameList
*@throwsIOException
*/
privateListdownNetFile(ListnameList)throwsIOException{
L.info("________________________downNetFile1");
if(null==nameList||nameList.isEmpty()){
returnnewArrayList();
}
Listflist=newArrayList();
for(Stringname:
nameList){
L.info("________________________downNetFile2");
URLurl=newURL(Config.local+"/excel/"+name);
HttpURLConnectionhttpConnection=(HttpURLConnection)url.openConnection();
//设置请求信息
httpConnection.setRequestProperty("GET","/down.zipHTTP/1.1");
//设置接受信息
httpConnection.setRequestProperty("Accept","image/gif,image/x-xbitmap,application/msword");
//设置连接信息
httpConnection.setRequestProperty("Connection","Keep-Alive");
//获得输入流
InputStreaminput=httpConnection.getInputStream();
//创建随机文件
flist.add(newFile(name));
RandomAccessFileoSavedFile=newRandomAccessFile(name,"rw");
byte[]b=newbyte[1024];
intnRead;
//从输入流中读入字节流,然后写到文件中
while((nRead=input.read(b,0,1024))>0){
oSavedFile.write(b,0,nRead);
}
input.close();
oSavedFile.close();
httpConnection.disconnect();
}
returnflist;
}
privatevoiddoPrintDoc(ListfileList){
if(null==fileList||fileList.isEmpty()){
return;
}
//初始化组件
ComThread.InitSTA();
L.info("___________________startPrint1");
for(Filefile:
fileList){
if(file==null||!
file.exists()){
return;
}
L.info("___________________startPrint2:
"+file.getAbsolutePath());
intindex=file.getName().indexOf(".");
StringextName=file.getName().toUpperCase().substring(index+1);
StringcomApp="Word.Application";
Stringproperty="Documents";
if(extName.equals("DOC")||extName.equals("DOCX")||extName.equals("WPS")){
comApp="Word.Application";
property="Documents";
}elseif(extName.equals("XLS")||extName.equals("XLSX")||extName.equals("ET")){
comApp="Excel.Application";
property="Workbooks";
}elseif(extName.equals("PPT")||extName.equals("PPTX")||extName.equals("DPS")){
comApp="PowerPoint.Application";
property="Presentations";
}
L.info("___________________startPrint3:
"+comApp);
ActiveXComponentaxc=newActiveXComponent(comApp);
try{
if(!
property.equals("Presentations")){
Dispatch.put(axc,"Visible",newVariant(false));
}
Dispatchdocument=axc.getProperty(property).toDispatch();
Dispatchdoc=null;
if(property.equals("Presentations")){
doc=Dispatch.call(document,"Open",file.getAbsolutePath(),true,true,false).toDispatch();
}else{
doc=Dispatch.invoke(document,"Open",Dispatch.Method,newObject[]{file.getAbsolutePath()},newint[1]).toDispatch();
}
L.info("___________________startPrint3:
"+file.getAbsolutePath());
Dispatch.call(doc,"PrintOut");
Dispatch.call(doc,"Close");
if(!
property.equals("Presentations")){
axc.invoke("Quit",newVariant[]{});
}
}catch(Exceptione){
e.printStackTrace();
}finally{
comApp="";
property="";
}
}
ComThread.Release();
}
publicstaticvoidmain(String[]args){