intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài giảng Chương 2: Lập trình giao diện đồ họa - Nguyễn Phúc Hào

Chia sẻ: Lavie Lavie | Ngày: | Loại File: PPT | Số trang:17

94
lượt xem
6
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Bài giảng Chương 2: Lập trình giao diện đồ họa trình bày về xử lý sự kiện; TextArea; Scrollbar. Mời các bạn tham khảo bài giảng để bổ sung thêm kiến thức về lĩnh vực này. Với các bạn chuyên ngành Công nghệ thông tin thì đây là tài liệu hữu ích.

Chủ đề:
Lưu

Nội dung Text: Bài giảng Chương 2: Lập trình giao diện đồ họa - Nguyễn Phúc Hào

  1. LẬP TRÌNH GIAO DIỆN ĐỒ HỌA Chương 2
  2. XỬ LÝ SỰ KiỆN Lớp sự kiện Mô tả ActionEvent Phát sinh khi một button được nhấn, một item trong danh sách chọn lựa được nhắp đôi hay một menu được chọn. AdjustmentEvent Phát sinh khi một thanh scrollbar được sử dụng. ComponentEvent Phát sinh khi một thành phần được thay đổi kích thước, được di chuyển, bị ẩn hay làm cho hoạt động được. FocusEvent Phát sinh khi một thành phần mất hay nhận focus từ bàn phím. ItemEvent Phát sinh khi một menu item được chọn hay bỏ chọn; hay khi một checkbox hay một item trong danh sách được click. WindowEvent Phát sinh khi một cửa sổ được kích hoạt, được đóng, được mở hay thoát. TextEvent Phát sinh khi giá trị trong thành phần text field hay text area bị thay đổi. MouseEvent Phát sinh khi chuột di chuyển, được click, được kéo hay bị thả ra. KeyEvent Phát sinh khi input được nhận từ bàn phím.
  3. XỬ LÝ SỰ KiỆN  Các giao diện được thi hành để xử lý một trong số những sự kiện này là:  ActionListener  AdjustmentListener  ComponentListener  FocusListener  ItemListener  WindowListener  TextListener  MouseListener  MouseMotionListener  KeyListener
  4. import java.awt.*; import java.awt.event.*; public void actionPerformed(ActionEvent ae) class evttest extends Frame implements { ActionListener If (ae.getSource()==btnResult) { Label lab=new Label(“Enter a { number”); int num=Integer.parseInt TextField tf1=new TextField(5); (tf1.getText())*2; TextField tf2=new TextField(5); tf2.setText(String.valueOf(num)); Button btnResult=new } Button(“Double is”); if (ae.getSource()==ext) Button ext=new Button(“exit”); { public evttest(String title) System.exit(0); { super(title); } setLayout(new FlowLayout()); } btnResult.addActionListener(this); public static void main(String args[]) ext.addActionListener(this); { add(lab); evttest t=new evttest(“Event handling”); add(tf1); t.setSize(300,200); add(btnResult); t.show(); add(tf2); } add(ext); } }
  5. XỬ LÝ SỰ KiỆN ActionListener AdjustmentListener E v ContainerListener e n FocusListener t L i ItemListener s t KeyListener e n MouseListener e r MouseMotionListener TextListener WindowListener
  6. XỬ LÝ SỰ KiỆN ActionListener Component ItemListener WindowListener Button Choice ComponentListener Dialog List Checkbox Frame FocusListener MenuItem KeyListener List TextField MouseListener MouseMotionLIstener
  7. XỬ LÝ SỰ KiỆN Giao tiếp Phương thức ActionListener actionPerformed(ActionEvent event) AdjustmentListener adjustmentValueChanged(AdjustmentEvent event) ComponentListener componentHidden(ComponentEvent event) ComponentAdapter componentShown(ComponentEvent event) componentMoved(ComponentEvent event) componentResized(ComponentEvent event) ContainerListener componentAdded(ContainerEvent event) ContainerAdapter componentRemoved(ContainerEvent event)
  8. XỬ LÝ SỰ KiỆN Giao tiếp Phương thức FocusListener focusGained(FocusEvent event) FocusAdapter focusLost(FocusEvent event) KeyListener keyPressed(KeyEvent event) KeyAdapter keyRelease(KeyEvent event) keyTyped(KeyEvent event) MouseListener mouseClicked(MouseEvent event) MouseAdapter mouseEntered(MouseEvent event) mouseExited(MouseEvent event) mousePressed(MouseEvent event) mouseReleased(MouseEvent event) MouseMotionListener mouseDragged(MouseEvent event) MouseMotionAdapter mouseMoved(MouseEvent event)
  9. XỬ LÝ SỰ KiỆN Giao tiếp Phương thức WindowListener windowOpened(WindowEvent event) WindowAdapter windowClosing(WindowEvent event) windowClosed(WindowEvent event) windowActivated(WindowEvent event) windowDeactivated(WindowEvent event) windowIconified(WindowEvent event) windowDeiconified(WindowEvent event) ItemListener itemStateChanged(ItemEvent event) KeyAdapter TextListener : textValueChanged(TextEvent event)
  10. TextArea Một Textarea được sử dụng khi văn bản nhập vào trên hai hay nhiều dòng. Textarea có một scrollbar. Thành phần TextArea là một trường văn bản có thể được soạn thảo với đặc tính nhiều dòng. Để tạo một Textarea, làm theo các bước sau:  Tạo một phần tử.  Chỉ ra số dòng hay số cột phần tử này cần có.  Bố trí phần tử này trên màn hình.
  11. TextArea  Trong Java, bạn có thể sử dụng các constructor sau để tạo TextArea:  TextArea(): Tạo một TextArea mới.  TextArea(int rows, int cols): Tạo một TextArea mới với số lượng cột và dòng được cho.  TextArea(String text): Tạo một TextArea mới với nhãn được cho.  TextArea(String text, int rows, int cols): Tạo một TextArea mới với nhãn, số dòng và số cột được cho.
  12. TextArea Phương thức Chức năng setText(String) Thiết lập nhãn cho TextArea. getText() Trả về nhãn của TextArea. setEdiable(boolean) Xác định xem trường có thể được soạn thảo hay không. Trường có thể được soạn thảo khi giá trị này là True. isEdiable() Xác định xem trường có đang trong mode soạn thảo được không. Trả về giá trị là kiểu Boolean. insertText(String, int) Chèn String được cho vào vị trí index được cho. replaceText(String, int, int) Thay thế văn bản nằm giữa vị trí int, int được cho.
  13. TextArea import java.awt.*; class TextAreatest extends Frame { Label lbl=new Label(“Details”); TextArea ta1=new TextArea(); public TextAreatest(String title) { super(title); setLayout(new FlowLayout()); add(lbl); add(ta1); } public static void main(String args[]) { TextAreatest t=new TextAreatest(“TextArea”); t.setSize(300,200); t.show(); } }
  14. Scrollbar  Một Scrollbar được sử dụng để tạo một thanh trượt nhằm kết hợp với component như Textarea, List, Panel,…. Nhằm quản lý hiển thị các nội dung lớn trong các component.  Để tạo một Textarea, làm theo các bước sau: Tạo một phần tử. Chỉ định thanh trượt nằm đứng hay ngang. Bố trí phần tử này trên màn hình.
  15. Scrollbar  Trong Java, bạn có thể sử dụng các constructor sau để tạo Scrollbar:  Scrollbar(): Tạo một Scrollbar mới.  Scrollbar(int huong): với huong có thể lấy giá trị là Scrollbar.VERTICAL hay Scrollbar.HORIZONTAL  Scrollbar(int huong,int vtri,int dodaiTrang,int min, int max): vtri là vị trí bắt đầu của thanh cuộn, dodaiTrang là số dòng trong một trang, min là điểm bắt đầu, và max là điểm kết thúc của thanh cuộn.
  16. Phương thức Chức năng setUnitIncrement(int inc) Chỉ định số dòng mỗi lần cuộn. getUnitIncrement() Trả về số dòng mỗi lần cuộn. setBlockIncrement(int b) Chỉ định số trang di dời khi có tình huống page xảy ra. setBlockIncrement() Trả về số trang di dời. setValue(int newPos) Đặt lại vị trí hiện hành cho thanh trượt. getValue() Lấy vị trí hiện tại của thanh trượt setValues(int vtri, int Thiết lập lại các thông số cho thanh trượt. dodaitrang, int min, int max)
  17. public class tHU extends Frame { private Scrollbar getScrollbar() { private static final long if (scrollbar == null) { serialVersionUID = 1L; scrollbar = new Scrollbar(); private Scrollbar scrollbar = null; scrollbar.setBounds(new Rectangle(278, private Scrollbar scrollbar1 = null; 32, 18, 136)); public tHU() { } super(); return scrollbar; initialize(); } } private Scrollbar getScrollbar1() { private void initialize() { if (scrollbar1 == null) { this.setLayout(null); scrollbar1 = new this.setSize(304, 200); Scrollbar(Scrollbar.HORIZONTAL); this.setTitle("Frame"); scrollbar1.setBounds(new Rectangle(6, this.add(getScrollbar(), null); 169, 289, 25)); this.add(getScrollbar1(), null); } } return scrollbar1; } }
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2