LẬP TRÌNH ĐỒ HỌA VỚI AWT

136 626 0
LẬP TRÌNH ĐỒ HỌA VỚI AWT

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

LẬP TRÌNH ĐỒ HỌA VỚI AWT

L L   p p t t r r ì ì n n h h     h h   a a v v   i i A A W W T T CAO Duc Thong – Thanglong University thongcd@thanglong.edu.vn 2 N N   i i d d u u n n g g  ng quan  AWT  Các thành phn AWT  Qun lý trình bày   lý  kin 3 T T h h   v v i i   n n A A W W T T  AWT là vit t a Abstract Windowing Toolkit  AWT cho phép o các thành phn  a  AWT cho phép nhn  liu  chut, bàn phím  Các thành phn  a  n a AWT  t cha (Container)  Thành phn (Component)  Trình qun lý trình bày (Layout Manager)   a (Graphics), phông ch (Font),  kin (Event) 4 T T h h   v v i i   n n A A W W T T Object Color Event Font Graphics Image Insets Polygon Rectangle Toolk i t MenuComponent XXXLayout CheckboxGroup Component MenuBar MenuItem Menu Checkbox MenuItem 5 T T h h   v v i i   n n A A W W T T Component Button Canvas AWTException AWTError Applet Checkbox Choice Container Label List Scrollbar TextComponent Panel Window Scroll Pane TextArea TextField Frame Dialog FileDialog 6 C C á á c c t t h h à à n n h h p p h h   n n A A W W T T  Component (thành phn)  Là t i ng  a có th hin th c trên màn hình và có th ng tác i ngi dùng  Là t abstract superclass cho u t các component a AWT  Ví   Component: button, checkbox, scrollbar…  t  phng thc  n a Component  getBackGround(): tr  u n a Component  getBounds(): tr  phm vi a Component (Rectangle)  getFont(): tr  font hin i a Component  getForeGround(): tr  màu  a Component  getHeight(): tr  chiu cao a Component (pixel, kiu int) 7 C C á á c c t t h h à à n n h h p p h h   n n A A W W T T  t  phng thc  n a Component  getSize(): tr  kích thc a Component (Dimenstion)  getWidth(): tr  chiu ng a Component (int)  getX(), getY(): tr  a  hin i  isEnable(): boolean  paint(Graphics): chu trách nhim hin th component  repaint(): c i   i giao din cho component  setVisible(boolean): hin th component 8 C C á á c c t t h h à à n n h h p p h h   n n A A W W T T  Container (vt cha)  Cha trong gói java.awt  Là vùng có th t các thành phn giao din  t  loi Container: Panel, Frame, Dialog  Có t Component có kh ng cha các Component khác   thêm t Component vào Container ta  ng phng thc add(Component)  Container  ng t “layout manager”  p p các Component 9 C C á á c c t t h h à à n n h h p p h h   n n A A W W T T  Frame  Tha   Window nên ng là t Container  Frame a là Component a là Container  o Frame  Frame()  Frame(String title)  Ví  10 C C á á c c t t h h à à n n h h p p h h   n n A A W W T T  Frame import java.awt.*; public class UseLessFrame extends Frame { public UseLessFrame(){ super("Useless Frame"); setS ize(300,200); setVisible(true); } public static void main(String[] args) { UseLessFrame frame = new UseLessFrame(); } } [...]... ButtonPressed 24 Cácthànhph nAWT Frame dùng test các thành ph n khác import java .awt. *; import java .awt. event.*; public class ComponentTestFrame extends Frame implements WindowListener { public ComponentTestFrame(String title){ super(title); setBackground(SystemColor.control); setSize(400,300); setLocation(200,150); setLayout(new FlowLayout()); addWindowListener(this); } 25 Cácthànhph nAWT Frame dùng test các... windowDeactivated(WindowEvent e){} public void windowOpened(WindowEvent e){} } 26 Cácthànhph nAWT t ph ng th c a Frame Frame() Frame(String) Image getIconImage() MenuBar getMenuBar() String getTitle() Boolean isResizeable() setIconImage(Image) setMenuBar(MenuBar) setTitle(String) setVisible(boolean) 27 Cácthànhph nAWT GUIFrame import java .awt. *; import java .awt. event.*; public class GUIFrame extends Frame { public GUIFrame(String... args[]) { EventTest t = new EventTest("Event handling"); t.setSize(300, 200); t.setVisible(true); } } 15 X lý cács ki n Ví lý ki n 16 X lý cács ki n Các lo i ki n (Event java.util.EventObject java .awt. AWTEvent java .awt. event ActionEvent AdjustmentEvent FocusEvent ComponentEvent InputEvent ItemEvent ContainerEvent TextEvent KeyEvent WindowEvent MouseEvent 17 X lý cács ki n Mô ki n 18 X lý cács ki n ActionListener... gây ra ki n ki n (source) y ra ng ng ng “nghe” (listener) và cài ng nghe cho t qu n lý i i i ng mà ta t các ph ng gây ra n ng th c ki n 21 X lý cács ki n Ví ki n import java .awt. *; import java .awt. event.ActionEvent; import java .awt. event.ActionListener; public class TestButton { private Frame f; private Button b; public TestButton(){ f = new Frame("Test"); b = new Button("Press me"); b.setActionCommand("ButtonPressed");... } 29 Cácthànhph nAWT GUIFrame 30 Cácthànhph nAWT Label Dùng hi n th t Các ph ng th c kh t n n n trong t Container o Label() Label(String text) Label(String text, alignment): alignment có th nh n các giá tr Label.LEFT, Label.RIGHT, Label.CENTER Ph ng th c khác setFont(Font f) setText(String s) getText() getAlignment() 31 Cácthànhph nAWT Label import java .awt. *; public class LabelTest { public LabelTest()... 28 Cácthànhph nAWT GUIFrame public void setVisible(boolean visible){ if(visible){ Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); setLocation((d.width - getWidth())/2, (d.height -getHeight())/2); } super.setVisible(visible); } public static void main(String[] args){ GUIFrame frame = new GUIFrame("GUI Frame"); frame.setSize(400,300); frame.setVisible(true); } } 29 Cácthànhph nAWT GUIFrame 30... ra t lý t p nghe t i i dùng c i là ng p nghe ki n nào ó, ngu n sinh ra ki n có th có nhi u p mu n là listener ph i implement p nghe t giao ti p thích p 13 X lý cács ki n Ví lý ki n import java .awt. *; import java .awt. event.*; class EventTest extends Frame implements ActionListener { Label lab = new Label("Enter a number"); TextField tf1 = new TextField(5); TextField tf2 = new TextField(5); Button btnResult... l4.setBackground(Color.black); ComponentTestFrame frame = new ComponentTestFrame("Label Test"); 32 Cácthànhph nAWT Label frame.add(l1); frame.add(l2); frame.add(l3); frame.add(l4); frame.setVisible(true); } public static void main(String[] args) { LabelTest lt = new LabelTest(); } } 33 Cácthànhph nAWT TextComponent Là p cha t ph a TextField và TextArea ng th c a TextComponent getCaretPosition() getSelectedText()...Cácthànhph nAWT Frame 11 X lý cács ki n ki n là gì Khi ng i dùng th c hi n c sinh ra Các ki n là các i tác nhau ng a ng i t hành ng mô i dùng ng trên GUI, nh ng gì ã o ra nh ng lo i t ki n y ra ki n khác Kích chu... getCaretPosition() getSelectedText() getSelectionStart() getSelectionEnd() getText(), setText() select(int, int) setCaretPosition(int) setEditable(boolean) setSelectionStart(int) setSelectionEnd(int) 34 Cácthànhph nAWT t ng th c TextField() TextField Ch ch a ph t dòng n n TextField(int columns) TextField(String s) TextField(String s, int columns) addActionListener(ActionListener) echoCharIsSet() setEchoChar(char)

Ngày đăng: 25/04/2013, 20:49

Hình ảnh liên quan

Là ti ng a có th hin th c trên màn hình và - LẬP TRÌNH ĐỒ HỌA VỚI AWT

ti.

ng a có th hin th c trên màn hình và Xem tại trang 6 của tài liệu.
t mà n gi dùng nh n ch ut trên màn hình - LẬP TRÌNH ĐỒ HỌA VỚI AWT

t.

mà n gi dùng nh n ch ut trên màn hình Xem tại trang 128 của tài liệu.
Vi tch ng trình mô trò chi dò mìn. Trên màn hình - LẬP TRÌNH ĐỒ HỌA VỚI AWT

i.

tch ng trình mô trò chi dò mìn. Trên màn hình Xem tại trang 130 của tài liệu.

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan