文档章节

Eclipse SWT的简单使用记录

独特程序
 独特程序
发布于 2017/02/02 12:56
字数 933
阅读 45
收藏 0

SWT使用的版本为 org.eclipse.swt.win32.win32.x86_3.105.2.v20161122-0613.jar 是eclipse neon中的jar

火狐内核运行时 xulrunner-26.0b8.en-US.win32.zip

https://archive.mozilla.org/pub/xulrunner/releases/26.0b8/runtimes/xulrunner-26.0b8.en-US.win32.zip

git:https://git.oschina.net/iproject/ToyBrowser.git

定义了一个GeckoBrowser类继承Browser对一些操作实现了一些封装实现了浏览器的简单操作

package com.taomus.browser;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.browser.StatusTextEvent;
import org.eclipse.swt.browser.StatusTextListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class GeckoBrowser extends Browser{
	private static String basePath = System.getProperty("user.dir");
	private static String xulpath= basePath+"/xulrunner";
	private static String LASTLOC = "";
	private static Browser browser;
	private static String defualUrl = "about:blank";
	private static boolean jsenabled = true;
	static{
		ResourceBundle rb = ResourceBundle.getBundle("config");
		defualUrl = rb.getString("url");
		String xulpath1 = rb.getString("xulpath");
		if(xulpath1 != null && !xulpath1.equals("")){
			xulpath = xulpath1;
		}
		System.setProperty("org.eclipse.swt.browser.XULRunnerPath", xulpath);
	}
	
	private GeckoBrowser(Composite parent, int style) {
		super(parent, style);
	}
	
	public GeckoBrowser(Composite parent){
		super(parent, SWT.MOZILLA);
		Shell shell = (Shell)parent;
		shell.setImage(SWTResourceManager.getImage(shell.getClass(), "/logo.png"));
		browser = this;
		browser.setJavascriptEnabled(jsenabled);
		Menu menu = new Menu(parent);
		MenuItem mitem = new MenuItem(menu, SWT.PUSH);
		mitem.setText("刷新");
		mitem.addListener(SWT.Selection, event->{
			MenuItem mi = (MenuItem)event.widget;
			if(mi.getText().trim().equals("刷新")){
				this.refresh();
			}
		});
		this.setMenu(menu);
		init();
	}

	
	private void init(){
		this.setUrl(defualUrl);
		this.addCloseWindowListener(event -> {
			Browser browser1 = (Browser) event.widget;
			Shell shell = browser1.getShell();
			shell.close();
		});
		this.addTitleListener(event->{
			Browser browser1 = (Browser) event.widget;
			browser1.getShell().setText(event.title);
		});
		this.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent arg0) {}
			
			@Override
			public void mouseDown(MouseEvent arg0) {
				if(arg0.button==1&&(LASTLOC.startsWith("http://")||LASTLOC.startsWith("https://"))){
					browser.setUrl(LASTLOC);
				}
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent arg0) {}
		});
	}
	
	public void setLeftInfo(Label label){
		this.addStatusTextListener(new StatusTextListener() {
			@Override
			public void changed(StatusTextEvent arg0) {
				String info = arg0.text;
				label.setText(info);
				LASTLOC = info;
			}
		});
	}
	
	public void go(Button btn,Scrollable dest){
		btn.setToolTipText("转到");
		btn.setText("→");
		if(dest instanceof Text){
			Text text = (Text)dest;
			this.addLocationListener(new LocationListener() {
				
				@Override
				public void changing(LocationEvent arg0) {}
				
				@Override
				public void changed(LocationEvent arg0) {
					text.setText(arg0.location);
				}
			});
			btn.addListener(SWT.Selection,event->{
				this.setUrl(text.getText());
			});
		
		}
		
	}
	
	private void browserButton(Button btn) {
		btn.addListener(SWT.Selection,event->{
			Button btn1 = (Button)event.widget;
			switch(btn1.getData().toString()){
			case "back":
				this.back();
				break;
			case "forward":
				this.forward();
				break;
			case "refresh":
				this.refresh();
				break;
			case "stop":
				this.stop();
				break;
			}
		});
	}
	
	public void back(Button btn){
		btn.setData("back");
		btn.setText("◀");
		btn.setToolTipText("上一页");
		browserButton(btn);
	}

	public void forward(Button btn){
		btn.setData("forward");
		btn.setText("▶");
		btn.setToolTipText("下一页");
		browserButton(btn);
	}
	
	public void refresh(Button btn){
		btn.setData("refresh");
		btn.setText("刷新");
		btn.setToolTipText("刷新");
		browserButton(btn);
	}
	
	public void stop(Button btn){
		btn.setData("stop");
		btn.setText("×");
		btn.setToolTipText("停止");
		browserButton(btn);
	}
	
	@Override
    protected void checkSubclass() { }
}

     

