随笔 - 303  文章 - 883  trackbacks - 0
<2007年7月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

欢迎光临! 
闲聊 QQ:1074961813

随笔分类(357)

我管理的群

公共blog

  • n维空间
  • Email : java3d@126.com 群 : 12999758

参与管理的论坛

好友的blog

我的其他blog

朋友的网站

搜索

  •  

最新评论

啊大家好啊

我是寻觅 最近在弄GUI 开始使用工具开发了
eclipse确实很不错,前面的几个文章已经向大家介绍了如何在eclipse下使用designer工具开发swt java应用程序
最近我也写了些啊 效果确实不错 下面是一个浏览器的代码 运行起来使用后感觉非常的快
有兴趣的朋友可以在上面做二次开发哦…………
源代码前面文章里有这里我再次给出,直接运行是不行的,需要相应的配置文件个图片

http://download.eclipse.org/eclipse/downloads/drops/R-3.2.2-200702121330/download.php?dropFile=eclipse-examples-3.2.2-win32.zip

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * 
http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************
*/

package org.eclipse.swt.examples.browserexample;

import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import java.io.*;
import java.util.*;

public class BrowserExample {
    
static ResourceBundle resourceBundle = ResourceBundle.getBundle("examples_browser");
    
int index;
    
boolean busy;
    Image images[];
    Image icon 
= null;
    
boolean title = false;
    Composite parent;
    Text locationBar;
    Browser browser;
    ToolBar toolbar;
    Canvas canvas;
    ToolItem itemBack, itemForward;
    Label status;
    ProgressBar progressBar;
    SWTError error 
= null;

    
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, boolean top) {
        
this.parent = parent;
        
try {
            browser 
= new Browser(parent, SWT.BORDER);
        }
 catch (SWTError e) {
            error 
= e;
            
/* Browser widget could not be instantiated */
            parent.setLayout(
new FillLayout());
            Label label 
= new Label(parent, SWT.CENTER | SWT.WRAP);
            label.setText(getResourceString(
"BrowserNotCreated"));
            parent.layout(
true);
            
return;
        }

        initResources();
        
final Display display = parent.getDisplay();
        browser.setData(
"org.eclipse.swt.examples.browserexample.BrowserApplication"this);
        browser.addOpenWindowListener(
new OpenWindowListener() {
            
public void open(WindowEvent event) {
                Shell shell 
= new Shell(display);
                
if (icon != null) shell.setImage(icon);
                shell.setLayout(
new FillLayout());
                BrowserExample app 
= new BrowserExample(shell, false);
                app.setShellDecoration(icon, 
true);
                event.browser 
= app.getBrowser();
            }

        }
);
        
if (top) {
            browser.setUrl(getResourceString(
"Startup"));
            show(
falsenullnulltruetruetruetrue);
        }
 else {
            browser.addVisibilityWindowListener(
new VisibilityWindowListener() {
                
public void hide(WindowEvent e) {
                }

                
public void show(WindowEvent e) {
                    Browser browser 
= (Browser)e.widget;
                    BrowserExample app 
= (BrowserExample)browser.getData("org.eclipse.swt.examples.browserexample.BrowserApplication");
                    app.show(
true, e.location, e.size, e.addressBar, e.menuBar, e.statusBar, e.toolBar);
                }

            }
);
            browser.addCloseWindowListener(
new CloseWindowListener() {
                
public void close(WindowEvent event) {
                    Browser browser 
= (Browser)event.widget;
                    Shell shell 
= browser.getShell();
                    shell.close();
                }

            }
);
        }

    }


    
/**
     * Disposes of all resources associated with a particular
     * instance of the BrowserApplication.
     
*/
    
    
public void dispose() {
        freeResources();
    }

    
    
/**
     * Gets a string from the resource bundle.
     * We don't want to crash because of a missing String.
     * Returns the key if not found.
     
*/

    
static String getResourceString(String key) {
        
try {
            
return resourceBundle.getString(key);
        }
 catch (MissingResourceException e) {
            
return key;
        }
 catch (NullPointerException e) {
            
return "!" + key + "!";
        }
            
    }

    
    
public SWTError getError() return error; }
    
    
public Browser getBrowser() return browser; }
    
    
public void setShellDecoration(Image icon, boolean title) {
        
this.icon = icon;
        
this.title = title;
    }

    
    
