使用WindowBuilder輔助Java GUI開發


      WindowBuilder的前身是Instantiations開發的SWT Designer,2010年8月初Google收購了Instantiations,之后重新發布了Instantiations的開發工具,並且對所有開發人員免費,其中就包括用於Java圖形界面設計的WindowBuilder。

      WindowBuilder的下載方式見https://developers.google.com/java-dev-tools/download-wbpro?hl=zh-CN。安裝完成后,即可在File->New->Other...中看到WindowBuilder,如圖1所示。



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

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 196, 169);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		txtName = new JTextField();
		txtName.setBounds(57, 18, 93, 21);
		frame.getContentPane().add(txtName);
		txtName.setColumns(10);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(57, 46, 93, 23);
		frame.getContentPane().add(btnSubmit);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(20, 21, 54, 15);
		frame.getContentPane().add(lblName);
		
		JLabel lblMessage = new JLabel("Please enter your name.");
		lblMessage.setBounds(20, 79, 151, 15);
		frame.getContentPane().add(lblMessage);
	}

}

  

如果在點擊”Source“標簽前選中了某個組件,則點擊”Source“回到代碼編輯器后,光標則會自動定位到對應的組件。

      再點擊”Design“回到WindowBuilder Editor,雙擊Submit按鈕,同大多數GUI開發工具類似,WindowBuilder認為此時要編寫事件處理代碼,界面自動切換到代碼編輯器,且WindowBuilder已經在initialize()中完成了監聽器的定義和注冊:

btnSubmit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    }
});

  

WindowBuilder使用匿名內部類的形式實現事件處理器。修改initialize()如下:

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 196, 169);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
		
    txtName = new JTextField();
    txtName.setBounds(57, 18, 93, 21);
    frame.getContentPane().add(txtName);
    txtName.setColumns(10);
		
    final JLabel lblMessage = new JLabel("Please enter your name.");
    lblMessage.setBounds(20, 79, 151, 15);
    frame.getContentPane().add(lblMessage);
		
    JButton btnSubmit = new JButton("Submit");
    btnSubmit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            lblMessage.setText("Hello " + txtName.getText() + "!");
        }
    });
    btnSubmit.setBounds(57, 46, 93, 23);
    frame.getContentPane().add(btnSubmit);
		
    JLabel lblName = new JLabel("Name:");
    lblName.setBounds(20, 21, 54, 15);
    frame.getContentPane().add(lblName);
}                

  

這里將lblMessage的定義放在按鈕btnSubmit之前,並定義為final(在匿名類內部使用外部定義的對象,則該對象必須為final),運行結果如圖5所示。

 



 

圖5

      WindowBuilder能夠可視化地開發界面,並自動生成大部分代碼,可以極大地方便JAVA GUI的設計和開發,但WindowBuilder完成的代碼畢竟為機器自動生成,對於復雜的界面和事件處理,仍需要手動對代碼進行整理。多數情況下,對WindowBuilder生成的代碼進行移動后,WindowBuilder Editor仍能可視化地顯示界面。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM