`
tryonmind
  • 浏览: 115818 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

用eclipse的浏览器包开发自己的浏览器

    博客分类:
  • java
阅读更多

前段时间一个客户说能不能在程序(web应用)运行的时候不要用普通的浏览去访问,比如直接双击桌面上的图标就运行程序,也不用输访问地址,也没有浏览器的菜单,导航按钮等,也就是给web应用装个壳。。。  不好做啊 以前只是做个屏蔽,浏览器只显示标题栏,不过IE7、8在弹出的窗口中还是会有地址栏,去不掉。另外多窗口的浏览器使得判断窗口关闭更麻烦,甚至不可能。 Jin天在java2s上转了转发现有用eclipse的jar包开发浏览器的列子,下下来做了个测试,还行,跑起来了,这个程序只是在弹出新窗口的时候没做好,会用默认的浏览器打开。 不过我程序那程序中正好没有弹出窗口,还能用。哈哈。 贴出来大家一起来研究。

 

以下是那个程序, 记得在引eclipse包的时候直接引导eclipse的plugin下,复制出来引可能会要问题。

 

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * This class implements a web browser
 */
public class AdvancedBrowser {
	// The "at rest" text of the throbber
	private static final String AT_REST = "Ready";

	/**
	 * Runs the application
	 * 
	 * @param location the initial location to display
	 */
	public void run(String location) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Advanced Browser");
		createContents(shell, location);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

	/**
	 * Creates the main window's contents
	 * 
	 * @param shell the main window
	 * @param location the initial location
	 */
	public void createContents(Shell shell, String location) {
		shell.setLayout(new FormLayout());

		// Create the composite to hold the buttons and text field
		Composite controls = new Composite(shell, SWT.NONE);
		FormData data = new FormData();
		data.top = new FormAttachment(0, 0);
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(100, 0);
		controls.setLayoutData(data);

		// Create the status bar
		Label status = new Label(shell, SWT.NONE);
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(100, 0);
		data.bottom = new FormAttachment(100, 0);
		status.setLayoutData(data);

		// Create the web browser
		final Browser browser = new Browser(shell, SWT.BORDER);
		data = new FormData();
		data.top = new FormAttachment(controls);
		data.bottom = new FormAttachment(status);
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(100, 0);
		browser.setLayoutData(data);

		// Create the controls and wire them to the browser
		controls.setLayout(new GridLayout(7, false));

		// Create the back button
		Button button = new Button(controls, SWT.PUSH);
		button.setText("Back");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				browser.back();
			}
		});

		// Create the forward button
		button = new Button(controls, SWT.PUSH);
		button.setText("Forward");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				browser.forward();
			}
		});

		// Create the refresh button
		button = new Button(controls, SWT.PUSH);
		button.setText("Refresh");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				browser.refresh();
			}
		});

		// Create the stop button
		button = new Button(controls, SWT.PUSH);
		button.setText("Stop");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				browser.stop();
			}
		});

		// Create the address entry field and set focus to it
		final Text url = new Text(controls, SWT.BORDER);
		url.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		url.setFocus();

		// Create the go button
		button = new Button(controls, SWT.PUSH);
		button.setText("Go");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				browser.setUrl(url.getText());
			}
		});

		// Create the animated "throbber"
		Label throbber = new Label(controls, SWT.NONE);
		throbber.setText(AT_REST);

		// Allow users to hit enter to go to the typed URL
		shell.setDefaultButton(button);

		// Add event handlers
		browser.addCloseWindowListener(new AdvancedCloseWindowListener());
		browser.addLocationListener(new AdvancedLocationListener(url));
		browser.addProgressListener(new AdvancedProgressListener(throbber));
		browser.addStatusTextListener(new AdvancedStatusTextListener(status));

		// Go to the initial URL
		if (location != null) {
			browser.setUrl(location);
		}
	}

	/**
	 * This class implements a CloseWindowListener for AdvancedBrowser
	 */
	class AdvancedCloseWindowListener implements CloseWindowListener {
		/**
		 * Called when the parent window should be closed
		 */
		public void close(WindowEvent event) {
			// Close the parent window
			((Browser) event.widget).getShell().close();
		}
	}

	/**
	 * This class implements a LocationListener for AdvancedBrowser
	 */
	class AdvancedLocationListener implements LocationListener {
		// The address text box to update
		private Text location;

		/**
		 * Constructs an AdvancedLocationListener
		 * 
		 * @param text the address text box to update
		 */
		public AdvancedLocationListener(Text text) {
			// Store the address box for updates
			location = text;
		}

		/**
		 * Called before the location changes
		 * 
		 * @param event the event
		 */
		public void changing(LocationEvent event) {
			// Show the location that's loading
			location.setText("Loading " + event.location + "...");
		}

		/**
		 * Called after the location changes
		 * 
		 * @param event the event
		 */
		public void changed(LocationEvent event) {
			// Show the loaded location
			location.setText(event.location);
		}
	}

	/**
	 * This class implements a ProgressListener for AdvancedBrowser
	 */
	class AdvancedProgressListener implements ProgressListener {
		// The label on which to report progress
		private Label progress;

		/**
		 * Constructs an AdvancedProgressListener
		 * 
		 * @param label the label on which to report progress
		 */
		public AdvancedProgressListener(Label label) {
			// Store the label on which to report updates
			progress = label;
		}

		/**
		 * Called when progress is made
		 * 
		 * @param event the event
		 */
		public void changed(ProgressEvent event) {
			// Avoid divide-by-zero
			if (event.total != 0) {
				// Calculate a percentage and display it
				int percent = (int) (event.current / event.total);
				progress.setText(percent + "%");
			} else {
				// Since we can't calculate a percent, show confusion :-)
				progress.setText("???");
			}
		}

		/**
		 * Called when load is complete
		 * 
		 * @param event the event
		 */
		public void completed(ProgressEvent event) {
			// Reset to the "at rest" message
			progress.setText(AT_REST);
		}
	}

	/**
	 * This class implements a StatusTextListener for AdvancedBrowser
	 */
	class AdvancedStatusTextListener implements StatusTextListener {
		// The label on which to report status
		private Label status;

		/**
		 * Constructs an AdvancedStatusTextListener
		 * 
		 * @param label the label on which to report status
		 */
		public AdvancedStatusTextListener(Label label) {
			// Store the label on which to report status
			status = label;
		}

		/**
		 * Called when the status changes
		 * 
		 * @param event the event
		 */
		public void changed(StatusTextEvent event) {
			// Report the status
			status.setText(event.text);
		}
	}

	/**
	 * The application entry point
	 * 
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		new AdvancedBrowser().run(args.length == 0 ? null : args[0]);
	}
}

 

程序运行效果:

4
0
分享到:
评论
2 楼 guzhan 2008-07-29  
通过 browser.addOpenWindowListener
倒是可以控制住
引用
弹出新窗口的时候没做好,会用默认的浏览器打开。


但不知道browser怎么和已有的URLConnection的cookie交互,哎
1 楼 minidayly 2008-07-18  
收藏,lz可否告诉一下开发浏览器的是什么插件

相关推荐

    开源的Web应用浏览器,基于“eclipse平台”自带精简1.5版jre,jre只有3m

    HYIE是在“eclipse平台”上开发的java浏览器,本身自带了一个精简的1.5版jre,HYIE不需要预 安装jre,精简后的jre只有3m,HYIE+jre一共才7m, 压缩后的安装包只有4m,非常适合在web应用中应用。 并且是开源的。 ...

    在线集成开发环境EclipseChe.zip

    Eclipse Che 是一个高性能的基于浏览器的集成开发环境,通过提供结构化的工作区、项目输入、模块化扩展插件来支持 Codenvy 的引擎。Eclipse Che 采用 Java 开发,支持 Windows、Linux 和 OS X 系统。提供扩展功能...

    eclipse全程指南 源代码 课后光盘

    方法有两种:在Eclipse 的包浏览器(package Explorer)中单击右键,选择导入(import);在弹出的对话框中选择“现有的项目”(Existing Project),然后定位到想要的项目目录。导入项目后可能会报错,那是因为项目...

    android网页浏览器demo源代码

    适合新手自己开发浏览器的参考demo eclipse编译通过 完全自主开发

    eclipse全程指南-王占全

    方法有两种:在Eclipse 的包浏览器(package Explorer)中单击右键,选择导入(import);在弹出的对话框中选择“现有的项目”(Existing Project),然后定位到想要的项目目录。导入项目后可能会报错,那是因为项目...

    EclipsePHP

    Eclipse底层开发而来,并且集成了JDK,免除处了安装配置的麻烦,一次安装即可使用 无需配置。此编译器为PHP编译器,辅助PHP代码的开发和调试,集成了代码高亮,函数 跟踪,时时纠错等功能。同时还增加了协作开发版本...

    JAVA浏览器

    用java编写的简易浏览器,可实现浏览器部分基本功能。开发环境:eclipse

    EclipsePHP Studio

    EclipsePHP Studio 1.2.2 (以下简称:EPP)是一个大型PHP项目开发编译器,给予Eclipse底层开发而来,并且集成了JDK,免除处了安装配置的麻烦,一次安装即可使用无需配置。此编译器为PHP编译器,辅助PHP代码的开发和...

    Docker 实现浏览器里开发Android应用的功能

    在浏览器里开发Android应用  这里需要用到Docker的知识, Che 发布后对Android应用开发多了一个工具,这里就对如何实现该功能就行详细介绍:  Eclipse Che 最近Che发布了正式版,那我就介绍下在Che上开发Android...

    NAOSI-DLUT#DLUT_SE_Courses#JavaEE 第五节课:Eclipse快速开发JSP1

    JavaEE 第五节课:Eclipse快速开发JSP1. 使用Eclipse开发web项目(jsp)在eclipse中创建的Web项目:浏览器可以直接访问Web

    eclipse项目启动快捷插件

    在开发中遇见的问题是,在开发一个项目的时候总需要在浏览器和eclipse直接切换,使用eclipse内置的还不方便,所以此插件就是在eclipse中直接调用浏览器来打开项目的插件,可以很方便快捷的打开项目,只需要把当前...

    EclipsePHP Studio 1.2.2 ( EPP) 简体中文版.rar

    Eclipse底层开发而来,并且集成了JDK,免除处了安装配置的麻烦,一次安装即可使用 无需配置。此编译器为PHP编译器,辅助PHP代码的开发和调试,集成了代码高亮,函数 跟踪,时时纠错等功能。同时还增加了协作开发...

    eclipse 打开选中文件或文件夹位置

    eclipse 有一个插件是open explorer,我最近下载下来安装不能用了,查了一下原理,自己开发了一个小插件 该插件主要是可以用windows的explorer 打开eclipse中被选择的文件或文件夹的位置,不用再复制路径信息然后...

    Eclipse FTP插件 - Sexftp 支持FTP上传、下载、比较等功能

    Sexftp简介Sexftp是FTP上传与下载的eclipse插件,集成在ECLIPSE中,方便开发人员进行FTP相关操作,省去来回切换工具及选择目录的麻烦。 •Sexftp支持上传、下载等基本操作;同时可以直接eclipse中查看或编辑服务器...

    web开发平台(基于JEE的web快速开发平台)

    应用开发:提供可视化的WebBuilder集成开发环境,帮助应用系统的快速开发,支持使用Eclipse等开发工具的开发和调试,可以在您原有使用的技术框架上混合使用。 应用部署:使用基于Web的资源管理器进行应用的部署,...

    使用EclipseRCP进行桌面程序开发(四):在Windows中使用ActiveX控件

    OLE的体验,就是平时我们可以把Excel表格嵌入Word文档,或者把PDF嵌入浏览器显示一样,而ActiveX控件更是无处不在,做VB开发和网页开发的人都应该很熟悉。使用Windows系统中丰富的ActiveX控件资源,我们可以实现功能...

    WebBuilder 2010 Web应用开发和部署平台

    应用开发:提供可视化的WebBuilder集成开发环境,帮助应用系统的快速开发,支持使用Eclipse等开发工具的开发和调试。 应用部署:使用基于Web的资源管理器进行应用的部署,支持Java,.Net,PHP等大部分Web应用的部署...

    微信ssm+微信小程序的点餐系统

    开发软件:eclipse/myeclipse/idea Maven包:Maven3.3.9 浏览器:谷歌浏览器 安卓框架:uniapp 安卓开发软件:HBuilder X 开发模式:混合开发 开发语言:Java 框架:ssm JDK版本:JDK1.8 服务器:tomcat7 数据库:...

    基于ssm+微信小程序的食堂窗口自助点餐系统

    开发软件:eclipse/myeclipse/idea Maven包:Maven3.3.9 浏览器:谷歌浏览器 安卓框架:uniapp 安卓开发软件:HBuilder X 开发模式:混合开发 开发语言:Java 框架:ssm JDK版本:JDK1.8 服务器:tomcat7 数据库:...

    基于ssm+微信小程序的考研知识题库小程序

    开发软件:eclipse/myeclipse/idea Maven包:Maven3.3.9 浏览器:谷歌浏览器 安卓框架:uniapp 安卓开发软件:HBuilder X 开发模式:混合开发 开发语言:Java 框架:ssm JDK版本:JDK1.8 服务器:tomcat7 数据库:...

Global site tag (gtag.js) - Google Analytics