使用Eclipse的VE(Visual Editor)
早就聽說Eclipse的鼎鼎大名,瀏覽了好多關於Eclipse的文章,聽說用SWT,JFace能設計出擁有本地作業系統介面風格的東東,如果在XP上運行,豈不是很讓客戶滿意,近期要做個案頭系統,^_^....., 想Trying 下.
用Eclipse當然就要裝上再說,這兩天我是吃勁了苦頭..., 下載了很多東西,讀了很多文章,終於調通了,現在給大家分享下.我沒有找到完整的安裝攻略,現在就寫一個最新的..... 建議您先瀏覽下Eclipse網站 http://www.eclipse.org ;尤其是 Projects 串連, 其中的Eclipse Projiect, 和 Eclipse Tools Project.
Eclipse (Eclipse SDK 最新的是3.0.2) 是一個平台,其中的JDT,PDE都是整合在Eclipse下載的,SWT,JFace開發包也是,不用關心太多,有些文章說的很多,反而讓新手迷惑...
Eclipse Tool Project 主要用到. Graphical Editor Framework (GEF) Project, EMF, VE
不要急於下載這些東東,我算是吃勁苦頭......
用Eclipse當然希望用它的可視化編程(Visual Edit Project) 外掛程式. 現在最新的是 VE1.0.2.1下載頁 http://www.eclipse.org/vep/
現看它的下載需求!!!!! 原來VE只能和相應版本的GEF,EMF,Eclipse SDK,一起工作,如果先前你下載了其他的高版本,都糟踐的,關鍵是耽誤自己的時間.
好了,攻略開始.建個下載檔案夾.
下載VE(1.0.2.1)
在同一個頁面的需求裡下載 Eclipse SDK 3.0.1, EMF 2.0.1, GEF3.0.1
解壓縮Eclipse SDK 3.0.1, 把解壓縮裡的 eclipse 檔案夾拷貝到D:/
看看裡邊的內容,主要是plugins,features
解壓縮 EMF 到/emf 檔案夾, 把emf/plugins 的東西都拷貝到d:/eclipse/plugins, emf/features 下的東西拷貝到d:/eclipse/features
VE,GEF 操作同上...
重要的,Eclipse 3.0.1需要 JDK1.4.2, 所以要先安裝它.JDK的路徑設定就不再說了...../bin, /lib , /jdk1.4.2
由於運行SWT的程式用到本地庫,因此拷貝 D:/eclipse/plugins/org.eclipse.swt.win32_3.0.1/os/win32/x86 下的 *.dll 拷貝到 jdk路徑下的 /bin 目錄, 運行程式可以自動找到. 這些問題都困擾了我好長時間.
現在啟動 d:/eclipse/eclipse.exe
^_^^_^,都OK了
eclipse 能自動認出你的JRE.
實戰!
啟動eclipse,看看welcome project 吧,不錯... 介面很迷人!
建立一個項目 file->New->project 出來項目嚮導, 選擇java project. ->next 輸入project Name:Hello Eclipse自動給你建個workspace 工作資料夾,你的項目都存在這裡的,d:/eclipse/workspace 點擊結束. 一個新項目建成了.
添加SWT ToolKit, 菜單Project->Properties 選擇左側的 Java Build Path 選擇 右側的 Libraries 標籤頁, 點擊 Add Library 按鈕, 選擇 Standard Widget Toolkit(SWT) 點擊 Next , 選上 Include support for JFace library 結束.這樣就可以使用 SWT Jface控制項了.
選中你的project Hello. 選擇建立表徵圖按鈕, 上端最左側的. 選擇 Visual Class ,點擊Next , Source Folder 是預設的 project folder, package 填入 test, Name 填入 FrmHello , 展開左側的 SWT 選擇 Shell, 勾選上 public static void main(String[] args), Constructors from superclass, Inherited abstract methods. 結束.
Eclipse自動啟動 VE.... 看看那些視窗... 比較熟悉.. 出現了可視化編輯視窗.
滑鼠移到右側的Palette(豎著的那個)把 一個Label,一個Button, 一個TextArea 放到shell上.
選中Label, 在下側的Properties屬性欄可以進行設定, >text 填入Hello World
選中 Button 在下側的Properties屬性欄可以進行設定, >text 填入Click Me.
選中 Button 右鍵, events->Add Events . 選擇Mouse - MouseAdapter ->mouseDown. 相應的代碼自動產生.添加如下代碼(藍色部分)
也可進行其他的風格設定, 但是如果JDK版本不對則不能操作.有錯...
全部代碼:
/*
* Created on 2005-4-20
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
/**
* @author w19284
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class FrmHello {
private org.eclipse.swt.widgets.Shell sShell = null; // @jve:decl-index=0:visual-constraint="41,12"
private Label label = null;
private Button button = null;
private Text textArea = null;
/**
*
*/
public FrmHello() {
super();
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
/* Before this is run, be sure to set up the following in the launch configuration
* (Arguments->VM Arguments) for the correct SWT library path.
* The following is a windows example:
* -Djava.library.path="installation_directory/plugins/org.eclipse.swt.win32_3.0.0/os/win32/x86"
*/
org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display.getDefault();
FrmHello thisClass = new FrmHello();
thisClass.createSShell() ;
thisClass.sShell.open();
while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep ();
}
display.dispose();
}
/**
* This method initializes sShell
*/
private void createSShell() {
sShell = new org.eclipse.swt.widgets.Shell();
label = new Label(sShell, SWT.NONE);
button = new Button(sShell, SWT.NONE);
textArea = new Text(sShell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
sShell.setText("Shell");
sShell.setBackground(org.eclipse.swt.widgets.Display.getDefault().getSystemColor(org.eclipse.swt.SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
label.setBounds(new org.eclipse.swt.graphics.Rectangle(167,5,96,24));
label.setText("Hello World");
label.setBackground(org.eclipse.swt.widgets.Display.getDefault().getSystemColor(org.eclipse.swt.SWT.COLOR_INFO_BACKGROUND));
label.setFont(new org.eclipse.swt.graphics.Font(org.eclipse.swt.widgets.Display.getDefault(), "Times New Roman", 12, org.eclipse.swt.SWT.BOLD));
button.setBounds(new org.eclipse.swt.graphics.Rectangle(152,34,123,24));
button.setText("Click Me");
button.setFont(new org.eclipse.swt.graphics.Font(org.eclipse.swt.widgets.Display.getDefault(), "Times New Roman", 14, org.eclipse.swt.SWT.NORMAL));
textArea.setBounds(new org.eclipse.swt.graphics.Rectangle(64,70,323,123));
sShell.setSize(new org.eclipse.swt.graphics.Point(451,232));
button.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
System.out.println("mouseDown()"); // TODO Auto-generated Event stub mouseDown()
textArea.setText(textArea.getText() + "|Hello,Eclipse");
}
});
}
}
好了,大功馬上告成. 選擇運行表徵圖, toolBar 上的綠色按鈕旁邊的黑下箭頭.選擇->run.., 選擇左側的java Application 點擊下面的建立. 選擇中間Main標籤頁, Project 選擇Hello , Main class: 選擇test.FrmHello. 點擊 run 按鈕.
介面出來了...., 以後你就可以直接點擊Run表徵圖,運行這個程式. 點擊Click Me 看看TextArea的變化...
注意:關閉當前表單用dispose()就可以了,如果要退出程式,就用System.Exit(0),上面的代碼是SWT的
以下是我自己的一些代碼:
public void keyPressed(java.awt.event.KeyEvent e) {
//KeyPress Events
FrmLogin frmlogin=new FrmLogin();
//以下設定強制回應視窗