SWT(JFace) 简易浏览器 制作实现代码第1/2页_Java编程

代码如下:
BrowserExample.java

复制代码 代码如下:

package swt_jface.demo5;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.CloseWindowListener;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.browser.OpenWindowListener;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.browser.StatusTextEvent;
import org.eclipse.swt.browser.StatusTextListener;
import org.eclipse.swt.browser.TitleEvent;
import org.eclipse.swt.browser.TitleListener;
import org.eclipse.swt.browser.VisibilityWindowListener;
import org.eclipse.swt.browser.WindowEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class BrowserExample {
//static ResourceBundle resourceBundle = ResourceBundle.getBundle("examples_browser");
int index;
boolean busy;
Image images[];
Text location;
Browser browser;
static final String[] imageLocations = {
"eclipse01.bmp", "eclipse02.bmp", "eclipse03.bmp", "eclipse04.bmp", "eclipse05.bmp",
"eclipse06.bmp", "eclipse07.bmp", "eclipse08.bmp", "eclipse09.bmp", "eclipse10.bmp",
"eclipse11.bmp", "eclipse12.bmp",};
static final String iconLocation = "document.gif";

public BrowserExample(Composite parent) {
final Display display = parent.getDisplay();
FormLayout layout = new FormLayout();
parent.setLayout(layout);
ToolBar toolbar = new ToolBar(parent, SWT.NONE);
final ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
itemBack.setText(getResourceString("Back"));
final ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
itemForward.setText(getResourceString("Forward"));
final ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
itemStop.setText(getResourceString("Stop"));
final ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
itemRefresh.setText(getResourceString("Refresh"));
final ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
itemGo.setText(getResourceString("Go"));
location = new Text(parent, SWT.BORDER);
images = new Image[]{new Image(display, "C:/icons/web/go.gif")};
final Canvas canvas = new Canvas(parent, SWT.NO_BACKGROUND);
final Rectangle rect = images[0].getBounds();
canvas.addListener(SWT.Paint, new Listener() {
public void handleEvent(Event e) {
Point pt = canvas.getSize();
e.gc.drawImage(images[index], 0, 0, rect.width, rect.height, 0, 0, pt.x, pt.y);
}
});
canvas.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event e) {
browser.setUrl(getResourceString("Startup"));
}
});

display.asyncExec(new Runnable() {
public void run() {
if (canvas.isDisposed()) return;
if (busy) {
index++;
if (index == images.length) index = 0;
canvas.redraw();
}
display.timerExec(150, this);
}
});
final Label status = new Label(parent, SWT.NONE);
final ProgressBar progressBar = new ProgressBar(parent, SWT.NONE);
FormData data = new FormData();
data.top = new FormAttachment(0, 5);
toolbar.setLayoutData(data);
data = new FormData();
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
data.top = new FormAttachment(canvas, 5, SWT.DEFAULT);
data.bottom = new FormAttachment(status, -5, SWT.DEFAULT);
try {
browser = new Browser(parent, SWT.NONE);
browser.setLayoutData(data);
} catch (SWTError e) {
Label label = new Label(parent, SWT.CENTER | SWT.WRAP);
label.setText(getResourceString("BrowserNotCreated"));
label.setLayoutData(data);
}
data = new FormData();
data.width = 24;
data.height = 24;
data.top = new FormAttachment(0, 5);
data.right = new FormAttachment(100, -5);
canvas.setLayoutData(data);
data = new FormData();
data.top = new FormAttachment(toolbar, 0, SWT.TOP);
data.left = new FormAttachment(toolbar, 5, SWT.RIGHT);
data.right = new FormAttachment(canvas, -5, SWT.DEFAULT);
location.setLayoutData(data);
data = new FormData();
data.left = new FormAttachment(0, 5);
data.right = new FormAttachment(progressBar, 0, SWT.DEFAULT);
data.bottom = new FormAttachment(100, -5);
status.setLayoutData(data);

