有關在Eclipse上安裝外掛WindowBuilder以及如何使用
其實在Eclipse上安裝外掛很簡單,主要有兩種方式:第一種就是線上安裝外掛;第二種就是,採取離線方式,也就是先將外掛壓縮包下載下來。推薦使用第二種,因為到時候你可以自己隨時刪除這個外掛。
首先,此時此刻Eclipse是可以工作的狀態,也即是打開了Eclipse。Google一下關於Java GUI外掛,會發現比較多的外掛,但是在這裡推薦使用WindowBuilder外掛,我記得,曾經WindowBuilder還不是免費的,昨天才發現這麼好的東西竟然已經免費了,真是開發者的一大福利。Google搜尋一下WindowBuilder這東西,第一就是了。如下圖:
在上面圖中,第一個結果是離線安裝方式,也就是等下要說的,至於第二個搜尋結果是線上安裝方式。點選第一個連結,來到:
點選帶下載圖示的那個Download,
可以看到以下內容:
====================================================================================
Installing WindowBuilder Pro
All downloads are provided under the terms and conditions of the Eclipse Foundation Software User Agreementunless otherwise specified.
Develop Java graphical user interfaces in minutes for Swing, SWT, RCP and XWT with WindowBuilder Pro’s WYSIWYG, drag-and-drop interface. Use wizards, editors and intelligent layout assist to automatically generate clean Java code, with the visual design and source always in sync.
These instructions assume that you have already installed some flavor of Eclipse. If you have not, Eclipse can be downloaded from http://www.eclipse.org/downloads/. Instructions and system requirements for installing WindowBuilder can be found here.
Update Sites
Eclipse Version |
Release Version |
Integration Version | ||
---|---|---|---|---|
Update Site | Zipped Update Site | Update Site | Zipped Update Site | |
4.4 (Luna) | link | |||
4.3 (Kepler) | link | link | ||
4.2 (Juno) | link | link | ||
3.8 (Juno) | link | link | ||
3.7 (Indigo) | link | link | ||
3.6 (Helios) | link | |||
3.5 (Galileo) | link | |||
3.4 (Ganymede) | link |
根據你下載的Eclipse版本選擇,應該下載那個。因為我使用的是Indigo(3.7),下載了這個對應的WindowBuilder外掛,下載成功之後將其放到你的理想目錄(一般也就放在Eclipse同目錄下)。
第二,點選Eclipse中的Help,再次點選Install New Software,會出現:
點選Add,
在Name那一欄填寫外掛的名稱,如WindowBuilder。再點選Archive,選擇外掛所在目錄,之後就是:
點選Ok,後面的步驟沒什麼難度,就是點點滑鼠而已。select all勾選,同意協議,一步一步的簡單點選就可以了,安裝成功後會要求重啟Eclipse。
下面來看看如何使用WindowBuilder外掛,
使用的方式也比較簡單,Java GUI方面上的所有介面容器都在這了,有JFrame,還有JPanel等等。
使用之前,必須建立一個工程:
第一種方式就是直接利用我們熟悉的建立方式,直接New 一個Java Project。
第二種方式就是使用我們安裝的外掛,如上圖中出現的,選取Swt/JFace Java Project。
現在工程也建立好了,這兩種方式的不同也就是:第二種方式還會引用一些包而已,至於第一種方式更加普遍。
第一種方式:
工程建立好了,那麼進入真正如何使用。
使用New〉〉Other〉〉
選擇Swing Designer,(如果確實要選擇SWT Designer也可以,只不過SWT Designer相容性沒有Swing Designer這麼好,畢竟Swing Designer是針對Java使用的,它裡面的控制元件都是JXXXX形式的,比如JText,而SWT Designer確實XXXX形式的,比如Text),所以,最好選擇Swing Designer裡面的元件。
可以選擇Application Window,一般也就是應用視窗,所以選擇這個。
下面的操作就像新建一個類一樣。。
圖1
在WindowBuilder下可以直接建立工程,也可以建立單個的視窗。假設已經建立了工程,這裡選擇Swing Designer下的Application Window,點選Next >後,類似於新建類,在後續對話方塊中輸入Name和Package,如圖2所示。
圖2
Finish後,即可得到HelloWorld.java。開啟HelloWorld.java,可見其中已經預先生成了一些程式碼,是一個空白的窗體。點選程式碼視窗左下角新出現“Design"標籤,可以使用WindowBuilder Editor視覺化地檢視窗體(也可以在HelloWorld.java上點選右鍵,選擇Open With->WindowBuilder Editor),如圖3所示。
圖3
WindowBuilder Eidtor的介面類似於VS等工具,能夠視覺化地對介面進行設計。點選Layouts下的Absolute layout,再點選窗體,使用絕對定位;點選Components下的JTextField,再點選窗體,新增一個文字框,修改其Variable屬性為”txtName“;點選Components下的JButton,再點選窗體,新增一個按鈕,修改其Variable屬性為btnSubmit,修改其text屬性為”Submit“;點選Components下的JLabel,再點選窗體,新增一個標籤,修改其Variable屬性為lblName,修改其text屬性為”Name:“;同樣方法再新增一個JLabel,修改其Variable屬性為lblMessage,修改其text屬性為”Please enter your name.“;調整介面尺寸,最終得到的介面如圖4所示。
圖4
這時點選左下角的”Source“標籤回到程式碼編輯器,可以看到WindowBuilder生成的程式碼如下:
package text;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
public class HelloWorld
{
private JFrame
frame;
private JTextField
txtName;
/**
*
Launch the application.
*/
public static void main(String[]
args) {
EventQueue.invokeLater( new Runnable()
{
public void run()
{
try {
HelloWorld
window = new HelloWorld();
window.frame.setVisible( true );
}
catch (Exception
e) {
e.printStackTrace();
}
}
});
}
/**
*
Create the application.
*/
public HelloWorld()
{
initialize();
}
|