Bài giảng Lập trình đồ họa: Các thành phần AWT

pdf 60 trang phuongnguyen 1740
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình đồ họa: Các thành phần AWT", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_lap_trinh_do_hoa_cac_thanh_phan_awt.pdf

Nội dung text: Bài giảng Lập trình đồ họa: Các thành phần AWT

  1. CCáácctthhàànnhhpphhầầnnAAWWTT  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
  2. CCáácctthhàànnhhpphhầầnnAAWWTT  Frame dùng để test các thành phần khác public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } public void windowActivated(WindowEvent e){} public void windowClosed(WindowEvent e){} public void windowIconified(WindowEvent e){} public void windowDeiconified(WindowEvent e){} public void windowDeactivated(WindowEvent e){} public void windowOpened(WindowEvent e){} } 26
  3. CCáácctthhàànnhhpphhầầnnAAWWTT  Một số phương thức của Frame  Frame()  Frame(String)  Image getIconImage()  MenuBar getMenuBar()  String getTitle()  Boolean isResizeable()  setIconImage(Image)  setMenuBar(MenuBar)  setTitle(String)  setVisible(boolean) 27
  4. CCáácctthhàànnhhpphhầầnnAAWWTT  GUIFrame import java.awt.*; import java.awt.event.*; public class GUIFrame extends Frame { public GUIFrame(String title){ super(title); setBackground(SystemColor.control); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } }); } 28
  5. CCáácctthhàànnhhpphhầầnnAAWWTT  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
  6. CCáácctthhàànnhhpphhầầnnAAWWTT  GUIFrame 30
  7. CCáácctthhàànnhhpphhầầnnAAWWTT  Label  Dùng để hiển thị một đoạn văn bản trong một Container  Các phương thức khởt tạ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
  8. CCáácctthhàànnhhpphhầầnnAAWWTT  Label import java.awt.*; public class LabelTest { public LabelTest() { Label l1 = new Label("Label"); Label l2 = new Label("I am a label"); l2.setFont(new Font("Timesroman", Font.BOLD, 18)); Label l3 = new Label(); l3.setText("I am disable"); l3.setEnabled(false); Label l4 = new Label("Colored, right aligned", Label.RIGHT); l4.setForeground(Color.green); l4.setBackground(Color.black); ComponentTestFrame frame = new ComponentTestFrame("Label Test"); 32
  9. CCáácctthhàànnhhpphhầầnnAAWWTT  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
  10. CCáácctthhàànnhhpphhầầnnAAWWTT  TextComponent  Là lớp cha của TextField và TextArea  Một số phương thức của TextComponent  getCaretPosition()  getSelectedText()  getSelectionStart()  getSelectionEnd()  getText(), setText()  select(int, int)  setCaretPosition(int)  setEditable(boolean)  setSelectionStart(int)  setSelectionEnd(int) 34
  11. CCáácctthhàànnhhpphhầầnnAAWWTT  Một số phương thức  TextField()  TextField  TextField(int columns)  Chỉ chứa một dòng văn bản  TextField(String s)  TextField(String s, int columns)  addActionListener(ActionListener)  echoCharIsSet()  setEchoChar(char)  setText()  setColumn(int) 35
  12. CCáácctthhàànnhhpphhầầnnAAWWTT  TextField  Một số phương thức  setEditable(boolean): đặt chế độTextField có soạn thảo được hay không  isEditable(): xác định xem có ở chế độ Editable không 36
  13. CCáácctthhàànnhhpphhầầnnAAWWTT  TextField import java.awt.*; public class TextFieldTest { public TextFieldTest() { super(); TextField tf1 = new TextField(); TextField tf2 = new TextField(25); tf2.setText("Type stuff here"); tf2.setFont(new Font("Timesroman",Font.BOLD,18)); TextField tf3 = new TextField("I am disabled",15); tf3.setEnabled(false); TextField tf4 = new TextField("Colors"); 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 !!!");
  14. CCáácctthhàànnhhpphhầầnnAAWWTT tf6.select(5, 13); 37
  15. CCáácctthhàànnhhpphhầầnnAAWWTT  TextField TextField tf7 = new TextField("Caret here >< "); TextField tf8 = new TextField("username",8); TextField tf9 = new TextField("password",8); tf9.setEchoChar('*'); ComponentTestFrame frame = new ComponentTestFrame("TextField Test"); frame.add(tf1); frame.add(tf2); frame.add(tf3); frame.add(tf4); frame.add(tf5); frame.add(tf6); frame.add(tf7); frame.add(tf8); frame.add(tf9); frame.setVisible(true); tf7.setCaretPosition(14); } public static void main(String[] args) { TextFieldTest test = new TextFieldTest(); } } 38
  16. CCáácctthhàànnhhpphhầầnnAAWWTT  TextField 39
  17. CCáácctthhàànnhhpphhầầnnAAWWTT  TextArea  Hiển thị văn bản có nhiều hơn một dòng  Mỗi TextArea có một Scrollbar  Một số phương thức khởi tạ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
  18. CCáácctthhàànnhhpphhầầnnAAWWTT  TextArea  Một số phương thức thường dùng  setText/getText  get/set row/column  setEditable/isEditable  append(String)  insert(String s, int i): chèn chuỗi vào một vị trí  replaceRange(String, int, int): thay thế văn bản nằm giữa vị trí int và int cho trước 41
  19. CCáácctthhàànnhhpphhầầnnAAWWTT  TextArea import java.awt.*; public class TextAreaTest { public TextAreaTest() { super(); TextArea ta1 = new TextArea(10,20); TextArea ta2 = new TextArea("Text Area\n with color",10,10,TextArea.SCROLLBARS_NONE); ta2.setFont(new Font("Timesroman",Font.ITALIC,12)); ta2.setBackground(Color.BLACK); ta2.setForeground(Color.GREEN); TextArea ta3 = new TextArea("This textarea is not editable", 10,15,TextArea.SCROLLBARS_HORIZONTAL_ONLY); ta3.setEditable(false); TextArea ta4 = new TextArea("This textarea is not enable", 4,25,TextArea.SCROLLBARS_NONE); 42
  20. CCáácctthhàànnhhpphhầầnnAAWWTT  TextArea ta4.setEditable(false); ComponentTestFrame frame = new ComponentTestFrame("TextArea Test"); frame.add(ta1); frame.add(ta2); frame.add(ta3); frame.add(ta4); frame.setVisible(true); } public static void main(String[] args) { TextAreaTest test = new TextAreaTest(); } } 43
  21. CCáácctthhàànnhhpphhầầnnAAWWTT  TextArea 44
  22. CCáácctthhàànnhhpphhầầnnAAWWTT  Button  Tương tác với người dùng  Thực hiện một hành động nào đó khi người dùng nhấn nút  Một số phương thức  Button()  Button(String text)  addActionListener(ActionListener)  String getLabel()  setLabel(String)  removeActionListener(ActionListener) 45
  23. CCáácctthhàànnhhpphhầầnnAAWWTT  Button import java.awt.Frame; import java.awt.*; public class ButtonTest { public ButtonTest() { Button b1 = new Button("Button"); Button b2 = new Button(); b2.setLabel("Press me!"); b2.setFont(new Font("Timesroman", Font.BOLD, 16)); Button b3 = new Button("Can't press me"); b3.setEnabled(false); Button b4 = new Button("Colors"); b4.setForeground(Color.green); b4.setBackground(Color.black); ComponentTestFrame frame = new ComponentTestFrame("Button Test"); 46
  24. CCáácctthhàànnhhpphhầầnnAAWWTT  Button frame.add(b1); frame.add(b2); frame.add(b3); frame.add(b4); frame.setVisible(true); } public static void main(String[] args) { ButtonTest lt = new ButtonTest(); } } 47
  25. CCáácctthhàànnhhpphhầầnnAAWWTT  Checkbox và RadioButton  Checkbox cho phép người dùng chọn một hay nhiều tùy chọn  Có thể chọn nhiều option của Checkbox  RadioButton cũng giống như Checkbox nhưng chỉ cho phép chọn một option tại một thời điểm  Thành phần Checkbox có thể dùng một lớp phụ (CheckboxGroup để tạo RadioButton)  Một số phương thức  Checkbox()  Checkbox(String)  Checkbox(String, boolean)  Checkbox(String, boolean, CheckboxGroup) 48
  26. CCáácctthhàànnhhpphhầầnnAAWWTT  Checkbox và RadioButton  Một số phương thức  addItemListener(ItemListener)  set/getCheckboxGroup()  set/getLabel()  set/getState()  removeItemListener(ItemListener) 49
  27. CCáácctthhàànnhhpphhầầnnAAWWTT  Checkbox và RadioButton import java.awt.Checkbox; public class CheckboxTest { public CheckboxTest() { super(); Checkbox cb1 = new Checkbox("Java",false); Checkbox cb2 = new Checkbox("C++",false); cb2.setEnabled(false); Checkbox cb3 = new Checkbox("HTML",true); Checkbox cb4 = new Checkbox(); cb4.setLabel("Pascal"); cb4.setState(false); ComponentTestFrame frame = new ComponentTestFrame("CheckboxTest"); frame.add(cb1); frame.add(cb2); frame.add(cb3); frame.add(cb4); frame.setVisible(true); } 50
  28. CCáácctthhàànnhhpphhầầnnAAWWTT  Checkbox và RadioButton public static void main(String[] args) { CheckboxTest test = new CheckboxTest(); } } 51
  29. CCáácctthhàànnhhpphhầầnnAAWWTT  Checkbox và RadioButton import java.awt.*; public class CheckboxGroupTest { public CheckboxGroupTest() { super(); CheckboxGroup group = new CheckboxGroup(); Checkbox cb1 = new Checkbox("Java",false,group); Checkbox cb2 = new Checkbox("C++",false,group); cb2.setEnabled(false); Checkbox cb3 = new Checkbox("HTML",true,group); Checkbox cb4 = new Checkbox("",true,group); cb4.setLabel("Pascal"); ComponentTestFrame frame = new ComponentTestFrame("CheckboxGroupTest"); frame.add(cb1); frame.add(cb2); frame.add(cb3); frame.add(cb4); 52
  30. CCáácctthhàànnhhpphhầầnnAAWWTT  Checkbox và RadioButton frame.setVisible(true); } public static void main(String[] args) { CheckboxGroupTest test = new CheckboxGroupTest(); } } 53
  31. CCáácctthhàànnhhpphhầầnnAAWWTT  Choice  Cho phép lựa chọn trên một danh sách các thành phần  Một số phương thức  Choice()  add(String)  addItem(String)  addItemListener(ItemListener)  getItem(int)  getItemCount()  getSelectedIndex()  insert(String, int) 54
  32. CCáácctthhàànnhhpphhầầnnAAWWTT  remove(int)  Choice  remove(String)  Một số phương thức  removeAll()  removeItemListener(ItemListener)  select(int)  select(String) 55
  33. CCáácctthhàànnhhpphhầầnnAAWWTT import java.awt.*; public class ChoiceTest {  Choice public ChoiceTest() { super(); Choice c1 = new Choice(); c1.add("Soup"); c1.add("Salad"); Choice c2 = new Choice(); c2.add("Java"); c2.add("C++"); c2.add("HTML"); c2.add("JavaScript"); c2.add("ADA"); Choice c3 = new Choice(); c3.add("One"); c3.add("Two"); c3.add("Three"); 56
  34. CCáácctthhàànnhhpphhầầnnAAWWTT  Choice c3.setFont(new Font("Timesroman",Font.BOLD,18)); Choice c4 = new Choice(); c4.add("Computer"); c4.setEnabled(false); ComponentTestFrame frame = new ComponentTestFrame("ChoiceTest"); frame.add(c1); frame.add(c2); frame.add(c3); frame.add(c4); frame.setVisible(true); } public static void main(String[] args) { ChoiceTest test = new ChoiceTest(); } } 57
  35. CCáácctthhàànnhhpphhầầnnAAWWTT  Choice 58
  36. CCáácctthhàànnhhpphhầầnnAAWWTT  List  Giống như Choice nhưng cho phép hiện thị nhiều lựa chọn cùng lúc  Người dùng có thể lựa chọn nhiều item  Tham khảo API Documentation 59
  37. CCáácctthhàànnhhpphhầầnnAAWWTT  Canvas  Là khu vực cho phép hiển thị hình ảnh hoặc nhận về các sự kiện của người dùng  Muốn vẽ bất cứ thứ gì trên Canvas ta phải cài đặt lại phương thức paint(Graphics)  Ví dụ import java.awt.*; public class CanvasTest extends Canvas { public void paint(Graphics g){ setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 16)); g.drawString("Canvas", 15, 25); } 60
  38. CCáácctthhàànnhhpphhầầnnAAWWTT  Canvas public static void main(String[] args) { CanvasTest c1 = new CanvasTest(); c1.setSize(100,100); CanvasTest c2 = new CanvasTest(); c2.setSize(100,100); c2.setBackground(Color.orange); c2.setForeground(Color.blue); CanvasTest c3 = new CanvasTest(); c3.setSize(200,50); c3.setBackground(Color.white); ComponentTestFrame frame = new ComponentTestFrame ("Canvas Test"); frame.add(c1);frame.add(c2);frame.add(c3); frame.pack(); frame.setVisible(true); } } 61
  39. C CáácctthhàànnhhpphhầầnnAAWWTT  Tổng quan về Menu MenuBar Menu Separator MenuItem 62
  40. CCáácctthhàànnhhpphhầầnnAAWWTT  Tạo MenuBar  Tổng quan về Menu MenuBar menuBar = new MenuBar();  Gắn MenuBar vào cửa sổ myFrame.setMenuBar(menuBar);  Tạo Menu Menu fileMenu = new Menu(“File”);  Gắn Menu vào MenuBar menuBar.add(fileMenu);  Tạo MenuItem MenuItem fileOpen = new MenuItem(“Open”); 63
  41. CCáácctthhàànnhhpphhầầnnAAWWTT fileMenu.add(fileOpen);  Tạo đường phân cách  Tổng quan về Menu fileMenu.addSeparator();  Gắn MenuItem vào Menu 64
  42. CCáácctthhàànnhhpphhầầnnAAWWTT  set/getLabel();   MenuItem get/setShortcut(MenuShortcu  MenuItem() t)  MenuItem(String)  isEnabled()  MenuItem(String, MenuShortcut)  setEnabled()  addActionListener(ActionListener) 65
  43. CCáácctthhàànnhhpphhầầnnAAWWTT  Ví dụ Menu import java.awt.*; import java.awt.event.KeyEvent; public class MenuTest { public MenuTest() { super(); MenuItem fileNew = new MenuItem("New"); fileNew.setShortcut(new MenuShortcut(KeyEvent.VK_N)); MenuItem fileOpen = new MenuItem("Open"); fileOpen.setShortcut(new MenuShortcut(KeyEvent.VK_O)); MenuItem fileSave = new MenuItem("Save"); fileSave.setShortcut(new MenuShortcut(KeyEvent.VK_S)); fileSave.setEnabled(false); MenuItem fileSaveAs = new MenuItem("Save As"); 66
  44. CCáácctthhàànnhhpphhầầnnAAWWTT  Ví dụ Menu fileSaveAs.setShortcut(new MenuShortcut(KeyEvent.VK_A)); fileSaveAs.setEnabled(false); MenuItem fileExit = new MenuItem("Exit"); fileExit.setShortcut(new MenuShortcut(KeyEvent.VK_X)); MenuItem editCut = new MenuItem("Cut"); editCut.setShortcut(new MenuShortcut(KeyEvent.VK_X)); MenuItem editCopy = new MenuItem("Copy"); editCopy.setShortcut(new MenuShortcut(KeyEvent.VK_C)); MenuItem editPaste = new MenuItem("Paste"); editPaste.setShortcut(new MenuShortcut(KeyEvent.VK_P)); Menu file = new Menu("File"); 67
  45. CCáácctthhàànnhhpphhầầnnAAWWTT  Ví dụ Menu file.add(fileNew);file.add(fileOpen); file.add(fileSave);file.add(fileSaveAs); file.addSeparator();file.add(fileExit); Menu edit = new Menu("Edit"); edit.add(editCut); edit.add(editCopy); edit.add(editPaste); MenuBar menuBar = new MenuBar(); menuBar.add(file); menuBar.add(edit); ComponentTestFrame frame = new ComponentTestFrame("MenuTest"); frame.setMenuBar(menuBar); frame.setVisible(true); } public static void main(String[] args) { MenuTest test = new MenuTest(); }
  46. } CCáácctthhàànnhhpphhầầnnAAWWTT 68
  47. CCáácctthhàànnhhpphhầầnnAAWWTT  Ví dụ Menu 69
  48. CCáácctthhàànnhhpphhầầnnAAWWTT  Là lớp con của Menu  PopupMenu  Đối tượng PopupMenu có thể hiển thị mà không cần MenuBar  Có thể hiển thị ở vị trí bất kỳ theo yêu cầu import java.awt.*; import java.awt.event.KeyEvent; public class PopupMenuTest { public PopupMenuTest() { super(); MenuItem cut = new MenuItem("Cut"); cut.setShortcut(new MenuShortcut(KeyEvent.VK_X)); MenuItem copy = new MenuItem("Copy"); copy.setShortcut(new MenuShortcut(KeyEvent.VK_C)); MenuItem paste = new MenuItem("Paste"); 70
  49. CCáácctthhàànnhhpphhầầnnAAWWTT  PopupMenu paste.setShortcut(new MenuShortcut(KeyEvent.VK_P)); MenuItem delete = new MenuItem("Delete"); PopupMenu popupMenu = new PopupMenu("Clipboard"); popupMenu.add(cut);popupMenu.add(copy); popupMenu.add(paste);popupMenu.add(delete); ComponentTestFrame frame = new ComponentTestFrame("PopupMenuTest"); frame.add(popupMenu);frame.setVisible(true); popupMenu.show(frame,50,50); } public static void main(String[] args) { PopupMenuTest test = new PopupMenuTest(); } } 71
  50. CCáácctthhàànnhhpphhầầnnAAWWTT  PopupMenu 72
  51. CCáácctthhàànnhhpphhầầnnAAWWTT  Panel  Là một Container nhưng không thể tồn tại độc lập  Phải gắn Panel vào một Frame  Dùng Panel để nhóm các thành phần GUI lại với nhau import java.awt.*; public class PanelTest { public PanelTest() { super(); Label lb1 = new Label("URL: "); TextField tf1 = new TextField("",20); Button b1 = new Button("Go"); Panel p1 = new Panel(); p1.setBackground(Color.CYAN); p1.add(lb1); p1.add(tf1); p1.add(b1); CheckboxGroup group = new CheckboxGroup(); 73
  52. CCáácctthhàànnhhpphhầầnnAAWWTT  Panel Checkbox cb1 = new Checkbox("Java",group,true); Checkbox cb2 = new Checkbox("C++",group,false); Checkbox cb3 = new Checkbox("HTML",group,false); Checkbox cb4 = new Checkbox("ADA",group,false); Panel p2 = new Panel(); p2.setBackground(Color.BLUE); p2.add(cb1); p2.add(cb2); p2.add(cb3); p2.add(cb4); ComponentTestFrame frame = new ComponentTestFrame("PanelTest"); frame.add(p1); frame.add(p2); frame.setVisible(true); } public static void main(String[] args) { PanelTest test = new PanelTest(); } }
  53. CCáácctthhàànnhhpphhầầnnAAWWTT 74
  54. CCáácctthhàànnhhpphhầầnnAAWWTT  Ví dụ về Panel 75
  55. CCáácctthhàànnhhpphhầầnnAAWWTT  Dialog  Là cửa sổ có thanh tiêu đề, đường biên  Thường dùng để cho phép nhập dữ liệu  Một Dialog phải tồn tại trong một Frame hoặc Dialog khác  Một Dialog có thể là modal hoặc non-modal  Modal dialog  Non-modal dialog 76
  56. CCáácctthhàànnhhpphhầầnnAAWWTT  Dialog  Một số phương thức  Dialog(Dialog)  Dialog(Dialog, String)  Dialog(Dialog, String, boolean)  Dialog(Frame)  Dialog(Frame, String)  Dialog(Frame, String, boolean) 77
  57. CCáácctthhàànnhhpphhầầnnAAWWTT  Ví dụ Dialog import java.awt.*; import java.awt.event.*; public class DialogTest implements WindowListener { public DialogTest() { super(); ComponentTestFrame frame = new ComponentTestFrame("DialogTest"); Dialog d1 = new Dialog(frame,"Modal Dialog Test",true); d1.add(new Label("Modal Dialog Test")); d1.addWindowListener(this); Dialog d2 = new Dialog(frame,"NonModal Dialog Test",false); d2.addWindowListener(this); d2.add(new Label("NonModal Dialog Test")); frame.setLocation(100,100); 78
  58. CCáácctthhàànnhhpphhầầnnAAWWTT  Ví dụ Dialog frame.setVisible(true); d2.pack(); d2.setLocation(250,280); d2.setVisible(true); d1.pack(); d1.setLocation(220,170); d1.setVisible(true); } public void windowOpened(WindowEvent arg0) { } public void windowClosing(WindowEvent arg0) { ((Dialog)arg0.getSource()).setVisible(false); } public void windowClosed(WindowEvent arg0) { } 79
  59. CCáácctthhàànnhhpphhầầnnAAWWTT  Ví dụ Dialog public void windowIconified(WindowEvent arg0) { } public void windowDeiconified(WindowEvent arg0) { } public void windowActivated(WindowEvent arg0) { } public void windowDeactivated(WindowEvent arg0) { } public static void main(String[] args) { DialogTest test = new DialogTest(); } } 80
  60. CCáácctthhàànnhhpphhầầnnAAWWTT  Ví dụ Dialog 81