Java Swing嵌入浏览器

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserCommandEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserListener;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowOpeningEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowWillOpenEvent;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.mohe.utils.system.L;

/**
 * 主窗体
 *
 * @author dxm
 *
 */
public class MainFrame extends JFrame {

	private static final long serialVersionUID = -6348199501339114346L;

	// 最后操作时间
	public long lastTime = new Date().getTime();

	// 浏览器窗体
	private JWebBrowser webBrowser = null;
	private JLabel lbl = null;
	private JPanel panel = null;

	public int pageFlag = 1;

	/**
	 * 构造器
	 */
	public MainFrame() {
		initData();
		initGUI();
		addListance();
	}

	private void initData() {

		Toolkit toolkit = 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 = new JWebBrowser();
		webBrowser.navigate(Config.local + Config.index);
		webBrowser.setBarsVisible(false);
		webBrowser.setMenuBarVisible(false);
		webBrowser.setLocationBarVisible(false);
		webBrowser.setButtonBarVisible(false);
		webBrowser.setStatusBarVisible(false);

		ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "\\城市.gif");
		File img = new File(System.getProperty("user.dir") + "\\" + Config.img);
		if (img.exists()) {
			icon = new ImageIcon(img.getAbsolutePath());
		}
		lbl = new JLabel(icon);