data = new FormData();
data.right = new FormAttachment(100, -5);
data.bottom = new FormAttachment(100, -5);
progressBar.setLayoutData(data);
if (browser != null) {
itemBack.setEnabled(browser.isBackEnabled());
itemForward.setEnabled(browser.isForwardEnabled());

Listener listener = new Listener() {
public void handleEvent(Event event) {
ToolItem item = (ToolItem)event.widget;
if (item == itemBack) browser.back();
else if (item == itemForward) browser.forward();
else if (item == itemStop) browser.stop();
else if (item == itemRefresh) browser.refresh();
else if (item == itemGo) browser.setUrl(location.getText());
}
};
browser.addLocationListener(new LocationListener() {
public void changed(LocationEvent event) {
busy = true;
if (event.top) location.setText(event.location);
}
public void changing(LocationEvent event) {
}
});
browser.addProgressListener(new ProgressListener() {
public void changed(ProgressEvent event) {
if (event.total == 0) return;
int ratio = event.current * 100 / event.total;
progressBar.setSelection(ratio);
busy = event.current != event.total;
if (!busy) {
index = 0;
canvas.redraw();
}
}
public void completed(ProgressEvent event) {
itemBack.setEnabled(browser.isBackEnabled());
itemForward.setEnabled(browser.isForwardEnabled());
progressBar.setSelection(0);
busy = false;
index = 0;
canvas.redraw();
}
});
browser.addStatusTextListener(new StatusTextListener() {
public void changed(StatusTextEvent event) {
status.setText(event.text);
}
});
if (parent instanceof Shell) {
final Shell shell = (Shell)parent;
browser.addTitleListener(new TitleListener() {
public void changed(TitleEvent event) {
shell.setText(event.title+" - "+getResourceString("window.title"));
}
});
}
itemBack.addListener(SWT.Selection, listener);
itemForward.addListener(SWT.Selection, listener);
itemStop.addListener(SWT.Selection, listener);
itemRefresh.addListener(SWT.Selection, listener);
itemGo.addListener(SWT.Selection, listener);
location.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event e) {
browser.setUrl(location.getText());
}
});
initialize(display, browser);
browser.setUrl(getResourceString("Startup"));
}
}
static String getResourceString(String key) {
try {
return "key";
} catch (MissingResourceException e) {
return key;
} catch (NullPointerException e) {
return "!" + key + "!";
}
}
static String getResourceString(String key, Object[] args) {
try {
return MessageFormat.format(getResourceString(key), args);
} catch (MissingResourceException e) {
return key;
} catch (NullPointerException e) {
return "!" + key + "!";
}
}
static void initialize(final Display display, Browser browser) {
browser.addOpenWindowListener(new OpenWindowListener() {
public void open(WindowEvent event) {
System.out.println("Open");
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
initialize(display, browser);
event.browser = browser;
}
});
browser.addVisibilityWindowListener(new VisibilityWindowListener() {
public void hide(WindowEvent event) {
}
public void show(WindowEvent event) {
System.out.println("Show");
Browser browser = (Browser)event.widget;
Shell shell = browser.getShell();
if (event.location != null) shell.setLocation(event.location);
if (event.size != null) {
Point size = event.size;
shell.setSize(shell.computeSize(size.x, size.y));
}
shell.open();
}
});
browser.addCloseWindowListener(new CloseWindowListener() {
public void close(WindowEvent event) {
System.out.println("Close");
Browser browser = (Browser)event.widget;
Shell shell = browser.getShell();
shell.close();
}
});
}
public void dispose() {
freeResources();
}
void freeResources() {
if (images != null) {
for (int i = 0; i < images.length; ++i) {
final Image image = images[i];
if (image != null) image.dispose();
}
images = null;
}
}
public void setFocus() {
location.setFocus();
}
public static void main(String [] args) {

Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("Browser example");
BrowserExample instance = new BrowserExample(shell);
Image icon = new Image(display, "C:/icons/web/go.gif");
shell.setImage(icon);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
icon.dispose();
instance.dispose();
display.dispose();
}
}

当前1/2页 12下一页阅读全文

时间: 2024-09-20 19:54:20

