1 / 23

Session 2

Session 2. Module 3: Layout Managers Module 4: Swing Menu Components. Module 1 Review. Module 2 Review. Module 3: Layout Managers. Objectives. Package java.awt. Setting a Layout to a Container. container.setLayout ( LayoutObject );. container.setSize (width,height);

lori
Télécharger la présentation

Session 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session 2 Module 3: Layout ManagersModule 4: Swing Menu Components

  2. Module 1 Review Layouts Manager and Menus / Session2 / 2 of 23

  3. Module 2 Review Layouts Manager and Menus / Session2 / 3 of 23

  4. Module 3: Layout Managers • Objectives Package java.awt Layouts Manager and Menus / Session2 / 4 of 23

  5. Setting a Layout to a Container container.setLayout ( LayoutObject ); container.setSize (width,height); // or container.pack(); container.setVisible(true); Layouts Manager and Menus / Session2 / 5 of 23

  6. Setting a Layout to a Container in NetBeans Layouts Manager and Menus / Session2 / 6 of 23

  7. FlowLayout FlowLayout is default layout of Jpanel Layouts Manager and Menus / Session2 / 7 of 23

  8. BorderLayout BorderLayout is default layout of JFrame, JApplet container.add( component com, int position) frame.add(btnNorth, BorderLayout.NORTH); Layouts Manager and Menus / Session2 / 8 of 23

  9. GridLayout JPanel p= new JPanel(); GridLayout g= new GridLayout(2,2); p.setLayout(g); p.add(btn1); p.add(btn2); p.add(btn3); p.add(btn4); Layouts Manager and Menus / Session2 / 9 of 23

  10. CardLayout Panel_Main Panel_1 Panel_2 CardLayout • Advantage: • Allows several containers and their • associated components to share the • same space in the container • Disadvantages • Not visual appealing as a tabbed pane • Requires other components like • buttons or drop-down to plip through. Panel_3 Control: next() previous() first() last() show() Layouts Manager and Menus / Session2 / 10 of 23

  11. Module 3 Summary Layouts Manager and Menus / Session2 / 11 of 23

  12. Module 4: Swing Menu Components Objectives Layouts Manager and Menus / Session2 / 12 of 23

  13. JMenuBar JCheckBoxMenuItem JMenu JRadioButtonMenuItem icon JMenuItem JSeparator text mnemonic Menu Components Layouts Manager and Menus / Session2 / 13 of 23

  14. JMenuBar class // Create a JFrame JFrame f= new JFrame(“Menu demo”); // Create a JMenuBar JMenuBar mnuBar = new JMenuBar(); // set menu bar to the frame f.setJMenuBar(mnuBar); Layouts Manager and Menus / Session2 / 14 of 23

  15. JMenu class • Constructors: JMenu() JMenu(String label) // create a menu bar JMenuBar mnuBar= new JMenuBar(); // create a JMenu JMenu mnuFile= new JMenu(“File”); // add a menu to menu bar mnuBar.add(mnuFile); // add a separator to menu mnuFile.addSeparator(); // set mnemonic : Alt + F mnuFile.setMnemonic(KyEvent.VK_F); Layouts Manager and Menus / Session2 / 15 of 23

  16. JMenuItem class • Constructors: • JMenuItem() • JMenuItem(Action a) • JMenuItem(Icon icon) • JMenuItem(String text) • JMenuItem(String text, Icon icon) • JMenuItem(String text, int mnemonic) • Important methods • setEnable(boolean enable) • setMnemonic ( int mnemonic) • setAccelerator(KeyStroke keyStroke) • Add menu item to menu menuObject.add(menuItemObject) • Event handling • ActionEvent • ActionListener • public void actionPerformed(ActionEvent evt) Layouts Manager and Menus / Session2 / 16 of 23

  17. JCheckBoxMenuItem class • Constructors: • JCheckBoxMenuItem() • JCheckBoxMenuItem(Action a) • JCheckBoxMenuItem(String text) • JCheckBoxMenuItem(Icon icon) • JCheckBoxMenuItem(String text, Icon icon) • JCheckBoxMenuItem(String text, boolean b) • JCheckBoxMenuItem(String text, Icon icon, boolean b) • Important methods • boolean isSelected() • get/ setSelected (boolean) • get/setState(boolean) • Add menu item to menu menuObject.add(checkBoxMenuItemObject) • Event handling • ItemEvent • ItemListener • public void itemStateChanged(ItemEvent evt) Layouts Manager and Menus / Session2 / 17 of 23

  18. JRadioButtonMenuItem To control the selected state of a group of radio button menu items, use a ButtonGroup object • Constructors: • JRadioButtonMenuItem() • JRadioButtonMenuItem(Action a) • JRadioButtonMenuItem(String text) • JRadioButtonMenuItem(Icon icon) • JRadioButtonMenuItem(Icon icon, boolean selected) • JRadioButtonMenuItem(String text) • JRadioButtonMenuItem(String text, boolean selected) • JRadioButtonMenuItem(String text, Icon icon, boolean selected) • Important methods • boolean isSelected() • get/setSelected (boolean) • Add menu item to menu menuObject.add(radioMenuItemObject) • Event handling • ActionEvent • ActionListener • public void actionPerformed (ActionEvent evt) Layouts Manager and Menus / Session2 / 18 of 23

  19. JPopupMenu class Constructors: • JPopupMenu() • JPopupMenu(String label) Common methods: Layouts Manager and Menus / Session2 / 19 of 23

  20. JFileChooser Dialog box Constructors: JFileChooser() JFileChooser(String curDir) Layouts Manager and Menus / Session2 / 20 of 23

  21. JFileChooser Dialog box … • Controlling on File dialog box: • File getSelectedFile() • File getCurrentDirectory() • setFileFilter (FileFilter obj) • Create a Filter: public class ImageFilter extends javax.swing.filechooser.FileFilter { // override: boolean accept (File) // override: String getDescription() } Layouts Manager and Menus / Session2 / 21 of 23

  22. JToolBar class Constructors: JToolBar() JToolBar (int orientation) JToolBar (String title) JToolBar (String title, int orientation ) Dockable? Adding a button to toolbar Methods Layouts Manager and Menus / Session2 / 22 of 23

  23. Module 4 Summary Layouts Manager and Menus / Session2 / 23 of 23

More Related