		panel = new JPanel(new BorderLayout());
	}

	private void initGUI() {
		panel.add(webBrowser, BorderLayout.CENTER);
		this.setContentPane(panel);
	}

	private void addListance() {

		webBrowser.addWebBrowserListener(new WebBrowserListener() {

			@Override
			public void windowWillOpen(WebBrowserWindowWillOpenEvent arg0) {
				lastTime = new Date().getTime();
				L.info("___________ windowWillOpen");
			}

			@Override
			public void windowOpening(WebBrowserWindowOpeningEvent arg0) {
				lastTime = new Date().getTime();
				L.info("___________ windowOpening");
			}

			@Override
			public void windowClosing(WebBrowserEvent arg0) {
				lastTime = new Date().getTime();
				L.info("___________ windowClosing");
			}

			@Override
			public void titleChanged(WebBrowserEvent arg0) {
				lastTime = new Date().getTime();
				L.info("___________ titleChanged");
			}

			@Override
			public void statusChanged(WebBrowserEvent arg0) {
				lastTime = new Date().getTime();
				L.info("___________ statusChanged");
			}

			@Override
			public void locationChanging(WebBrowserNavigationEvent arg0) {
				lastTime = new Date().getTime();
				L.info("___________ locationChanging");
			}

			@Override
			public void locationChanged(WebBrowserNavigationEvent arg0) {

				lastTime = new Date().getTime();
				L.info("___________ locationChanged");

				// 判断当前页面等级
				String locationURL = arg0.getNewResourceLocation();
				int index = 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("____________________ file Index:" + index);

				if (index != -1) {

					String fs = locationURL.substring(index + 9);
					L.info("______________________ fileName = " + fs);
					String[] fns = fs.split(",");
					try {
						List<File> flist = downNetFile(Arrays.asList(fns));
						doPrintDoc(flist);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						webBrowser.setVisible(false);
						webBrowser.setVisible(true);
						webBrowser.updateUI();
						webBrowser.repaint();
						panel.updateUI();
						panel.repaint();
						panel.validate();
					}
				});

			}

			@Override
			public void locationChangeCanceled(WebBrowserNavigationEvent arg0) {
				lastTime = new Date().getTime();
				L.info("___________ locationChangeCanceled");
			}

			@Override
			public void loadingProgressChanged(WebBrowserEvent arg0) {
				lastTime = new Date().getTime();
				L.info("___________ loadingProgressChanged");
			}

			@Override
			public void commandReceived(WebBrowserCommandEvent arg0) {
				lastTime = new Date().getTime();
				L.info("___________ commandReceived");
			}
		});

		lbl.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseClicked(MouseEvent arg0) {
				change(2);
			}
		});

	}

	/**
	 * 屏保切换
	 */
	public void change(final int type) {
		lastTime = new Date().getTime();

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				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();
			}
		});
	}

	/**
	 * 下载网络文件
	 *
	 * @param nameList
	 * @throws IOException
	 */
	private List<File> downNetFile(List<String> nameList) throws IOException {

		L.info("________________________ downNetFile1");

		if (null == nameList || nameList.isEmpty()) {
			return new ArrayList<File>();
		}

		List<File> flist = new ArrayList<File>();

		for (String name : nameList) {

			L.info("________________________ downNetFile2");

			URL url = new URL(Config.local + "/excel/" + name);
			HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();

			// 设置请求信息
			httpConnection.setRequestProperty("GET", "/down.zip HTTP/1.1");

			// 设置接受信息
			httpConnection.setRequestProperty("Accept", "image/gif,image/x-xbitmap,application/msword");

			// 设置连接信息
			httpConnection.setRequestProperty("Connection", "Keep-Alive");

			// 获得输入流
			InputStream input = httpConnection.getInputStream();

			// 创建随机文件
			flist.add(new File(name));
			RandomAccessFile oSavedFile = new RandomAccessFile(name, "rw");

			byte[] b = new byte[1024];
			int nRead;

			// 从输入流中读入字节流,然后写到文件中
			while ((nRead = input.read(b, 0, 1024)) > 0) {
				oSavedFile.write(b, 0, nRead);
			}

			input.close();
			oSavedFile.close();
			httpConnection.disconnect();
		}

		return flist;

	}

	private void doPrintDoc(List<File> fileList) {

		if (null == fileList || fileList.isEmpty()) {
			return;
		}

		// 初始化组件
		ComThread.InitSTA();

		L.info("___________________ start Print 1");

		for (File file : fileList) {

			if (file == null || !file.exists()) {
				return;
			}

			L.info("___________________ start Print 2 :" + file.getAbsolutePath());

			int index = file.getName().indexOf(".");
			String extName = file.getName().toUpperCase().substring(index + 1);

			String comApp = "Word.Application";
			String property = "Documents";

			if (extName.equals("DOC") || extName.equals("DOCX") || extName.equals("WPS")) {
				comApp = "Word.Application";
				property = "Documents";
			} else if (extName.equals("XLS") || extName.equals("XLSX") || extName.equals("ET")) {
				comApp = "Excel.Application";
				property = "Workbooks";
			} else if (extName.equals("PPT") || extName.equals("PPTX") || extName.equals("DPS")) {
				comApp = "PowerPoint.Application";
				property = "Presentations";
			}

			L.info("___________________ start Print 3 :" + comApp);
			ActiveXComponent axc = new ActiveXComponent(comApp);
			try {
				if (!property.equals("Presentations")) {
					Dispatch.put(axc, "Visible", new Variant(false));
				}
				Dispatch document = axc.getProperty(property).toDispatch();

				Dispatch doc = null;

				if (property.equals("Presentations")) {
					doc = Dispatch.call(document, "Open", file.getAbsolutePath(), true, true, false).toDispatch();
				} else {
					doc = Dispatch.invoke(document, "Open", Dispatch.Method, new Object[] { file.getAbsolutePath() }, new int[1]).toDispatch();
				}

				L.info("___________________ start Print 3 :" + file.getAbsolutePath());

				Dispatch.call(doc, "PrintOut");
				Dispatch.call(doc, "Close");
				if (!property.equals("Presentations")) {
					axc.invoke("Quit", new Variant[] {});
				}

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				comApp = "";
				property = "";

			}
		}

		ComThread.Release();
	}

	public static void main(String[] args) {

		L.APPNAME = "Street_cs";
		L.LOGF = true;
		L.initLog();

		UIUtils.setPreferredLookAndFeel();
		NativeInterface.open();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainFrame frame = new MainFrame();
				frame.setVisible(true);
				new ChangeWait(frame).tryStart();
				new ServeSocket();
			}
		});
		NativeInterface.runEventPump();
	}

}

时间: 2024-10-05 11:32:30

Java Swing嵌入浏览器的相关文章

JAVA SWING嵌入IE浏览器控件JExplorer详细介绍及下载

JExplorer用于在Swing程序中添加Microsoft Internet Explorer的控件.您可以结合JNIWrapper中的共同特点来使用该类库,尤其是当您需要使用低级Internet Explorer的API函数来深度整合MSHTML和DOM功能时. 具体功能: JExplorer具有多种功能特点,方便您在Java程序中整合Internet Explorer Visual Browser组件用于嵌入一个Swing用户界面中,并自动执行HeadlessBrowser进行测试 自动

