Lập trình giao diện đồ họa

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

 

ppt17 trang | Chia sẻ: Mr Hưng | Lượt xem: 676 | Lượt tải: 0download
Nội dung tài liệu Lập trình giao diện đồ họa, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
LẬP TRÌNH GIAO DIỆN ĐỒ HỌAChương 2XỬ LÝ SỰ KiỆNPhát sinh khi input được nhận từ bàn phím.KeyEventPhát sinh khi chuột di chuyển, được click, được kéo hay bị thả ra.MouseEventPhát sinh khi giá trị trong thành phần text field hay text area bị thay đổi.TextEventPhát sinh khi một cửa sổ được kích hoạt, được đóng, được mở hay thoát.WindowEventPhá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.ItemEventPhát sinh khi một thành phần mất hay nhận focus từ bàn phím.FocusEventPhá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.ComponentEventPhát sinh khi một thanh scrollbar được sử dụng.AdjustmentEventPhá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. ActionEventMô tả Lớp sự kiệnXỬ LÝ SỰ KiỆNCác giao diện được thi hành để xử lý một trong số những sự kiện này là:ActionListenerAdjustmentListenerComponentListenerFocusListenerItemListenerWindowListenerTextListenerMouseListenerMouseMotionListenerKeyListenerimport java.awt.*;import java.awt.event.*;class evttest extends Frame implements ActionListener{ Label lab=new Label(“Enter a number”); TextField tf1=new TextField(5); TextField tf2=new TextField(5); Button btnResult=new Button(“Double is”); Button ext=new Button(“exit”); public evttest(String title)‏ { super(title); setLayout(new FlowLayout()); btnResult.addActionListener(this); ext.addActionListener(this); add(lab); add(tf1); add(btnResult); add(tf2); add(ext); }public void actionPerformed(ActionEvent ae)‏{If (ae.getSource()==btnResult)‏ { int num=Integer.parseInt (tf1.getText())*2; tf2.setText(String.valueOf(num)); } if (ae.getSource()==ext)‏ { System.exit(0); }}public static void main(String args[])‏{ evttest t=new evttest(“Event handling”); t.setSize(300,200); t.show();}}XỬ LÝ SỰ KiỆNXỬ LÝ SỰ KiỆNXỬ LÝ SỰ KiỆNcomponentAdded(ContainerEvent event) componentRemoved(ContainerEvent event)‏ContainerListener ContainerAdaptercomponentHidden(ComponentEvent event) componentShown(ComponentEvent event) componentMoved(ComponentEvent event) componentResized(ComponentEvent event)‏ComponentListener ComponentAdapteradjustmentValueChanged(AdjustmentEvent event)‏AdjustmentListeneractionPerformed(ActionEvent event)‏ActionListenerPhương thứcGiao tiếpXỬ LÝ SỰ KiỆNmouseDragged(MouseEvent event) mouseMoved(MouseEvent event)‏MouseMotionListener MouseMotionAdaptermouseClicked(MouseEvent event) mouseEntered(MouseEvent event) mouseExited(MouseEvent event) mousePressed(MouseEvent event) mouseReleased(MouseEvent event)‏MouseListener MouseAdapterkeyPressed(KeyEvent event) keyRelease(KeyEvent event) keyTyped(KeyEvent event)‏KeyListener KeyAdapterfocusGained(FocusEvent event) focusLost(FocusEvent event)‏FocusListener FocusAdapterPhương thứcGiao tiếpXỬ LÝ SỰ KiỆNitemStateChanged(ItemEvent event) ItemListener KeyAdapter windowOpened(WindowEvent event) windowClosing(WindowEvent event) windowClosed(WindowEvent event) windowActivated(WindowEvent event) windowDeactivated(WindowEvent event) windowIconified(WindowEvent event) windowDeiconified(WindowEvent event)‏WindowListener WindowAdapterPhương thứcGiao tiếpTextListener : textValueChanged(TextEvent event)‏TextAreaMộ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. TextAreaTrong 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.TextAreaThay thế văn bản nằm giữa vị trí int, int được cho.replaceText(String, int, int)‏Chèn String được cho vào vị trí index được cho.insertText(String, int)‏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.isEdiable()‏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.setEdiable(boolean)‏Trả về nhãn của TextArea.getText()‏Thiết lập nhãn cho TextArea.setText(String)‏Chức năng Phương thứcTextAreaimport 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();}}ScrollbarMộ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. ScrollbarTrong 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.Lấy vị trí hiện tại của thanh trượtgetValue()‏Thiết lập lại các thông số cho thanh trượt.setValues(int vtri, int dodaitrang, int min, int max)‏Đặt lại vị trí hiện hành cho thanh trượt.setValue(int newPos)‏Trả về số trang di dời.setBlockIncrement()‏Chỉ định số trang di dời khi có tình huống page xảy ra.setBlockIncrement(int b)‏Trả về số dòng mỗi lần cuộn.getUnitIncrement()‏Chỉ định số dòng mỗi lần cuộn.setUnitIncrement(int inc)‏Chức năng Phương thứcpublic class tHU extends Frame {private static final long serialVersionUID = 1L;private Scrollbar scrollbar = null;private Scrollbar scrollbar1 = null;public tHU() {super();initialize();}private void initialize() {this.setLayout(null);this.setSize(304, 200);this.setTitle("Frame");this.add(getScrollbar(), null);this.add(getScrollbar1(), null);}private Scrollbar getScrollbar() {if (scrollbar == null) {scrollbar = new Scrollbar();scrollbar.setBounds(new Rectangle(278, 32, 18, 136));}return scrollbar;}private Scrollbar getScrollbar1() {if (scrollbar1 == null) {scrollbar1 = new Scrollbar(Scrollbar.HORIZONTAL);scrollbar1.setBounds(new Rectangle(6, 169, 289, 25));}return scrollbar1;}}

Các file đính kèm theo tài liệu này:

  • pptsession02_tt__3047.ppt