SWT(JFace) 简易浏览器 制作实现代码第1/2页_Java编程的相关文章

SWT(JFace)体验之List演示汇总第1/2页_Java编程

代码如下:DropDownAndSimple.java 复制代码 代码如下: package swt_jface.demo3; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; p

SWT(JFace) 图片浏览器 实现代码_Java编程

代码如下: ImageViewer.java 复制代码 代码如下: package swt_jface.demo11; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image;

SWT(JFace) Menu、Bar...体验代码_Java编程

演示代码: MenuExamples.java 复制代码 代码如下: package swt_jface.demo5; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; impo

MYSQL批量插入数据的实现代码第1/3页_java

@echo off cls set CLASSPATH=..\api\jogre.jar set CLASSPATH=%CLASSPATH%;. set CLASSPATH=%CLASSPATH%;classes set CLASSPATH=%CLASSPATH%;lib\dom4j.jar java org.jogre.server.JogreServer 建表 复制代码 代码如下: create database con_test; use con_test; create table te

Jsp页面实现文件上传下载类代码第1/2页_JSP编程

刚才和lp看完电影,把jsp页面抽出class调整了一下.最近总上经典,是感觉既然当了斑竹,就该留下点什么.lp这几天也半开玩笑半生气的说,一回来就上经典,就发帖,你干脆娶经典作lp得了.想想,这几天是有点夸张,以后放慢速度了.保持1星期1帖吧,那样也能多想写,多总结些.发帖的初衷就是有时候看到有的朋友问的问题,似乎还没有走进java的门,希望这样的帖子,能对新手一点帮助,也就满足了.有时候随意的一段话,其实也是自己的一点经验,而有时候之所以絮絮叨叨,是想把问题说的清楚明白,让高手见笑了.因为在

jsp hibernate的分页代码第1/3页_JSP编程

可见使用Hibernate,在进行查询分页的操作上,是具有非常大的灵活性,Hibernate会首先尝试用特定数据库的分页sql,如果没用,再尝试Scrollable,如果不行,最后采用rset.next()移动的办法. (一)pager类 * @(#)Pager.java 2005-5-3 * * Copyright (c) 2005, Jeffrey Hsu */ package com.jeffrey.messagelove; /** * Pager holds the page info.

易语言基础教程之制作简易浏览器_易语言

方法/步骤 首先打开易语言: 点击--工具--支持库配置--全选--确认 之后点击属性--拉宽 之后点击标题输入测试浏览器 拉一个编辑框和按钮最后一个超文本浏览框(自己找) 之后双击按钮输入代码: .版本 2 .支持库 HtmlView .子程序 _按钮1_被单击 超文本浏览框1.地址 = 编辑框1.内容 按F5测试,好的行了 注意事项 编辑框要输入网址,不能输入其他的OK. 以上是小编为您精心准备的的内容,在的博客.问答.公众号.人物.课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜

代码-C#做简易浏览器的一些疑惑

问题描述 C#做简易浏览器的一些疑惑 我下载了两个代码原件(是不是这么表述?),结果不会看,虽然将我所招收道德是代码运行了,但只得到空白窗口.是我找错了地方,还是代码有问题.另外,有没有C#做浏览器讲的详细点的信息. 解决方案 先从简单的开始.http://www.cnblogs.com/finallyliuyu/archive/2009/06/16/csharpwebbrower.html 解决方案二: 新建一个WinForms窗体,拖一个TextBox,一个Button,一个WebBrows

eclipse3.6新建项目中没有swt/jface随便问一下用swt/jface怎么做界面开发

问题描述 如题,怎么使eclipse3.6集成到swt/jface中,那里下载,怎么swt/jface做办面开发 解决方案 解决方案二:eclipse下关于RCP版得,然后http://apps.hi.baidu.com/share/detail/24455394,根据这个网址,基本上差不多解决方案三:引用楼主zh346400987的回复: 如题,怎么使eclipse3.6集成到swt/jface中,那里下载,怎么swt/jface做办面开发 这你算问对了哈哈这个版块局势围绕swtjfacerc