package com.taomus.browser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.wb.swt.SWTResourceManager;

public class BrowserShell extends Shell {
	private Text location;
	private Map<String, Object> objMap = new HashMap<String, Object>();

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public BrowserShell(Display display) {
		super(display, SWT.SHELL_TRIM | SWT.BORDER);
		setLayout(new FormLayout());
		GeckoBrowser browser = new GeckoBrowser(this);
		FormData fd_browser = new FormData();
		fd_browser.bottom = new FormAttachment(100, -23);
		fd_browser.top = new FormAttachment(0, 33);
		fd_browser.right = new FormAttachment(100);
		fd_browser.left = new FormAttachment(0);
		browser.setLayoutData(fd_browser);

		Button backBtn = new Button(this, SWT.NONE);
		FormData fdBackBtn = new FormData();
		fdBackBtn.top = new FormAttachment(0);
		fdBackBtn.left = new FormAttachment(0);
		backBtn.setLayoutData(fdBackBtn);
		backBtn.setText("<");
		browser.back(backBtn);

		Button forwardBtn = new Button(this, SWT.NONE);
		FormData fdForwordBtn = new FormData();
		fdForwordBtn.top = new FormAttachment(backBtn, 0, SWT.TOP);
		fdForwordBtn.left = new FormAttachment(backBtn, 6);
		forwardBtn.setLayoutData(fdForwordBtn);
		forwardBtn.setText(">");
		browser.forward(forwardBtn);

		Button btnRefresh = new Button(this, SWT.NONE);
		FormData fdBtnRefresh = new FormData();
		fdBtnRefresh.left = new FormAttachment(backBtn, 33);
		fdBtnRefresh.top = new FormAttachment(backBtn, 0, SWT.TOP);
		btnRefresh.setLayoutData(fdBtnRefresh);
		browser.refresh(btnRefresh);

		location = new Text(this, SWT.BORDER);
		FormData fdLocation = new FormData();
		fdLocation.right = new FormAttachment(100, -50);
		fdLocation.top = new FormAttachment(0, 4);
		fdLocation.left = new FormAttachment(0, 157);
		location.setLayoutData(fdLocation);
		location.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.keyCode == SWT.CR) {
					browser.setUrl(location.getText());
				}
			}
		});

		Button btnGo = new Button(this, SWT.NONE);
		FormData fdBtnGo = new FormData();
		fdBtnGo.left = new FormAttachment(100, -45);
		fdBtnGo.top = new FormAttachment(0, 2);
		fdBtnGo.right = new FormAttachment(100, -10);
		btnGo.setLayoutData(fdBtnGo);
		btnGo.setText("go");
		browser.go(btnGo, location);

		Button btnStop = new Button(this, SWT.NONE);
		FormData fdBtnStop = new FormData();
		fdBtnStop.top = new FormAttachment(backBtn, 0, SWT.TOP);
		fdBtnStop.left = new FormAttachment(btnRefresh, 6);
		btnStop.setLayoutData(fdBtnStop);
		btnStop.setText("✘");
		browser.stop(btnStop);

		Label lblloc = new Label(this, SWT.NONE);
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.bottom = new FormAttachment(100);
		fd_lblNewLabel.right = new FormAttachment(100, -120);
		fd_lblNewLabel.left = new FormAttachment(0);
		lblloc.setLayoutData(fd_lblNewLabel);
		lblloc.setText("info");
	    browser.setLeftInfo(lblloc);

		Label lblhr = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		FormData fd_label_2 = new FormData();
		fd_label_2.left = new FormAttachment(0);
		fd_label_2.right = new FormAttachment(100);
		fd_label_2.bottom = new FormAttachment(100, -17);
		lblhr.setLayoutData(fd_label_2);
		this.thislistener(this);
		createContents();
	}

	private void thislistener(Shell shell) {
		this.addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent e) {
				MessageBox mb = new MessageBox(shell,SWT.ICON_QUESTION | SWT.OK| SWT.CANCEL);
				mb.setText("提示");
				mb.setMessage("确定要关闭吗?");
				int rc = mb.open();
				if (e.doit == (rc == SWT.OK)) {
					System.exit(0);
				} else if (e.doit == (rc == SWT.CANCEL)) {
					Display display = shell.getDisplay();
					shell.open();
					shell.layout();
					while (!shell.isDisposed()) {
						if (!display.readAndDispatch()) {
							display.sleep();
						}
					}
					shell.close();
				}
			}
		});
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("MyBrowser");
		setSize(800, 600);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}

      

