Java 学习

坚持不懈,打通-->软件—控制—机械
posts - 5, comments - 3, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

2006年7月4日

上一篇详细介绍了一个SWT实例,这一篇接下来介绍SWT组件的生命周期
本文地址:http://www.blogjava.net/cpenet/archive/2006/07/04/56539.html

本篇译自《 Eclipse.Building.Commercial.Quality.Plug.ins.2nd.Edition 》第四章第二节,希望对入门者有所帮助。

 

组件的生命周期

设计 SWT 的一个目标就是小而灵巧。为了达到这个目标,一个基本的设计策略,就是尽可能的使组件的状态存储在平台组件中而不是存储在 SWT 组件中 ( 这句话,我不理解,也翻译的不明白,附原文: To achieve this, a basic design decision was made that as much widget state as possible would be stored in the platform widget rather than in the SWT widget.) 。这与 Swing 形成显著的对比, Swing Swing 组件中维护了所有的组件状态。 ( 可能是这样: SWT 把各个组件的状态交给系统平台来管理,而 Swing 自己管理各个 Swing 组件的状态,这样 Swing 就复杂,耗费的资源,内存的需求也多,不知道是不是 J ) 。通过不把系统平台级别的组件信息复制过来自己维护, SWT 就非常的小巧而且对内存的需求也相应少。

达到这个目的所付出的代价就是 SWT 的组件不能够自己独立存在。当一个 SWT 组件被创建时,同时立即在系统平台下也创建了该组件的对应体。接下来,几乎所有对该组件的信息状态请求都会由平台来处理。

大部分的系统平台在创建一个组件时,需要该组件明确的父组件的上下文,所以 SWT 需要一个父组件来最为它的构造声明。许多平台在创建组件时还需要该组件的特定风格参数的设定。例如:按钮有复选框,单选框,简单按钮和文本域有单行和多行之分。

SWT 类中,风格位段用整数类型来定义且不会更改。风格参数被组织在一起,其它各种构造器传递着这些参数来创建一个组件的初始风格。注意到,所有的平台并不支持所有的风格,所以在很多情况下,被请求的风格被认为是一种提示,它或许会或许不会对一个特殊的平台产生影响。

SWT 组件不在需要时,一些平台要求作出明确的释放。对于组件本身和一些资源 ( 例如:图形,字体,颜色 ) 而言,他们已经具备了系统的这个要求。基本的规则是:如果你创建了一个组件,就要使用 dispose() 方法来撤销这个组件。如果你使用了一些系统资源,例如系统颜色,你就不需要释放他们。

幸运的,当一个组件被释放的,它的所有子组件会自动地被释放。这意味着,如果你释放了一个 shell ,那么所有 shell 包含的组件都会被自动释放。

posted @ 2006-07-04 14:52 燕然 阅读(965) | 评论 (1)编辑 收藏

2006年7月3日

本文译自《 Eclipse.Building.Commercial.Quality.Plug.ins.2nd.Edition 》第四章第二节,详细解释了 SWT 程序的语句,希望对入门者有所帮助。

  本文地址:http://www.blogjava.net/cpenet/archive/2006/07/03/56407.html

让我们从简单的 HelloWorld 应用程序开始。

 一、SWT添加到工程的类路径中

首先建立一个java工程。在开始使用SWT之前,需要将SWT库文件添加到工程的类路径中。步骤如下:

1.    下载SWT。在Eclipse SDK的下载页面中,提供了独立版本的SWT下载。找到标有SWT Binary and Source的栏目。下载适合你操作系统的版本,不用解压,直接保存到硬盘中。

2.    Eclipse菜单栏,选择File-Import...打开导入向导

3.    选择Existing Projects into Workspace,点击Next按钮

4.    选择 Select archive file并使用 Browse...按钮来找到你刚才下载的SWT文件。

 5.    点击Finish按钮,完成SWT导入。

 6.    右键点击你建立的工程,选择Properties 来打开Properties对话框。

 7.    选择 Java Build Path -> Projects tab并点击Add按钮.

 8.    选择 org.eclipse.swt工程, 点击 OK,完成添加SWT库到你的工程中(见图1).

