Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 34 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
34
Dung lượng
107,24 KB
Nội dung
PHẦN - SWING MENUS Menu look Khoa CNTT – ĐH Nông Lâm TP HCM 2014 2/34 The Menu Component Hierarchy Khoa CNTT – ĐH Nông Lâm TP HCM 2014 3/34 The Menu Structure JMenuBar: mainMenu Jmenu: fileMenu Jmenu: editMenu Jmenu: …… JMenuItem: open JMenuItem: close JMenuItem: exit … Khoa CNTT – ĐH Nông Lâm TP HCM 2014 4/34 Creating Menus //in the constructor for a JFrame subclass: JMenuBar menuBar; JMenu menu, submenu; JMenuItem menuItem; JCheckBoxMenuItem cbMenuItem; JRadioButtonMenuItem rbMenuItem //Create the menu bar menuBar = new JMenuBar(); setJMenuBar(menuBar); Khoa CNTT – ĐH Nông Lâm TP HCM 2014 5/34 Creating Menus //Build the first menu menu = new JMenu(“File"); menu.setMnemonic(KeyEvent.VK_F); menuBar.add(menu); //a group of JMenuItems menuItem = new JMenuItem("New ", KeyEvent.VK_N); //used constructor instead //menuItem.setMnemonic(KeyEvent.VK_N); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.addActionListener(this); menu.add(menuItem); Khoa CNTT – ĐH Nông Lâm TP HCM 2014 6/34 Creating Menus //Menu item with Text and Icon menuItem = new JMenuItem("Open ", new ImageIcon("images/middle.gif")); menuItem.setMnemonic(KeyEvent.VK_O); menuItem.addActionListener(this); menu.add(menuItem); //Menu item has only a Icon menuItem = new JMenuItem(new ImageIcon("images/middle.gif")); menuItem.setMnemonic(KeyEvent.VK_D); menuItem.addActionListener(this); menu.add(menuItem); Khoa CNTT – ĐH Nông Lâm TP HCM 2014 7/34 Creating Menus //a group of radio button menu items menu.addSeparator(); ButtonGroup group = new ButtonGroup(); rbMenuItem = new JRadioButtonMenuItem("A radio button menu item 1“); rbMenuItem.setMnemonic(KeyEvent.VK_1); rbMenuItem.setSelected(true); group.add(rbMenuItem); rbMenuItem.addActionListener(this); menu.add(rbMenuItem); rbMenuItem = new JRadioButtonMenuItem("A radio button menu item 2“); rbMenuItem.setMnemonic(KeyEvent.VK_2); group.add(rbMenuItem); rbMenuItem.addActionListener(this); menu.add(rbMenuItem); Khoa CNTT – ĐH Nông Lâm TP HCM 2014 8/34 Creating Menus //a group of check box menu items menu.addSeparator(); cbMenuItem = new JCheckBoxMenuItem("A check box menu item 1"); cbMenuItem.setMnemonic(KeyEvent.VK_C); cbMenuItem.addItemListener(this); menu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("A check box menu item 2"); cbMenuItem.setMnemonic(KeyEvent.VK_A); cbMenuItem.addItemListener(this); menu.add(cbMenuItem); Khoa CNTT – ĐH Nông Lâm TP HCM 2014 9/34 Creating Menus //a submenu menu.addSeparator(); submenu = new JMenu("A submenu"); submenu.setMnemonic(KeyEvent.VK_S); menuItem = new JMenuItem("An item in the submenu"); menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_2, ActionEvent.ALT_MASK)); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Another item in the submenu"); menuItem.addActionListener(this); submenu.add(menuItem); menu.add(submenu); Khoa CNTT – ĐH Nông Lâm TP HCM 2014 10/34 The Menu API : javax.swing.JMenuBar public JMenuBar() Creates a new menu bar public JMenu add(JMenu c) Appends the specified menu to the end of the menu bar Parameters:c - the JMenu component to add Returns:the menu component public JMenu add(JMenu c) Appends the specified menu to the end of the menu bar Parameters:c - the JMenu component to add Returns:the menu component public int getMenuCount() Returns the number of items in the menu bar Returns:the number of items in the menu bar Khoa CNTT – ĐH Nông Lâm TP HCM 2014 20/34 The Menu API : javax.swing.JMenu public JMenu(String s) Constructs a new JMenu with the supplied string as its text Parameters:s - the text for the menu label public JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied Parameters:a - an Action public JMenuItem add(JMenuItem menuItem) Appends a menu item to the end of this menu Returns the menu item added Parameters:menuItem - the JMenuitem to be added Returns:the JMenuItem added public Component add(Component c, int index) Adds the specified component to this container at the given position If index equals -1, the component will be appended to the end Khoa CNTT – ĐH Nông Lâm TP HCM 2014 21/34 The Menu API : javax.swing.JMenu public JMenuItem add(String s) Creates a new menu item with the specified text and appends it to the end of this menu Parameters:s - the string for the menu item to be added public JMenuItem add(Action a) Creates a new menu item attached to the specified Action object and appends it to the end of this menu Parameters:a - the Action for the menu item to be added public void addSeparator() Appends a new separator to the end of the menu public void insert(String s, int pos) Inserts a new menu item with the specified text at a given position Parameters:s - the text for the menu item to addpos - an integer specifying the position at which to add the new menu item Khoa CNTT – ĐH Nông Lâm TP HCM 2014 22/34 The Menu API : javax.swing.JMenu public JMenuItem insert(JMenuItem mi,int pos) Inserts the specified JMenuitem at a given position Parameters:mi - the JMenuitem to addpos - an integer specifying the position at which to add the new Jmenuitem public JMenuItem insert(Action a, int pos) Inserts a new menu item attached to the specified Action object at a given position Parameters:a - the Action object for the menu item to addpos - an integer specifying the position at which to add the new menu item public JMenuItem getItem(int pos) Returns the JMenuItem at the specified position If the component at pos is not a menu item, null is returned Parameters:pos - an integer specifying the position Returns:the menu item at the specified position; or null if the item as the specified position is not a menu item Khoa CNTT – ĐH Nông Lâm TP HCM 2014 23/34 The Menu API : javax.swing.JMenu public void remove(int pos) Removes the menu item at the specified index from this menu Parameters:pos - the position of the item to be removed public void removeAll() Removes all menu items from this menu public void addMenuListener(MenuListener l) Adds a listener for menu events Khoa CNTT – ĐH Nông Lâm TP HCM 2014 24/34 The Menu API : javax.swing.JMenuItem public JMenuItem(Icon icon) Creates a JMenuItem with the specified icon Parameters:icon - the icon of the JMenuItem public JMenuItem(String text) Creates a JMenuItem with the specified text Parameters:text - the text of the JMenuItem public JMenuItem(Action a) Creates a menu item whose properties are taken from the specified Action Parameters:a - the action of the JMenuItem public JMenuItem(String text, Icon icon) Creates a JMenuItem with the specified text and icon Parameters:text - the text of the JMenuItem icon - the icon of the JMenuItem Khoa CNTT – ĐH Nông Lâm TP HCM 2014 25/34 The Menu API : javax.swing.JMenuItem public JMenuItem(String text, int mnemonic) Creates a JMenuItem with the specified text and keyboard mnemonic Parameters:text - the text of the JMenuItem mnemonic - the keyboard mnemonic for the JMenuItem public void setEnabled(boolean b) Enables or disables the menu item public void setAccelerator(KeyStroke keyStroke) Sets the key combination which invokes the menu item's action listeners without navigating the menu hierarchy Note that when the keyboard accelerator is typed, it will work whether or not the menu is currently displayed Parameters:keyStroke - the KeyStroke which will serve as an accelerator Khoa CNTT – ĐH Nông Lâm TP HCM 2014 26/34 The Menu API : javax.swing.JFrame void setJMenuBar(JMenuBar menubar) sets the menu bar for this frame Khoa CNTT – ĐH Nông Lâm TP HCM 2014 27/34 javax.swing.JCheckBoxMenuItem public JCheckBoxMenuItem(Icon icon) Creates an initially unselected check box menu item with an icon Parameters:icon - the icon of the CheckBoxMenuItem public JCheckBoxMenuItem(String text) Creates an initially unselected check box menu item with text Parameters:text - the text of the CheckBoxMenuItem public JCheckBoxMenuItem(Action a) Creates a menu item whose properties are taken from the Action supplied public JCheckBoxMenuItem(String text, Icon icon) Creates an initially unselected check box menu item with the specified text and icon Parameters:text - the text of the CheckBoxMenuItem icon - the icon of the CheckBoxMenuItem Khoa CNTT – ĐH Nông Lâm TP HCM 2014 28/34 javax.swing.JCheckBoxMenuItem public JCheckBoxMenuItem(String text, boolean b) Creates a check box menu item with the specified text and selection state Parameters:text - the text of the check box menu item b - the selected state of the check box menu item public JCheckBoxMenuItem(String text, Icon icon, boolean b) Creates a check box menu item with the specified text, icon, and selection state Parameters:text - the text of the check box menu item icon - the icon of the check box menu item b- the selected state of the check box Khoa CNTT – ĐH Nông Lâm TP HCM 2014 menu item 29/34 javax.swing.JCheckBoxMenuItem public boolean getState() Returns the selected-state of the item This method exists for AWT compatibility only New code should use isSelected() instead Returns:true if the item is selected public void setState(boolean b) Sets the selected-state of the item This method exists for AWT compatibility only New code should use setSelected() instead Parameters:b - a boolean value indicating the item's selected-state, where true=selected Khoa CNTT – ĐH Nông Lâm TP HCM 2014 30/34 ... menuBar; JMenu menu, submenu; JMenuItem menuItem; JCheckBoxMenuItem cbMenuItem; JRadioButtonMenuItem rbMenuItem //Create the menu bar menuBar = new JMenuBar(); setJMenuBar(menuBar); Khoa CNTT –... Lâm TP HCM 2014 5/ 34 Creating Menus //Build the first menu menu = new JMenu(“File"); menu. setMnemonic(KeyEvent.VK_F); menuBar.add (menu) ; //a group of JMenuItems menuItem = new JMenuItem("New... ActionEvent.ALT_MASK)); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Another item in the submenu"); menuItem.addActionListener(this); submenu.add(menuItem); menu. add(submenu);