swing和java里嵌入浏览器

Swing 基于chromiun的swing组件 http://www.teamdev.com/jxbrowser Swing使用JavaFX的方式 http://www.tuicool.com/articles/N7J3au http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm DJ NativeSwing 非常强大,可以使用IE. Firefox, XULRunner or Webkit - http://gongqi.itey

java swing开发最简单的浏览器源代码下载

原文:java swing开发最简单的浏览器源代码下载 源代码下载地址:http://www.zuidaima.com/share/1550463483628544.htm java swing开发最简单的浏览器,已测试.... package com.zuidaima; import org.eclipse.swt.SWT; import org.eclipse.swt.browser.Browser; import org.eclipse.swt.browser.CloseWindowLis

atitit.D&amp;D drag&amp;drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结

atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结 1. DND的操作流程 1 2. Html5 注解事件 document.dragover >>preventDefault 1 3. 代码(js) 1 4. C++ 实现拖曳 2 5. QT拖拽功能简介 - pcsuite的专栏 - 博客频道 - CSDN.NET.htm 2 1. DND的操作流程 Dragenter 事件::更改提示的颜色

Java Swing的进化

摘 要:Swing已是一个比较老的工具集了,在美观的用户界面出来之前需要开发很长时间.它缺少一些你在开发富UI时所需的组件.幸运地是,像 Substance,SwingX及Java Look-and_Feel图形仓库这样的开源项目使这一切变得不同.作者Steven Haines向你展示了如何无痛苦地向你的Swing UI中添加树表,语法高亮,以及其它更多的东西. 在最近这些年里,用户界面设计与开发已经发生了很大的改变,一些人可能会说Java平台已经停滞不前了.发布于1997年的Swing仍然是在

Java Swing 第一记 Hello Word

首先来一个Java Swing的HelloWord程序. 1 package cn.java.swing.chapter03; 2 3 import javax.swing.JButton; 4 import javax.swing.JFrame; 5 import javax.swing.JPanel; 6 7 public class Demo01 { 8 9 public static void main(String[] args) { 10 JFrame jFrame = new JF

java Swing组件和事件处理(二)

1.BoxLayout类可以创建一个布局对象,成为盒式布局,BoxLayout在javax.Swing  border 包中,java.swing 包提供一个Box类,该类也是一个类,创建的容器称作一个盒式布局,不   允许盒式容器的布局.在策划程序布局的时候,可以利用容器的嵌套,将某个容器嵌入几个盒式容器,达到布局的目的. 使用盒式布局的容器组件将排列一行或一列,这取决于创建盒式布局对象时,是否确定行的排列和列的排列, package com.Example2; import javax.sw

Atitit。D&amp;D drag&amp;drop拖拽功能c#.net java swing的对比与实现总结

Atitit.D&D drag&drop拖拽功能c#.net java swing的对比与实现总结 1. 实现一个D&D操作一般包括三个步骤: 1 2. .net黑头的拖曳机制.必须有DragEnter事件(单独写DragDrop事件是不会具有拖拽功能的) 2 3. ---java黑头的拖曳..必须有DragEnter事件(单独写 Drop事件是不会具有拖拽功能的) 2 4. 代码 3 5. 参考 5 1. 实现一个D&D操作一般包括三个步骤: 首先实现一个拖拽源,这个拖拽

【小型系统】抽奖系统-使用Java Swing完成

一.需求分析 1. 显示候选人照片和姓名. 2. 可以使用多种模式进行抽奖,包括一人单独抽奖.两人同时抽奖.三人同时抽奖. 3. 一个人可以在不同的批次的抽奖中获取一.二.三等奖,但是不能在同一批次抽奖中获取多个奖项. 二.系统总体框架 系统需求比较简单,采用Java Swing技术可以很好的实现,系统的总体类框架图如下 说明:基于主要的三个类就可以完成所需要的功能,Frame有Panel类型与MouseAdapter类型的成员,Panel主要用户存放具体的控件和绘制相应的信息,MouseAda