void show(boolean owned, Point location, Point size, boolean addressBar, boolean menuBar, boolean statusBar, boolean toolBar) {
        
final Shell shell = browser.getShell();
        
if (owned) {
            
if (location != null) shell.setLocation(location);
            
if (size != null) shell.setSize(shell.computeSize(size.x, size.y));
        }

        FormData data 
= null;
        
if (toolBar) {
            toolbar 
= new ToolBar(parent, SWT.NONE);
            data 
= new FormData();
            data.top 
= new FormAttachment(05);
            toolbar.setLayoutData(data);
            itemBack 
= new ToolItem(toolbar, SWT.PUSH);
            itemBack.setText(getResourceString(
"Back"));
            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"));
            
            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(locationBar.getText());
                }

            }
;
            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);

            canvas 
= new Canvas(parent, SWT.NO_BACKGROUND);
            data 
= new FormData();
            data.width 
= 24;
            data.height 
= 24;
            data.top 
= new FormAttachment(05);
            data.right 
= new FormAttachment(100-5);
            canvas.setLayoutData(data);
            
            
final Rectangle rect = images[0].getBounds();
            canvas.addListener(SWT.Paint, 
new Listener() {
                
public void handleEvent(Event e) {
                    Point pt 
= ((Canvas)e.widget).getSize();
                    e.gc.drawImage(images[index], 
00, rect.width, rect.height, 00, pt.x, pt.y);            
                }

            }
);
            canvas.addListener(SWT.MouseDown, 
new Listener() {
                
public void handleEvent(Event e) {
                    browser.setUrl(getResourceString(
"Startup"));
                }

            }
);
            
            
final Display display = parent.getDisplay();
            display.asyncExec(
new Runnable() {
                
public void run() {
                    
if (canvas.isDisposed()) return;
                    
if (busy) {
                        index
++;
                        
if (index == images.length) index = 0;
                        canvas.redraw();
                    }

                    display.timerExec(
150this);
                }

            }
);
        }

        
if (addressBar) {
            locationBar 
= new Text(parent, SWT.BORDER);
            data 
= new FormData();
            
if (toolbar != null{
                data.top 
= new FormAttachment(toolbar, 0, SWT.TOP);
                data.left 
= new FormAttachment(toolbar, 5, SWT.RIGHT);
                data.right 
= new FormAttachment(canvas, -5, SWT.DEFAULT);            
            }
 else {
                data.top 
= new FormAttachment(00);
                data.left 
= new FormAttachment(00);
                data.right 
= new FormAttachment(1000);            
            }

            locationBar.setLayoutData(data);
            locationBar.addListener(SWT.DefaultSelection, 
new Listener() {
                
public void handleEvent(Event e) {
                    browser.setUrl(locationBar.getText());
                }

            }
);
        }

        
if (statusBar) {
            status 
= new Label(parent, SWT.NONE);
            progressBar 
= new ProgressBar(parent, SWT.NONE);
            
            data 
= new FormData();
            data.left 
= new FormAttachment(05);
            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);
            
            browser.addStatusTextListener(
new StatusTextListener() {
                
public void changed(StatusTextEvent event) {
                    status.setText(event.text);    
                }

            }
);
        }

        parent.setLayout(
new FormLayout());

        Control aboveBrowser 
= toolBar ? (Control)canvas : (addressBar ? (Control)locationBar : null);
        data 
= new FormData();
        data.left 
= new FormAttachment(00);
        data.top 
= aboveBrowser != null ? new FormAttachment(aboveBrowser, 5, SWT.DEFAULT) : new FormAttachment(00);
        data.right 
= new FormAttachment(1000);
        data.bottom 
= status != null ? new FormAttachment(status, -5, SWT.DEFAULT) : new FormAttachment(1000);
        browser.setLayoutData(data);
            
        