package com.taomus.browser;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;

public class Main {
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			BrowserShell shell = new BrowserShell(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
			shell.close();
			//display.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

end

© 著作权归作者所有

上一篇: Xtend语法记录
独特程序

独特程序

粉丝 7
博文 5
码字总数 5549
作品 4
沈阳
高级程序员
私信 提问
加载中

评论(0)

GUI开发之AWT、SWING、SWT和JFACE的比较

核心提示:AWT Abstract Windows Toolkit(AWT)是最原始的 Java GUI 工具包。在任何一个 Java 运行环境中都可以使用它。 AWT 是一个非常简单的具有有限 GUI 组件、布局管理器和事件的工具包...

AlphaJay
2010/07/23
1.3K
0
Eclipse 3.3.2中配置Visual Editor

这两天在学习Eclipse的VE,中间也出现了很多问题,终于配置成功,记录一下配置过程,主要参考了“从头开始安装Eclipse和VE"的文章,网址是在http://www.cnblogs.com/pantian/archive/2006/1...

墙头草
2011/06/28
263
0
Java开发环境搭建全过程(上)

今天重装系统,对java开发环境重新进行了搭建,记录下自己搭建的过程留做以后参考。。。^o^ 第一部分 J2EE环境搭建 用的是j2sdk1.5+Tomcat5.5.16+Eclipse3.1.1+MyEclipse4.1.1 1. 安装jdk到...

嗯哼9925
2017/12/27
0
0
swing 事件处理机制

Swing的事件处理过程为:事件调度线程(Event Dispatch Thread)从事件队列(EventQueue)中获取底层系统捕获的原生事件,如鼠标、键盘、焦点、PAINT事件等。接着调用该事件源组件的dispachEven...

ensoncto
2014/09/19
0
0
eclipse git 报错

今天,想克隆一个git仓库。一点eclipse的git克隆,就报错了。Internal error; consult Eclipse error log.打开eclipse的error log视图,可以看到具体信息。 解决:"Preferences > General > ......

tyou
2016/08/20
69
0

没有更多内容

加载失败,请刷新页面

加载更多

springcloud微服务实战_09_消息驱动

9.1 spring cloud stream 简介 spring cloud stream 是一个用来为微服务应用提供消息驱动能力的框架. 它可以基于 springboot 来单独的创建独立的,可用于生产的 spring 的应用程序. 它通过使用...

SP_K
12分钟前
65
0
福昕PDF编辑器个人版不能登录多个电脑问题处理

问题 福昕PDF编辑器个人版,只能在两台电脑登录,而且不能同时登录。但是当买换了新电脑后,新电脑属于第三台电脑,就不能登录使用编辑功能了。 解决 打开“我的电脑”,搜索出如下文件,删除...

InfoInit
25分钟前
65
0
pandas操作excel-08-简单绘图-柱状图

import pandas as pdimport matplotlib.pyplot as pltstudents = pd.read_excel('D:/output.xlsx', index_col='idx')students.sort_values(by='SinglePrice', inplace=True, ascending=......

烽焱10仴
26分钟前
52
0
向点击事件监听器添加“返回假”有什么作用?

很多次,我在HTML页面中都看到过这样的链接: <a href='#' onclick='someFunc(3.1415926); return false;'>Click here !</a> 那里return false有什么作用? 另外,我通常不会在按钮中看到它......

技术盛宴
31分钟前
37
0
搞不定Kafka重复消费?来看看就不一样了

前言 今天我们聊一个话题,这个话题大家可能在面试过程中,或者是工作当中经常遇到 :point_right: 如何保证 Kafka 消息不重复消费? 我们在做开发的时候为了程序的健壮性,在使用 Kafka 的时...

Java进阶程序员xx
41分钟前
43
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部