{26F7CFAE-1A69-42BA-A826-4576DB987FD5}.BMP 

                              1

二、 SWT 代码

现在在你建立的java工程中,新建一个java文件,取名为 HelloWorld。在HelloWorld.java文件中把main()方法覆盖为以下代码 

1     public   static   void  main(String[] args) {
2       Display display  =   new
 Display();
3       Shell shell  =   new
 Shell(display);
4       shell.setText( " Hello World "
);
5       shell.setBounds( 100 100 200 50
);
6       shell.setLayout( new
 FillLayout());
7       Label label  =   new
 Label(shell, SWT.CENTER);
8       label.setText( " Hello World "
);
9       Color red  =   new  Color(display,  255 0 0
);
10
     label.setForeground(red);
11
     shell.open();
12       while  ( !
shell.isDisposed()) {
13          if  ( !
display.readAndDispatch()) display.sleep();
14
     }
15
     red.dispose();
16
     display.dispose();
17
  }

注:

在覆盖了上述代码后, 选择菜单栏中的 Source -> Organize Imports命令 (或者按Ctrl+Shift+O) 来把需要引用的SWT包导入到HelloWorld.java文件中

以下是对各行代码的详细解释:

2行:每个基于SWT的应用程序都有一个Display类的实例。用来将低层平台和SWT进行链接。除了管理SWT的事件循环,还能访问SWT需要的平台资源。在16行,display实例将会提交给垃圾收集器。

3行:每一个窗口都有一个Shell窗口框架,来与用户进行交互。Shell像所有的windows系统一样来处理动作行为,并作为窗口控件的放置场所。

4行: setText()方法设置窗口的标题.

5行: setBounds() 方法设置窗口的大小和放置的位置. 在这个例子中, 设置窗口为200个像素宽,50个像素高,并放在离屏幕的左上角100x100像素的位置上。

6行: setLayout()方法 设置窗口框架的布局. FillLayout,充满式布局管理器,使得组件大小会尽量的充满整个容器.SWT的布局管理器会在以后详细介绍。

7行:在shell上新建一个简单的label组件并居中显示label的文本内容。

8行: setText() 方法 设置label的文本内容。

9行:创建一个红色的颜色类的实例。你也可以使用以下语句获得系统红色的实例:

Color red = display.getSystemColor(SWT.COLOR_RED);

10行: setForeground() 方法  设置label的前景色

11行:到目前为止,窗口框架还是不可见的。通过open()方法使得窗口可见。

12行: while语句循环检测窗口有没有关闭。

13行:display 控制事件的循环. readAndDispatch() 方法从平台的事件队列中读取事件,并分配他们到合适的处理程序(接收者)。只要队列中一直有事件可以处理,这个方法一直返回true,当事件队列为空时,则返回false(因此允许用户界面UI线程出于sleep状态直到事件队列不为空)

15,16行:当循环检测到窗口被关闭时,需要将colordisplay和一些相关联的平台资源释放。注意到系统颜色实例(colors)将会被提交释放。

二、            运行这个例子

通常情况下,为了启动一个java应用程序,会使用Run As - Java Application 命令。在这里,如此运行将会抛出 UnsatisfiedLinkError 异常,说明没有找到 SWT 的本地库。如果运行 Run As > SWT Application 命令,将会弹出 SWT 启动配置窗口,见图 2 ,在图 2 中可以点击 run 按钮。

 {3717602A-4D02-4E2D-A913-A6BC4E1289A8}.BMP

                                2

点击 run ,运行结果图 3

 {978DFCF8-658E-40EC-80D4-3DD5D8FACF1F}.BMP

          3

(以后继续)

posted @ 2006-07-03 20:09 燕然 阅读(774) | 评论 (0)编辑 收藏

2006年6月30日

org.eclipse.ui包是应用程序界面的包, Eclipse的平台的界面也是使用了这个包.
包中有接口IWorkbenchWindow,API中的说明