if (statusBar || toolBar) {
            browser.addProgressListener(
new ProgressListener() {
                
public void changed(ProgressEvent event) {
                    
if (event.total == 0return;                            
                    
int ratio = event.current * 100 / event.total;
                    
if (progressBar != null) progressBar.setSelection(ratio);
                    busy 
= event.current != event.total;
                    
if (!busy) {
                        index 
= 0;
                        
if (canvas != null) canvas.redraw();
                    }

                }

                
public void completed(ProgressEvent event) {
                    
if (progressBar != null) progressBar.setSelection(0);
                    busy 
= false;
                    index 
= 0;
                    
if (canvas != null{
                        itemBack.setEnabled(browser.isBackEnabled());
                        itemForward.setEnabled(browser.isForwardEnabled());
                        canvas.redraw();
                    }

                }

            }
);
        }

        
if (addressBar || statusBar || toolBar) {
            browser.addLocationListener(
new LocationListener() {
                
public void changed(LocationEvent event) {
                    busy 
= true;
                    
if (event.top && locationBar != null) locationBar.setText(event.location);
                }

                
public void changing(LocationEvent event) {
                }

            }
);
        }

        
if (title) {
            browser.addTitleListener(
new TitleListener() {
                
public void changed(TitleEvent event) {
                    shell.setText(event.title
+" - "+getResourceString("window.title"));
                }

            }
);
        }

        parent.layout(
true);
        
if (owned) shell.open();
    }


    
/**
     * Grabs input focus
     
*/

    
public void focus() {
        
if (locationBar != null) locationBar.setFocus();
        
else if (browser != null) browser.setFocus();
        
else parent.setFocus();
    }

    
    
/**
     * Frees the resources
     
*/

    
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;
        }

    }

    
    
/**
     * Loads the resources
     
*/

    
void initResources() {
        
final Class clazz = this.getClass();
        
if (resourceBundle != null{
            
try {
                
if (images == null{
                    images 
= new Image[imageLocations.length];
                    
for (int i = 0; i < imageLocations.length; ++i) {
                        InputStream sourceStream 
= clazz.getResourceAsStream(imageLocations[i]);
                        ImageData source 
= new ImageData(sourceStream);
                        ImageData mask 
= source.getTransparencyMask();
                        images[i] 
= new Image(null, source, mask);
                        
try {
                            sourceStream.close();
                        }
 catch (IOException e) {
                            e.printStackTrace ();
                        }

                    }

                }

                
return;
            }
 catch (Throwable t) {
            }

        }

        String error 
= (resourceBundle != null? getResourceString("error.CouldNotLoadResources") : "Unable to load resources";
        freeResources();
        
throw new RuntimeException(error);
    }

    
    
public static void main(String [] args) {
        Display display 
= new Display();
        Shell shell 
= new Shell(display);
        shell.setLayout(
new FillLayout());
        shell.setText(getResourceString(
"window.title"));
        InputStream stream 
= BrowserExample.class.getResourceAsStream(iconLocation);
        Image icon 
= new Image(display, stream);
        shell.setImage(icon);
        
try {
            stream.close();
        }
 catch (IOException e) {
            e.printStackTrace();
        }

        BrowserExample app 
= new BrowserExample(shell, true);
        app.setShellDecoration(icon, 
true);
        shell.open();
        
while (!shell.isDisposed()) {
            
if (!display.readAndDispatch())
                display.sleep();
        }

        icon.dispose();
        app.dispose();
        display.dispose();
    }

}



地震让大伙知道:居安思危,才是生存之道。
posted on 2007-07-09 21:52 小寻 阅读(4702) 评论(6)  编辑  收藏 所属分类: j2se/j2ee/j2me

FeedBack:
# re: 纯Java写的简单浏览器代码 速度胜过IE 2007-09-26 19:06 aaa
你煞笔吧!SWT浏览器用的就是IE的内核,还说速度胜过IE?!  回复  更多评论
  
# re: 纯Java写的简单浏览器代码 速度胜过IE[未登录] 2007-09-26 21:35 寻觅
呵呵,用过便知,有人就像有人说TT比IE好  回复  更多评论
  
# re: 纯Java写的简单浏览器代码 速度胜过IE[未登录] 2010-07-13 14:36 aa
wu yu  回复  更多评论
  
# re: 纯Java写的简单浏览器代码 速度胜过IE 2010-12-05 20:48 voyager
错的一塌糊涂  回复  更多评论
  
# re: 纯Java写的简单浏览器代码 速度胜过IE[未登录] 2010-12-07 13:06 小寻
注意运行环境,不能少  回复  更多评论
  
# re: 纯Java写的简单浏览器代码 速度胜过IE 2012-10-09 11:26 HXY
哈哈,好好看看SWT的源码,Eclipse都附带的呢。SWT的Browser都是直接调用系统的浏览器,在不同的平台调用不同的浏览器,SWT的Browser只是提供了些Java版的借口而已。在Windows上运行,其实就是IE。你真外行啊,哈哈哈哈哈哈。你唯一可以提速的只有你的界面代码,而不是浏览器本身。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: