Chương 6: Lập trình GUI (tt) ppsx

57 220 0
Chương 6: Lập trình GUI (tt) ppsx

Đ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

Chương 6: L p trình GUI (tt) GVLT: Tr n Anh Dũng N i dung Gi i thi u v Swing Nh ng ñ c ñi m c a Swing Các thành ph n GUI thông d ng c a Swing Menu H th ng menu Các lo i menu Các thành ph n c a menu Menubar MenuItems M t s l p đ h a thơng d ng Gi i thi u Swing (1) AWT (Abstract Windows Toolkit) Java ðư c g n v i platform xác đ nh Thích h p v i vi c phát tri n ng d ng GUI ñơn gi n Swing Java Không g n v i platform c ñ nh M nh, ña năng, linh ho t Gi i thi u Swing (2) Là m t gói n m thư vi n JFC (Java Foundation Classes), g m nhi u l p (classes) giao di n (interfaces) h tr m nh m cho vi c l p trình giao di n đ h a JDK JFC m t nhóm thư vi n ñư c thi t k ñ h tr l p trình viên t o ng d ng enterprise v i Java Swing ch m t thư vi n t o nên JFC javax.swing Nh ng ñ c ñi m c a Swing (1) C m quan (Look & Feels) Kh vi t nh ng c m quan (Look & Feels) cho m i thành ph n, th m chí thay đ i c m quan vào th i ñi m runtime Swing có kh th hi n nhi u L&F khác hi n t i h tr L&F bao g m Metal (m c ñ nh), Motif Windows Nh ng ñ c ñi m c a Swing (2) Swing s (MVC) d ng ki n trúc Model - View - Controller Swing có r t nhi u nh ng thành ph n m i Table, Tree, Slider, Progress Bar, Spinner, Internal frame Text Các thành ph n Swing có Tooltip đ t bên chúng B n có th tùy ch n s ki n bàn phím cho thành ph n, ñ nh nghĩa chúng ho t ñ ng th v i nh ng phím nóng cho AWT & Swing AWT v n ñư c h tr Java Các thành ph n thư vi n Swing không th thay t t c thành ph n thư vi n AWT Chúng ch thay th m t ph n c a AWT như: Label, Button, TextFeild, Panel Các l p tr giúp khác AWT như: Graphics, Color, Font, FontMetrics,… v n không thay ñ i Các thành ph n Swing s d ng mơ hình s lý s ki n c a AWT Sơ ñ phân c p javax.swing JComponent JCheckBoxMenuItem JMenuItem JButton AbstractButton JMenu JRadioButtonMenuItem JToggleButton JCheckBox JRadioButton JEditorPane JComponent JTextField JTextComponent JPasswordField J TextArea JLabel JList JTabbedPane JComboBox JMenuBar JPanel JOptionPane JScrollBar JScrollPane JFileChooser JPopupMenu JSeparator JSlider JRootPane JPane JProgressBar JToolBar JSplitPane JTable JTree JInternalFrame JToolTip JLayeredPane JTableHeader JColorChooser Swing Components Top-level container Menu Bar Visible Components Content pane 10 JSpinner JSpinner has an editor that displays the values and buttons on the right hand side to move through the values SpinnerModel is used to represent a sequence of values SpinnerModel model = new SpinnerDateModel(); SpinnerModel model = new SpinnerNumberModel(); SpinnerModel model = new SpinnerListModel(List values); SpinnerModel model = new SpinnerListModel(Object[] values); JSpinner spinner1 = new JSpinner(model); 43 JSlider Cho phép ngư i s d ng ch n giá tr nguyên m t vùng giá tr xác ñ nh JSlider(): Creates a horizontal slider with the range to 100 and an initial value of 50 44 JSlider M t s constructors: JSlider(int orientation): Creates a slider using the specified orientation with the range to 100 and an initial value of 50 JSlider(int min, int max): Creates a horizontal slider using the specified and max with an initial value equal to the average of the plus max JSlider(int min, int max, int value) JSlider(int orientation, int min, int max, int value): Creates a slider with the specified orientation and the specified minimum, maximum, and initial values 45 JSlider M t s methods: void setMajorTickSpacing(int n): This method sets the major tick spacing void setMaximum(int maximum) void setMinimum(int minimum) void setMinorTickSpacing(int n): This method sets the minor tick spacing void setOrientation(int orientation): Set the scrollbars orientation to either VERTICAL or HORIZONTAL … 46 ng d ng MDI JDesktopPane A container used to create a multiple-document interface You create JInternalFrame objects and add them to the JDesktopPane 47 L p Color Thi t l p màu cho thành ph n GUI b ng cách s dung l p java.awt.Color Các màu ñư c t o t màu b n red, green, blue g i h màu RGB (RGB model) Color c = new Color(r, g, b); Ví d : Color c = new Color(228, 100, 255); S d ng phương th c tương ng ñ thi t l p màu background foreground cho thành ph n 48 L p Font Thi t l p Font cho thành ph n Font myFont = Font(name, style, size); Ví d : Font font1 = new Font(“Arial", Font.BOLD, 16); Font font2 = new Font("Serif", Font.BOLD + Font.ITALIC, 12); 49 GraphicsEnvironment Ví d : Tìm t t c tên font ph d ng import java.awt.GraphicsEnvironment; public class testAllFonts { public static void main(String[] args) { GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontnames = e.getAvailableFontFamilyNames(); for (int i = 0; i < fontnames.length; i++) System.out.println(fontnames[i]); } } 50 L p FontMetrics 51 L p FontMetrics M t s phương th c thông d ng public int getAscent() public int getDescent() public int getLeading() public int getHeight() public int stringWidth(String str) … Ví d : Hi n th chu i “Welcome to Java” canh gi a frame, v i font kích thư c ñư c xác ñ nh trư c 52 L p Graphics (1) drawLine(x1, y1, x2, y2); drawRect(x, y, w, h); fillRect(x, y, w, h); drawRoundRect(x, y, w, h, aw, ah); fillRoundRect(x, y, w, h, aw, ah); 53 L p Graphics (2) drawOval(x, y, w, h); fillOval(x, y, w, h); drawArc(x, y, w, h, angle1, angle2); fillArc(x, y, w, h, angle1, angle2); 54 L p Graphics (3) V ña giác int[] x = {40, 70, 60, 45, 20}; int[] y = {20, 40, 80, 45, 60}; g.drawPolygon(x, y, x.length); g.fillPolygon(x, y, x.length); 55 Bài t p Tìm hi u thư vi n javax.swing Tìm hi u phương th c l p Graphics Xây d ng d ng d ng v đư ng cong hình sin Xây d ng ng d ng v ñ ng h 56 H i & ñáp 57 ... p (classes) giao di n (interfaces) h tr m nh m cho vi c l p trình giao di n đ h a JDK JFC m t nhóm thư vi n đư c thi t k đ h tr l p trình viên t o ng d ng enterprise v i Java Swing ch m t thư... mnuNew.setShortcut(CtrlN); // Ctrl + N mnuOpen.setShortcut(new MenuShortcut(KeyEvent.VK_O)); 29 Ví d Chương trình minh h a cách thi t l p menu cho d ng: ng MenuDemo Hot key? MenuHotkeyDemo 30 L p PopupMenu/JPopupMenu... Constructor PopupMenu()/JPopupMenu() PopupMenu(String label)/JPopupMenu(String label) 31 Ví d Chương trình minh h a cách thi t l p popup menu cho ng d ng: Popup Menu Jpopup Menu 32 JProgressBar

Ngày đăng: 27/07/2014, 19:20

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