A workbench window is a top level window in a workbench. Visually, a workbench window has a menubar, a toolbar, a status bar, and a main area for displaying a single page consisting of a collection of views and editors.

Each workbench window has a collection of 0 or more pages; the active page is the one that is being presented to the end user; at most one page is active in a window at a time.

This interface is not intended to be implemented by clients.

一个工作台窗口是一个工作台的顶层窗口.从视觉上看,一个工作台窗口拥有一个菜单栏,工具栏,状态栏和一个主区域,用来显示包括显示和编辑的一个页面.

每个工作台窗口有0个或者更多的页面.活动的页面是一种显示给当前使用者的页面.大多数情况下,在一个时刻只有一个页面处于活动状态.

这个接口不打算由用户来实现.

(to more)

posted @ 2006-06-30 20:10 燕然 阅读(3569) | 评论 (0)编辑 收藏

Rich Client Tutorial是Eclipse官方网站的RCP开发指南。原文地址为: http://www.eclipse.org/articles/Article-RCP-1/tutorial1.html
第一次翻译,有错误,欢迎指正。

Rich Client Tutorial Part 1

The Rich Client Platform (RCP) is an exciting new way to build Java applications that can compete with native applications on any platform. This tutorial is designed to get you started building RCP applications quickly. It has been updated for Eclipse 3.1.2

富客户端平台( RCP )是一个令人兴奋的创建 java 应用程序的新方式,她可以与任何平台下的本地应用程序相媲美。这份指南目的在于让你快速的构建 rcp 应用程序。本指南已更新,适用于 Eclipse 3.1.2

Introduction

Try this experiment: Show Eclipse to some friends or co-workers who haven't seen it before and ask them to guess what language it is written in. Chances are, they'll guess VB, C++, or C#, because those languages are used most often for high quality client side applications. Then watch the look on their faces when you tell them it was created in Java, especially if they are Java programmers.

Because of its unique open source license, you can use the technologies that went into Eclipse to create your own commercial quality programs. Before version 3.0, this was possible but difficult, especially when you wanted to heavily customize the menus, layouts, and other user interface elements. That was because the "IDE-ness" of Eclipse was hard-wired into it. Version 3.0 introduced the Rich Client Platform (RCP), which is basically a refactoring of the fundamental parts of Eclipse's UI, allowing it to be used for non-IDE applications. Version 3.1 updated RCP with new capabilities, and, most importantly, new tooling support to make it easier to create than before.

If you want to cut to the chase and look at the code for this part you can find it in the accompanying zip file . Otherwise, let's take a look at how to construct an RCP application.

引言

尝试做这个实验:把 Eclipse 展示给你的那些以前没见过 Eclipse 的朋友或合作伙伴,让他们猜猜这是用何种语言编写的。很有可能,他们会说是 VB C++ 或者 C# ,因为这些语言经常被用来作为高质量客户端方面的应用程序。当你告诉他们是用 java 编写时,察看他们脸上的表情,尤其当他们是 java 程序员的话。

因为 Eclipse 是唯一 开放源代码许可的,你可以用 Eclipse 这个技术来创建你自己商业质量的程序。在 3.0 版本以前,这是可能的,但也是困难的,尤其当你想要订制菜单,多层和其他用户界面元素时。那是因为很难接触到 Eclipse IDE 核心。 3.0 版本的 Eclipse 引入了富客户端编程( RCP ),这基本上是 Eclipse UI 的重新构建,也就允许 Eclipse UI 被应用到非 Eclipse IDE 的应用程序中。 3.1 版本的 Eclipse 用新特性升级了 RCP ,最重要的是,新的工具使的创建 RCP 应用程序比以前更加容易。

如果你想要研究 Rich Client Tutorial Part 1 的代码,你可以到 accompanying zip file 下载。另外,让我们一步步来看看如何创建一个 RCP 应用程序。

Getting started

开始

