lập trình đồ họa với awt tiếng việt

114 339 0
lập trình đồ họa với awt tiếng việt

Đ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 CAO Duc Thong – Thanglong University thongcd@thanglong.edu.vn N i dun g ng quan AWT Các thành ph n AWT Qu n lý trình bày lý ki n 2 Th vi nAWT 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) 3 Th vi nAWT Object Color Event Font Graphics Image Insets Polygon Rectangle Toolkit MenuComponent MenuBar XXXLayout MenuItem CheckboxGroup M enu Component C heckbox M enuItem 4 Component Button Canvas Checkbox Choice Container Label List Scrollbar TextComponent Th vi nAWT AWTException AWTError Applet Panel Frame Window Dialog ScrollPane F ileDialog TextArea TextField 5 Cácthànhph n AWT 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) 6 Cácthànhph n AWT 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 7 Cácthànhph n AWT 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 8 Cácthànhph n AWT Frame Th a Window nên ng là t Container Frame a là Component a là Container o Frame Frame() Frame(String title) Ví 9 Cácthànhph nAWT Frame import java.awt.*; public class UseLessFrame extends Frame { public UseLessFrame(){ super("Useless Frame"); setSize(300,200); setVisible(true); } public static void main(String[] args) { UseLessFrame frame = new UseLessFrame(); } } 10 Cácthànhph n AWT Frame 11 X l ý cács ki n k i n là gì Khi ng i dùng th c hi n t hành ng trên GUI, t ki n c sinh ra. Các ki n là các i ng mô nh ng gì ã y ra i tác ng a ng i dùng o ra nh ng lo i ki n khác nhau Kích chu t Button ActionEvent Eve ntHandler actionPerformed(ActionEvent e) { //Do something [...]... 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... t = new EventTest("Event handling"); t.setSize(300, 200); t.setVisible(true); } } 15 X l ý các s i k n V í lý ki n 16 X l ý cá cs ki n C ác lo i ki n (Even t java.util.EventObject java .awt. AWTEvent ActionEvent java .awt. event AdjustmentEv FocusEvent ent InputEvent ComponentEve nt KeyEvent MouseEvent ContainerEvent ItemEvent TextEvent WindowEvent 17 X l ý các s ki n M ô ki n 18 X lý cács ActionListener... (object) i ng gây ra ki n (source) ng n Xác nh i ng “nghe” (listener) và cài t các ph ng th c ng ng ng ký i ng nghe cho i ng gây ra ki n 21 X ki lý cács n Ví qu n lý ki import java .awt. *; import java .awt. event.ActionEvent; import java .awt. event.ActionListener; public class TestButton { n 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 n n n trong t Container Các ph ng th c kh t o Label() Label(String text) Label(String text, alignment): alignment có th tr Label.LEFT, Label.RIGHT, Label.CENTER Ph ng th c khác setFont(Font f) setText(String s) getText() getAlignment() nh n các giá 31 Cácthànhph nAWT Label import java .awt. *; public class LabelTest { public LabelTest()... Cácthànhph nAWT t ph ng th c TextField() TextField Ch ch a TextField(int columns t dòng n ) n TextField(String s, int columns) TextField(String s) addActionListener(ActionListener) echoCharIsSet() setEchoChar(char) setText() setColumn(int) 35 Cácthànhph nAWT TextField t ph ng th setEditable(boolean): so n th o c t ch TextField có c hay không isEditable(): xác nh xem có Editable không ch 36 Cácthànhph nAWT... TextFieldTest test = new TextFieldTest(); } } 38 Cácthànhph nAWT TextField 39 Cácthànhph nAWT TextArea Hi n th n n có nhi u i TextArea có t ph n t dòng t Scrollbar ng th c kh i o TextArea() TextArea(int rows, int columns) TextArea(String text) TextArea(String text, int rows, int columns) TextArea(String text, int rows, int columns, int ScrollType) 40 Cácthànhph nAWT TextArea t ph ng th setText/getText get/set row/column... 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 TextCom ponent Là p cha t ph a TextField và TextArea ng th c a TextComponent getCaretPosition() getSelectedText()... Cácthànhph nAWT GUIFrame public void setVisible(boolean visible){ if(visible){ Dimension d = Toolkit.getDefaultToolkit().getScreenSize( ); setLocation((d.width - getWidth())/2, (d.h eight -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... tf4.setBackground(Color.BLACK); tf4.setForeground(Color.WHITE); TextField tf5 = new TextField("Not editable"); tf5.setEditable(false); TextField tf6 = new TextField("I am selected text !!!"); Cácthànhph nAWT tf6.select(5, 13); 37 Cácthànhph nAWT TextField TextField tf7 = new TextField("Caret here >< "); TextField tf8 = new TextField("username",8); TextField tf9 = new TextField("password",8); tf9.setEchoChar('*'); ComponentTestFrame . trình h a v i AWT CAO Duc Thong – Thanglong University thongcd@thanglong.edu.vn N i dun g ng quan AWT Các thành ph n AWT Qu n lý trình bày lý ki n 2 Th vi nAWT AWT. ki n 2 Th vi nAWT 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. MenuComponent MenuBar XXXLayout MenuItem CheckboxGroup M enu Component C heckbox M enuItem 4 Component Button Canvas Checkbox Choice Container Label List Scrollbar TextComponent Th vi nAWT AWTException AWTError Applet Panel Frame Window Dialog ScrollPane F ileDialog TextArea TextField 5 Cácthànhph n AWT Component (thành ph n) Là t i

Ngày đăng: 23/10/2014, 22:03

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