intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

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

Chia sẻ: Vo Minh Tu | Ngày: | Loại File: PDF | Số trang:57

144
lượt xem
23
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Là một gói nằm trong thư viện JFC (Java Foundation Classes), gồm nhiều lớp (classes) và giao diện (interfaces) hỗ trợ mạnh mẽ cho việc lập trình giao diện đồ họa trên JDK. JFC là một nhóm các thư viện được thiết kế và hỗ trợ lập trình viên tạo ra các ứng dụng enterprise với java.

Chủ đề:
Lưu

Nội dung Text: Chương 6: Lập trình GUI (tt)

  1. Chương 6: L p trình GUI (tt) GVLT: Tr n Anh Dũng 1
  2. 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 2
  3. Gi i thi u Swing (1) AWT (Abstract Windows Toolkit) Java 1 ðư c g n v i platform xác ñ nh Thích h p v i vi c phát tri n các ng d ng GUI ñơn gi n. Swing Java 2 Không g n v i platform c ñ nh M nh, ña năng, linh ho t 3
  4. Gi i thi u Swing (2) Là m t gói n m trong thư vi n JFC (Java Foundation Classes), g m nhi u l p (classes) và giao di n (interfaces) h tr m nh m cho vi c l p trình giao di n ñ h a trên JDK. JFC là m t nhóm các thư vi n ñư c thi t k ñ h tr l p trình viên t o ra các ng d ng enterprise v i Java. Swing ch là m t trong các thư vi n t o nên JFC. javax.swing 4
  5. Nh ng ñ c ñi m c a Swing (1) C m quan (Look & Feels) Kh năng 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 năng th hi n nhi u L&F khác nhau và hi n t i h tr các L&F bao g m Metal (m c ñ nh), Motif và Windows. 5
  6. Nh ng ñ c ñi m c a Swing (2) Swing s d ng ki n trúc Model - View - Controller (MVC). Swing có r t nhi u nh ng thành ph n m i Table, Tree, Slider, Progress Bar, Spinner, Internal frame và Text Các thành ph n Swing có các Tooltip ñ t bên trên chúng. B n có th tùy ch n các s ki n bàn phím cho các thành ph n, ñ nh nghĩa chúng ho t ñ ng như th nào v i nh ng phím nóng ñã cho. 6
  7. AWT & Swing AWT v n ñư c h tr trong Java 2 Các thành ph n trong thư vi n Swing không th thay t t c các thành ph n trong 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 trong 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. 7
  8. Sơ ñ phân c p javax.swing 8
  9. JComponent JCheckBoxMenuItem JMenuItem JMenu JButton JRadioButtonMenuItem AbstractButton JToggleButton JCheckBox JRadioButton JEditorPane JComponent JPasswordField JTextField JTextComponent J TextArea JLabel JList JComboBox JMenuBar JPanel JOptionPane JScrollBar JScrollPane JTabbedPane JFileChooser JPopupMenu JSeparator JSlider JPane JRootPane JProgressBar JToolBar JSplitPane JTable JTree JInternalFrame JToolTip JLayeredPane JColorChooser JTableHeader 9
  10. Swing Components Top-level Menu Bar container Visible Components Content pane 10
  11. JComponent M t s thành ph n GUI thư ng dùng: JFrame, JPanel, JScrollPane Jbutton, JLabel JTextArea Jcheckbox JRadioButton JList JComboBox ... 11
  12. JLabel JLabel JLabel (Icon img): Only Icon will be used for label. JLabel (String str): Only text will be used for label. JLabel (String str, Icon img, int align): Label will have both text and icon. Alignment is specified by the align argument and can be LEFT, RIGHT, CENTER, LEADING or TRAILING. These are constants and are defined in SwingConstant interface. l.setToolTipText(“ToolTip"); l.setDisplayedMnemonic('e'); 12
  13. JButton JButton JButton(), JButton(Icon icon) JButton(String text) JButton(String text, Icon icon) b1.setMnemonic(KeyEvent.VK_F); 13
  14. JTextComponent JTextComponent is the root class of all Swing text components. JTextField JTextArea JPasswordField JPasswordField p = new JPasswordField(“123"); p.setEchoChar('a'); 14
  15. JCheckBox JCheckBox class has the following constructors: JCheckBox() JCheckBox(Icon icon) JCheckBox(Icon icon, boolean selected) JCheckBox(String text) JCheckBox(String text, boolean selected) JCheckBox(String text, Icon icon) JCheckBox(String text, Icon icon, boolean selected) 15
  16. JRadioButton JRadioButton object can be created by using: JRadioButton() JRadioButton(Icon icon) JRadioButton(Icon, boolean selected) JRadioButton(String text) JRadioButton(String text, boolean selected) JRadioButton(String text, Icon icon) JRadioButton(String text, Icon icon, boolean selected) 16
  17. JList JFrame f = new JFrame("My frame"); String[ ] label = {"ASM","Pascal","C\\C++","VB","Java"}; JList l = new JList(label); l.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); ScrollPane s = new ScrollPane(); s.add(l); f.getContentPane().add(s); f.setSize(100,100); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); 17
  18. JComboBox JFrame f = new JFrame("My frame"); JComboBox com = new JComboBox(); com.addItem("Item 1"); com.addItem("Item 2"); com.addItem("Item 3"); f.getContentPane().add(com); f.getContentPane().setLayout(new FlowLayout()); f.setSize(100,100); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); 18
  19. JTabbedPane JFrame f = new JFrame("My frame"); String[ ] label={"ASM","Pascal","C\\C++","VB","Java"}; JButton[ ] b = new JButton[label.length]; JTabbedPane p = new JTabbedPane(); for(int i=0;i
  20. JTable JFrame f = new JFrame("My frame"); String[ ][ ] dat={{"1","JCreator","Xinox","Beginer"}, {"2","jGRASP","Auburn","Medium"}, {"3","NetBeans","Sun Microsystems","Expert"}, {"4","Gel","GExperts","Beginer"}, {"5","Eclipse","Eclipse","Expert"}, {"6","JBuilder","Borland","Expert"}}; String[ ] columnName = {"ID","Name","Company","Rank"}; JTable t = new JTable(dat,columnName); JScrollPane s = new JScrollPane(t); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(s); f.setSize(500,150); f.setVisible(true); 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2