Lập trình giao diện với AWT

17 619 3
Lập trình giao diện 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ựa chọn 1 container: Frame, Window, or Applet •Tạo các điều khiển (buttons, text areas ) • Đưa các điều khiển vào vùng chứa •Sắp xếp các điều khiển(layout) • Thêm các xử lý sự kiện (Listeners) •AWT viết tắt của Abstract Windowing Toolkit •AWT làtập hợp các lớp Java cho phép chúng ta tạo một GUI • Cung cấp các mục khác nhau để tạo hoạt động và hiệu ứng GUI • import java.awt.*; import java.awt.event.*; • Containers • Components • Layout managers • Graphics và drawing capabilities • Fonts •Events AW TEvent Font FontMetrics Component G raphics Object Color Canvas Button TextComponent Label List CheckBoxGroup CheckBox Choice Container Panel Applet Frame Dialog F ile Dia l og Window TextField TextArea MenuCom ponent MenuItem MenuBar Menu Scrollbar LayoutManager •Tất cả các thành phần cấu tạo nên chương trình GUI được gọi là component. • Ví dụ – Containers, – textfields, labels, checkboxes, textareas – scrollbars, scrollpanes, dialog • Là thành phần mà có thể chứa các thành phần khác,có thể vẽ và tô màu. • Có các frames, panes, Dialogs , ScrollPanes • Java.awt chứa một lớp có tên là Container. Lớp này dẫn xuất trực tiếp và không trực tiếp theo 2 cách là: – Frames – Panels •Làcác cửa sổ •Làlớpconcủa Windows • Được hiển thị trong một cửa sổ và có đường viền • javax.awt.Frame import java.awt.Frame; public class MyWindow { public static void main(String[] args) { Frame frame = new Frame("My First Window"); frame.setSize ( 400, 300 ); frame.setVisible( true ); } } import java.awt.Frame; public class MyWindow { public static void main(String[] args) { Frame frame = new Frame("My First Window"); frame.setSize ( 400, 300 ); frame.setVisible( true ); } } screen screen • java.awt.Frame –kíchthướcmặc định: bằng thanh tiêu đề –vị trí mặc định: (0,0) MyWindow MyWindow height screen height y x width screen width import java.awt.*; class FrameDemo extends Frame { public FrameDemo(String title) { super(title); } public static void main (String args[]) { FrameDemo ObjFr = new FrameDemo("I have been Framed!!!"); ObjFr.setSize(500,500); ObjFr.setVisible(true); } } Output • Là các vùng chứa trong một cửa sổ. • Đượcsử dụng để nhóm một số các thành phần •Một panel không có sẳn vì thế chúng ta cần phải thêm nó vào frame . •Hàm dựng – Panel() • java.awt.Panel – khung chứa bên trong các frame để gom nhóm các components: •1.cóthể gắn các components vào panels •2.cóthể gắn panel vào: a) frames b) panels khác public Panel( java.awt.LayoutManager ) tạo panel với layout manager. public Panel() tạo panel vớibộ trình bày mặc định FlowLayout. public Panel( java.awt.LayoutManager ) tạo panel với layout manager. public Panel() tạo panel vớibộ trình bày mặc định FlowLayout. • java.awt.Panel –Vídụ microwave GUI Button Button TextField TextField 12 Button 12 Button Panel(GridLayout) Panel(GridLayout) Panel(BorderLayout) Panel(BorderLayout) Frame(BorderLayout) Frame(BorderLayout) •Làmột lớpconcủa lớp Window • Đối tượng dialog được cấu trúc như sau : Frame myframe = new Frame(“My frame”); String title = “Title”; boolean modal = true; Dialog dlg = new Dialog( myframe, title, modal); • Ví dụ – textfields, labels, checkboxes, textareas – scrollbars, scrollpanes, dialog • java.awt.Button /* Create a button with text OK */ Button okButton = new Button( "OK" ); /* Create a button with text OK */ Button okButton = new Button( "OK" ); • java.awt.Label /* Create a label with text "Enter your name:" */ Label nameLabel = new Label( "Enter your name: " ); /* Create a label with text "Enter your name:" */ Label nameLabel = new Label( "Enter your name: " ); • java.awt.TextField /* Create a text field with text "Type Name Here" */ TextField nameField = new TextField( "Type Name Here" ); /* Create a text field with text "Type Name Here" */ TextField nameField = new TextField( "Type Name Here" ); • java.awt.CheckBox /* Create a check box with text bold */ CheckBox boldCheck = new CheckBox( "Bold" ); /* Create a check box with text bold */ CheckBox boldCheck = new CheckBox( "Bold" ); Label Button Button Checkbox Choice List Scrollbar TextField TextArea CheckboxGroup Checkbox • Gắn component vào khung chứa: objectName.add(… ); • Lấy thông tin của component: objectName.getxxx( ); • Gán thông tin cho component: objectName.setxxx( ); • Được dùng để hiển thị chuỗi(String) • Các hàm dựng: – Label( ) – Label(String labeltext) – Label(String labeltext, int alignment) •Cácphương thức: – setFont(Font f) – setText(String s) – getText( ) import java.awt.*; class AcceptName extends Frame { TextField txtName = new TextField(20); Label lblName = new Label("Name :"); public AcceptName (String title) { super(title); setLayout(new FlowLayout()); add(lblName); add(txtName); } public static void main(String args[]) { AcceptName ObjAccName = new AcceptName ("Testing components!"); ObjAccName.setSize(300,200); ObjAccName.show(); } } Output •Làđiềukhiểntext chophéphiểnthị text hoặc cho user nhậpdữ liệu vào. •Các hàmdựng: – TextField( ) – TextField(int columns) – TextField(String s) – TextField(String s, int columns) • Các phương thức: – setEchoChar(char) – setTest(String s) – getText( ) – setEditable(boolean) – isEditable( ) • Được dùng khi text có nội dung từ hai dòng trở lên •Làđiềukhiển text có thể soạnthảo đượcvới nhiều dòng •Cácbước để tạoTextArea: – Tạomộtphầntử (element) – Chỉ ra số dòng hay số cột(tùychọn) – Chỉ ra vị trí của điềukhiểntrênmànhình • Các hàm dựng: – TextArea( ) – TextArea(int rows, int cols ) – TextArea(String text) – TextArea(String text, int rows, int cols) • setText(String) • getText( ) • setEditable(boolean) • isEditable( ) • insertText(String, int) • replaceText(String, int, int) addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { setVisible(false); System.exit(0); } }); } public static void main(String args[]) { TextComments ObjComment = new TextComments("Testing components!"); ObjComment.setSize(200,200); ObjComment.show(); } } import java.awt.*; import java.awt.event.*; class TextComments extends Frame { TextArea txtComment = new TextArea(5,25); Label lblCom = new Label("Comments :"); public TextComments(String title) { super(title); setLayout(new FlowLayout()); add(lblCom); add(txtComment); Output • Các nút Push hay Command là cách dễ nhất để lấycácsư kiện của user •Cácbước để tạo button: – Tạomộtphầntử button, nên tạocho nómột caption để chỉ ra mục đích củanó – Chỉ ra vị trí đặt button trên màn hình – HIển thị ra trên màn hình • Các hàm dựng: – Button( ) – Button(String text) addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { setVisible(false); System.exit(0); } }); } public static void main(String args[]) { ButtonTest ObjTest = new ButtonTest("The three little buttons!"); ObjTest.setSize(500,500); ObjTest.show(); } } import java.awt.*; import java.awt.event.*; class ButtonTest extends Frame { Button btnBread = new Button("Bread!"); Button btnButter = new Button("Butter!"); Button btnJam = new Button("Jam!"); public ButtonTest(String title) { super(title); setLayout(new FlowLayout()); add(btnBread); add(btnButter); add(btnJam); Output • Checkboxes được dùng khi cho phép user nhiềucọn chọnlựa • Radiobuttons được dùng để user chỉ ra mộtlựachọn duy nhất •Cácbước để tạo checkbox hoặc radiobutton: – Tạomộtphầntử (element) – Khởitạogiátrị ban đầu(cógiátrị selected hay unselected) – Chỉ ra vị trí trên màn hình – Hiểnthị ra màn hình •Cáchàmdựng để tạo checkbox: – Checkbox( ) – Checkbox(String text) • Để tạo radiobutton, ta phảitạo đốitượng CheckBoxGroup trướckhitạobutton addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { setVisible(false); System.exit(0); } }); } public static void main(String args[]) { Hobbies ObjHobby = new Hobbies ("A basket full of checkboxes!"); ObjHobby.setSize(300,300); // Objhobby.pack(); ObjHobby.show(); } } import java.awt.*; import java.awt.event.*; class Hobbies extends Frame { Checkbox cboxRead = new Checkbox("Reading",false); Checkbox cboxMus = new Checkbox("Music",false); Checkbox cboxPaint = new Checkbox("Painting",false); Checkbox cboxMovie = new Checkbox("Movies",false); Checkbox cboxDance = new Checkbox("Dancing",false); Label lblQts = new Label("What's your hobby?" ); public Hobbies(String str ) { super(str); setLayout(new GridLayout(6,1)); add(lblQts); add(cboxRead); add(cboxMus); add(cboxPaint); add(cboxMovie); add(cboxDance); Output import java.awt.*; import java.awt.event.*; class Qualification extends Frame { CheckboxGroup cg = new CheckboxGroup(); Checkbox radUnder = new Checkbox("Undergraduate",cg,false); Checkbox radGra = new Checkbox("Graduate",cg,false); Checkbox radPost = new Checkbox("Post Graduate",cg,false); Checkbox radDoc = new Checkbox("Doctorate",cg,false); Label lblQts = new Label("What's your primary qualification?" ); public Qualification(String str) { super(str); setLayout(new GridLayout(6,1)); add(lblQts); add(radUnder); add(radGra); add(radPost); add(radDoc); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { setVisible(false); System.exit(0); } }); } public static void main(String args[]) { Qualification ObjQualification = new Qualification ("Literacy!"); ObjQualification.pack(); ObjQualification.show( ); } } Output •Lớp ‘Choice’ cho phép ta tạo danh sách có nhiềuchọn lựa • Khi list đượctạolần đầu tiên, nó đượckhởitạolà empty •Cácbước để tạo danh sách chọnlựa: –Tạo mộtphầntử –Thêmcácmục (có kiểu Strings) vào danh sách đó, từng mụcmột –Chỉ ra vị trí trên màn hình –Hiểnthị ra màn hình •Vídụ Choice colors=new Choice( ); colors.addItem(“Red”); colors.addItem(“Green”); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { setVisible(false); System.exit(0); } }); } public static void main(String args[]) { Stars ObjStar = new Stars ("A sky full of stars!"); ObjStar.setSize(400,400); ObjStar.show(); } } import java.awt.*; import java.awt.event.*; class Stars extends Frame { Choice moviestars = new Choice(); Label lblQts = new Label("Who is your favorite movie star?"); public Stars(String str) { super(str); setLayout(new FlowLayout()); moviestars.addItem("Antonio Banderas"); moviestars.addItem("Leonardo DiCaprio"); moviestars.addItem("Sandra Bullock"); moviestars.addItem("Hugh Grant"); moviestars.addItem("Julia Roberts"); add(lblQts); add(moviestars); Output •Cácloại layout khác nhau: – Flow Layout – Border Layout – Card Layout – Grid Layout – GridBag Layout • Trình quảnlýlayout được thiết lập bằng cách gọi phương thức ‘setLayout( )’ •Làtrìnhquảnlýlayout mặc định cho các applet và các panel •Với FlowLayout các thành phầnsẽ được xắpxếptừ góc trái trên đến góc phảidưới của màn hình • Các constructor: FlowLayout mylayout = new FlowLayout(); FlowLayout exLayout = new flowLayout(FlowLayout.RIGHT); Flow Layout – Left and Right Aligned • Là trình quản lý layout mặc định cho Window, Frame và Dialog • Trình quản lý này có thể xắp xếp đến5 thành phần trong container • Các thành phầncóthể được đặt vào 5 hướng NORTH, EAST, SOUTH, WEST và CENTER của container • Ví dụ: Để thêm một thành phần vào vùn g North của container Button b1= new Button(“North Button”); setLayout(new BorderLayout( )); add(b1, BorderLayout.NORTH); [...]... hình Event Listener: – – – Abstract Windowing Toolkit Trình duyệt Các trình xử lý sự kiện do các lập trình viên tạo riêng • Các ứng dụng cần đăng ký trình xử lý sự kiện với đối đối tượng • Các trình xử lý này được gọi khi có một sự kiện tương ứng xảy ra – – – – Cài đặt Listener tương ứng Nhận diện được tất cả các thành phần tạo ra sự kiện Nhận diện được tất cả các sự kiện được xử lý Cài đặt các phương...• • • • • Có thể lưu trữ một danh sách các kiểu layout khác nhau Mỗi layout được xem như một thẻ (card) Thẻ thường là đối tượng Panel Một thành phần độc lập như button sẽ điều khiển các thẻ được đặt ở phía trên nhất Các bước để tạo CardLayout: – – Bố trí layout của panel chính là CardLayout Lần lượt thêm các panel khác vào panel chính • Hỗ trợ việc chia... TextEvent MouseEvent • KeyEvent ActionListener AdjustmentListener ComponentListener FocusListener ItemListener WindowListener TextListener MouseListener MouseMotionListener KeyListener • Được cung cấp bởi gói AWT • Cung cấp một tập hợp các phương thức để vẽ như sau: – – – – – – Oval Rectangle Square Circle Lines Text in different fonts • Các loại menu : – – Pull-down Pop-up menu • Chỉ có thể đặt các thanh menubar... xArray[ ], int yArray[ ], int totalPoints); – g.setFont(new Font("Times Roman", Font.BOLD,15)); – drawPolygon(int x[ ], int y[ ], int numPoints); fillPolygon(int x[ ], int y[ ], int numPoints); • Bảng trình bày các giá trị màu RGB thôn thường • Java sử dụng màu RGB • Bảng các giá trị màu Element Red Green Blue Range 0-255 0-255 0-255 Cú pháp của hàm dựng để tạo một màu color(int red, int green, int... Gray Dark Gray Black Pink Orange Yellow Magenta Red 255 192 128 64 0 255 255 255 255 Green 255 192 128 64 0 175 200 255 0 Blue 255 192 128 64 0 175 0 0 255 • Hàm dựng Font nhận 3 tham số – • Gói java .awt package cung cấp bởi lớp ‘Font’ • Các phương thức của lớp Font: – – – – getAllFont( ) getLocalGraphicsEnvironment( ) getFont( ) getFontList( ) • Đo lường các ký tự khác nhau hiển thị trong các font

Ngày đăng: 13/05/2014, 10:55

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

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

Tài liệu liên quan