RCP applications are based on the familiar Eclipse plug-in architecture, (if it's not familiar to you, see the references section). Therefore, you'll need to create a plug-in to be your main program. Eclipse's Plug-in Development Environment (PDE) provides a number of wizards and editors that take some of the drudgery out of the process. PDE is included with the Eclipse SDK download so that is the package you should be using. Here are the steps you should follow to get started.

RCP 应用程序基于熟悉的 Eclipse 插件机制,(如果你不熟悉,请查看参考书目部分)。因此,你要创建一个插件来作为你的主程序。 Eclipse 的插件开发环境( PDE )提供了大量的控件和编辑器来使得开发过程脱离苦海。 PDE 包括在了 Eclipse SDK ,所以 PDE 开发包你可以直接使用。以下是开始开发的步骤:

First, bring up Eclipse and select File > New > Project, then expand Plug-in Development and double-click Plug-in Project to bring up the Plug-in Project wizard. On the subsequent pages, enter a Project name such as org.eclipse.ui.tutorials.rcp.part1, indicate you want a Java project, select the version of Eclipse you're targeting (at least 3.1), and enable the option to Create an OSGi bundle manifest. Then click Next >.

首先,启动 Eclipse ,选择File > New > Project接下来选中 Plug-in Development双击来打开插件工程向导。在弹出的页面中,键入工程名(例如: org.eclipse.ui.tutorials.rcp.part1),指明你要一个 java工程,选择你想创建适用的 Eclise的版本,并选中使用 OSGI包。接下来点击 Next>

note:Beginning in Eclipse 3.1 you will get best results by using the OSGi bundle manifest. In contrast to previous versions, this is now the default.

In the next page of the Wizard you can change the Plug-in ID and other parameters. Of particular importance is the question, "Would you like to create a rich client application?". Select Yes. The generated plug-in class is optional but for this example just leave all the other options at their default values. Click Next > to continue.

注: 通过使用 Eclipse 3.1 OSGI 包,你会获得很好的结果。与以前的版本相比, OSGi 现在是默认的了。

在下一页向导中,你可以更改插件的 ID 号和其他参数。尤其要关注一个问题:“你想要创建一个富客户端的应用程序吗?”,选择“是”。生成的插件类是可选的,但对于这个例子,一切其他的可选项都设为默认值。单击Next >到下一页

note: If you get a dialog asking if Eclipse can switch to the Plug-in Development Perspective click Remember my decision and select Yes (this is optional).

注:如果你得到一个对话框,询问 Eclipse 是否切换到插件开发透视图。这时单击记住我的选择并选择“是”(这是可选的)。

Starting with Eclipse 3.1, several templates have been provided to make creating an RCP application a breeze. We'll use the simplest one available and see how it works. Make sure the option to Create a plug-in using one of the templates is enabled, then select the Hello RCP template. This is RCP's equivalent of "Hello, world". Click Finish to accept all the defaults and generate the project (see Figure 1). Eclipse will open the Plug-in Manifest Editor. The Plug-in Manifest editor puts a friendly face on the various configuration files that control your RCP application.

Eclipse 3.1 开始,提供了几个模板使得创建 RCP 应用程序相当容易。我们使用最简单的一个模板来观察这是如何工作的。确保创建一个插件使用模板之一是允许的,接着选择“ Hello RCP ”模板。这是 RCP 应用程序中的“ Hello, world ”。选择“完成”来创建工程(见图一)。 Eclipse 将会打开插件编辑器。插件编辑器提供了友好的界面,拥有多样的配置文件来控制你的RCP应用程序。



Figure 1. The Hello World RCP project was created by a PDE wizard.

图一,PDE向导创建的Hello World工程

(未完,待续)

posted @ 2006-06-30 18:27 燕然 阅读(972) | 评论 (1)编辑 收藏

这个RCP的开发怎么感觉转来转去,转晕了。。唉,对eclipse的开发api又很不熟悉,感觉无从下手。

posted @ 2006-06-30 18:16 燕然 阅读(949) | 评论 (1)编辑 收藏