JFace HelloWorld

Trong bài này, bạn sẽ làm quen với JFace bằng một chương trình đơn giản đầu tiên "HelloWorld".

 

Trình độ: Cơ bản về Java

Yêu cầu: Eclipse 3.2
Hãy làm theo các bước như sau.

1. Tạo một Java Project tên là JFaceDemo

2. Tạo một class tên là HelloWorld như sau:

package com.javaquan.tutorials.jface;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.widgets.Display;

public class HelloWorld extends ApplicationWindow{

public HelloWorld() {
super(null);
}

public static void main(String[] args) {
HelloWorld demo = new HelloWorld();
demo.setBlockOnOpen(true);
demo.open();
Display.getCurrent().dispose();
}
}



Giải thích đôi chút:



  • hàm setBlockOnOpen(true) để khi window được mở, nó giữ trạng thái mở cho đến khi người dùng nhấn nút Close
  • chương trình sẽ dừng ở dòng :demo.open() cho đến khi người dùng nhấn nút Close thì chương trình sẽ chạy tiếp đến lệnh tiếp theo là Display.getCurrent().disposeI(). Lệnh này có tác dụng giải phóng cửa sổ.

3. Chạy thử chương trình


Nhấn chuột phải vào HelloWorld trong Package Explorer và chọn Run as\Java Application



Bạn sẽ thấy một cửa sổ trắng xóa, không có nội dung và title. Bước cơ bản vậy là đã hoàn thành. Bây giờ bạn hãy chỉnh sửa đôi chút để cửa sổ mới tạo đẹp mắt hơn.

Thêm mắm thêm muối


1. Bạn cần override method createContents


...
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
...
public Control createContents(Composite parent) {
Shell shell = parent.getShell();
shell.setSize(320, 240);
shell.setText("HelloWorld window title");
return parent;
}



Phương thức này được gọi sau khi tất cả các widget đã được vẽ, nhưng trước khi window được hiển thị. Đây chính là nơi chúng ta chỉnh sửa thuộc tính của window, cũng như thêm các widget khác như Label, Text,Button, ComboBox...


2. Thêm một vài widget:


Bạn hãy tạo một composite là "con" (child) của Composite parent. Sau đó thì đặt các widget vào trong composite con này. Đương nhiên, lệnh return của createContents() sẽ trả về composite con chứ không phải parent.


import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
...
public class HelloWorld extends ApplicationWindow{
...
Text textYourName;
Button button1;
public Control createContents(Composite parent) {
Shell shell = parent.getShell();
shell.setSize(320, 240);
shell.setText("HelloWorld window title");

Composite composite = new Composite(parent,SWT.NONE);
composite.setLayout(new RowLayout());

new Label(composite,SWT.NONE).setText("Your name:");

textYourName = new Text(composite,SWT.BORDER);
textYourName.setText("Enter your name here !");

button1 = new Button(composite,SWT.PUSH);
button1.setText("Send");

return composite;
}
}


Chạy thử chương trình xem nào !




3. Thêm chức năng cho button "Send".  Đơn giản bạn chỉ việc add một Listener như sau:


button1.addSelectionListener(new SelectionListener(){

public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub

}

public void widgetSelected(SelectionEvent arg0) {
MessageDialog msgBox;
String strMsg = "Hello ";
strMsg = strMsg.concat(textYourName.getText());
msgBox = new MessageDialog(
null,
"HelloWorld",
null,
strMsg,
MessageDialog.INFORMATION,
new String[]{"OK"},
0);
msgBox.open();

}

});



Bây giờ mỗi khi người dùng nhấn nút Send, một dialog sẽ hiển thị một câu chào "Hello ...".
Như vậy là chúng ta đã hoàn thành bài đầu tiên rất cơ bản về JFace.
Trong các phần tiếp theo chúng ta sẽ cùng tìm hiểu các widget khác như menu,statusbar,treeViewer....




Download source code của ví dụ trên

Gặp lại vào hôm sau.

Hiện có (3) phản hồi: