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

代码如下:
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下一页阅读全文

(0)

相关推荐

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

    代码如下: 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.e

  • Python简易计算器制作方法代码详解

    主要用到的工具是Python中的Tkinter库 比较简单 直接上图形界面和代码 引用Tkinter库 from tkinter import * 建立主窗口对象 window=Tk() #设置窗口对象 window.title('counting machine') window.geometry("350x280") window['bg']='red' 建立标签框以及标签(将运算字符串显示在上面) frame=LabelFrame(window,bg='yellow',width

  • SWT(JFace) 图片浏览器 实现代码

    代码如下: 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...体验代码

    演示代码: 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

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

    代码如下: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

  • JS制作简易计算器的实例代码

    做一个简易计算器,效果图片 c表示清空,为一个空字符串 +/-表示该值为正还是负 %表示当前值/100 ←表示退格,往前删除一个值 eval 函数是能够计算出字符串表达式或者语句的结果,把结果求出来. 代码如下 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"

  • SWT(JFace)体验之GridLayout布局

    GridLayout布局 GridLayout 布局的功能非常强大,也是笔者常用的一种布局方式.GridLayout是网格式布局,它把父组件分成一个表格,默认情况下每个子组件占据一个单元格的空间,每个子组件按添加到父组件的顺序排列在表格中.GridLayout提供了很多的属性,可以灵活设置网格的信息.另外,GridLayout 布局提供了GridData类,子组件可以设置相应的GridData,例如 "dogPhoto.setLayoutData(gridData)",GridData

  • 最新最全PHP生成制作验证码代码详解(推荐)

    1.0 首先先看代码 <?php header("Content-Type:text/html;Charset=UTF-");// 设置页面的编码风格 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像 $img = imagecreatetruecolor(,);//创建画布并设置大小 x轴 y轴 $bgcolor = imagecolorallocate($img, mt_rand(,), mt_

  • JavaScript简易计算器制作

    本文实例为大家分享了JavaScript实现简易计算器的具体代码,供大家参考,具体内容如下 成品图 这个简易的计算器只能实现 + - * /的运算 在动手之前,我们要先理清思路,看图,若想实现这样一个简易的计算器,我们需要两个输入框用来存放我们需要进行运算的两个值,一个下拉框来选择要进行什么运算,一个按钮来实现开始运算,一个输入框来保存我们计算后的值! 首先,我们在body中制作页面需要的这些元素 <body> <input type="text" id="

  • Python使用Pyqt5实现简易浏览器(最新版本测试过)

    准备环境 首先我们需要的是我们的开发环境,我使用的是python 3.8.2和pyqt 5.14.2,因为有强迫症,所以喜欢使用最新版的 安装QtWebEngineWidgets 这是新版使用的web浏览器引擎,更加的贴近谷歌浏览器,好像是需要单独安装,我就是这样的 pip3 install QtWebEngineWidgets 多tab页面 做这个的时候遇到好多坑,比如在多个tab里面打开页面,要使用这个QTabWidget,这个不用我们再去下载了,已经集成了 页面链接点击无反应 这个搞了我半

随机推荐