1 / 59

Chapter 14- More Swing, Better looking applications.

Chapter 14- More Swing, Better looking applications. Overview. Menus Icons (again) Scroll Panes. Borders Look and Feel More Layouts Inner Classes Review. Menus. The use of menus. Menus are basically collections of buttons.

helga
Download Presentation

Chapter 14- More Swing, Better looking applications.

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. Chapter 14- More Swing, Better looking applications.

  2. Overview • Menus • Icons (again) • Scroll Panes. • Borders • Look and Feel • More Layouts • Inner Classes • Review

  3. Menus

  4. The use of menus • Menus are basically collections of buttons. • Each button in the collection is in the same category (like all the entries in the File menu are usually related to files). • Java deals with menu selections the same way as it deals with buttons.

  5. Menu hierarchy • Create JMenuItems and register an action listener for them (just like JButtons). • Add JMenuItems to JMenus. • Add JMenus to other JMenus(to make menus within menus). • Add your finished JMenu to a JMenuBar. • Add JMenuBar to the JFrame.

  6. JMenuItem • Create it with some text to set the label of the menu item. Also sets the action command for the menu item. • Register an action listener (usually this) • Add the item to the JMenu with the add(JMenuItem) method of JMenu.

  7. JMenuItem example JMenu menu = new JMenu(“Mugglets”); JMenuItem mi = new JMenuItem(“Blar!”); mi.addActionListener(this); menu.add(mi);

  8. JMenu • Construct a JMenu also with a string to set the label of the button you push to bring up the contents of the menu (“File”). • JMenu inherits from JMenuItem, so you can actually add JMenus to other JMenus (nested menus). • Use the add method to add items to the JMenu. • When you are done, use the add method of JMenuBar to add the JMenu to the menu bar.

  9. JMenu example JMenu menu = new JMenu(“Mugglets”); JMenu overMenu= new JMenu(“Snubbles”); JMenuItem mi = new JMenuItem(“Blar!”); mi.addActionListener(this); menu.add(mi); overMenu.add(menu); JMenuBar theBar = new JMenuBar(); theBar.add(overMenu);

  10. JMenuBar • No string in its constructor, as there is no label to display for a menu bar. • Use add method to add JMenus to the menu bar. • When finished with JMenuBar, use setJMenuBar(JMenuBar) on the JFrame, or add the JMenuBar to the content pane of the JFrame.

  11. JMenuBar example. JMenu menu = new JMenu(“Mugglets”); JMenu overMenu= new JMenu(“Snubbles”); JMenuItem mi = new JMenuItem(“Blar!”); mi.addActionListener(this); menu.add(mi); overMenu.add(menu); JMenuBar theBar = new JMenuBar(); theBar.add(overMenu); setJMenuBar(theBar); //or getContentPane().add(theBar);

  12. Menu Review • What are the smallest items that we add to menus? • Can we add menus to menus? • What do we add menus to? • How do we get the whole menu system to display in a JFrame (2 ways)?

  13. Icons

  14. A picture is worth a thousand words. • Icons can make things more interesting to look at as well as easier to understand. • We can add icons to our Java GUIs. • We usually add them to labels and buttons and menu items.

  15. ImageIcon • We create icons by constructing ImageIcon objects. • When you construct the object, you pass it the name of an image file. • Most popular image formats can be read by Java (JPG, GIF, etc.).

  16. Adding Icons • We can make an ImageIcon the sole displayed item in a button, label, or menu item by passing an ImageIcon in the constructor of the object. • We can also use setIcon(ImageIcon). • We can also have both images and text.

  17. ImageIcon example. ImageIcon blar = new ImageIcon(“someImg.gif”); JButton howdy = new JButton(“Howdy”); howdy.setIcon(blar); // or JButton howdy = new JButton(blar); howdy.setText(“Howdy”); // or JButton howdy = new JButton(“Howdy”, blar);

  18. Icon review • What class do we use if we want to create icons? • What kinds of items can we add icons to? • What method do we use to add an icon to an already-existing object?

  19. Scroll Panes

  20. When the screen just isn’t big enough… • If our information is bigger than a particular JTextArea that we have, we can add scroll bars so that we can view all of the information. • This is done using a view port called JScrollPane. • A view port is basically a window through which you can see the information in the background.

  21. How to create a JScrollPane. • When we construct a JScrollPane, we want to pass the component that we want scrollbars on to it (usually some text area). • Once we have done that, we set the scroll bar policies and add it to a panel or frame.

  22. JScrollPane Example JTextArea someText = new JTextArea(20,40); JScrollPane viewWindow = newJScrollPane(someText); viewWindow.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); viewWindow.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); //or viewWindow.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_NEVER); getContentPane().add(viewWindow);

  23. Scroll Bar Policies • NEVER- No scroll bar is ever displayed in the indicated direction. • AS_NEEDED- Scroll bar only appears when what is being viewed becomes big enough to require scrolling. • ALWAYS- Scroll bar is always there. Whether it is needed or not.

  24. Scroll Bar Review • What class do we use to create scrollable text areas? • How do we set the scroll bar policies? To what do we set them too?

  25. Borders

  26. Borders • We can also set the borders of any JComponent (Just about anything we have been dealing with) to almost any kind of border we might want (and quite a few we don’t).

  27. Borders • First, import javax.swing.border.* • Then use the setBorder(Border) method on whatever component you want to put a border on. • You can specify lots of kinds of borders: EtchedBorders, EmptyBorders, BevelBorders, LineBorders, MatteBorders, SoftBevelBorders, or you can make your own.

  28. Border Examples JButton arg = new JButton(“Argh!”); arg.setBorder(new EtchedBorder(Color.yellow, Color.orange)); arg.setBorder(new LineBorder(Color.black, 5)); …

  29. Borders Review • What library do we have to import to be able to use borders? • What method of JComponent do we use to change the border of an object? • What are some examples of Border classes?

  30. Look And Feel

  31. Changing the look and feel of Java. • We can change the whole appearance of Java using pluggable “look and feel” designs. • The standard default one is called Metal. • There is another called Motif that looks more like a UNIX X-Windows system • There is yet another standard one called Windows which looks a lot like Windows. • You could even make your own if you were in to that kind of thing.

  32. Changing the Look and Feel. • You usually do it in the constructor of your JFrame. • You will need to catch a fair number of exceptions when you try to change the look and feel(or you can catch them all as Exception). • You will need to use some special statements seen in the example.

  33. Look and Feel Example try { UIManager.setLookAndFeel(SomeLookAndFeelClass); //like “javax.swing.plaf.metal.MetalLookAndFeel” //“com.sun.java.swing.plaf.motif.MotifLookAndFeel” //”com.sun.java.swing.plaf.windows.WindowsLookAndFeel” SwingUtilities.updateComponentTreeUI(this); } catch(Exception e) { … }

  34. Look and Feel Review • Just remember that you can change the look and feel of Java and that you usually do it in the constructor of your JFrame.

  35. More Layouts

  36. Layouts and more layouts • We mentioned before that there were some more layouts. • Now we will look at some more layouts in depth and see what they produce.

  37. BoxLayout • A horizontal BoxLayout is like a FlowLayout, while a vertical BoxLayout is somewhat like a GridLayout with 1 column. • When we construct a BoxLayout, we pass two things to it, the container you want to set the layout for, and the orientation of the Box. JPanel somePanel = new JPanel(); somePanel.setLayout(new BoxLayout(somePanel, BoxLayout.X_AXIS)); //or Y_AXIS.

  38. The power of BoxLayout • BoxLayout is not all that impressive until you start adding struts and glue. • Struts are commands to Java on how much space to put between components (rigid, no change). • Glues are requests to Java on how much space to put between components (they can be squished).

  39. Creating Struts (and glue). • Make them of type Component. • Use Box.createHorizontalStrut(int) or Box.createVerticalStrut(int) or Box.createHorizontalGlue() or Box.createVerticalGlue(). • Then just use the usual add method to put the strut or glue into the layout. • Only uses struts and glue in BoxLayout or Box(coming up next). They don’t work so well with other layout managers.

  40. Box- An easier way. • Notice that we used a few static methods to create the struts and glue off of the Box class. • We can use this Box class to automatically create a JPanel that has a BoxLayout. • Don’t have to use a constructor for this, but you can. Box aBox = Box.createHorizontalBox(); Box aBox = new Box(BoxLayout.X_AXIS);

  41. Box • After you have created a Box, you can then use it like you would any other JPanel. • You can of course add struts and glue to the Box.

  42. CardLayout. • This layout is quite different than the other layouts we have seen. • When we add things to a CardLayout panel, we don’t get multiple items in the same panel, we get multiple panels on top of one another. • We can call any of these panels up to the front at any time, but only the front one shows at any time.

  43. CardLayout details • Do not use an anonymous object to set the layout. You will need a CardLayout object later to show different cards. • When we add things to the panel, we first give it a label to identify by, and then the container we want to add to a card. • We show different cards by calling several methods off of the CardLayout object that we saved and giving them the Container we want to change.

  44. CardLayout example JPanel somePanel = new JPanel(); CardLayout aDeck = new CardLayout(); somePanel.setLayout(aDeck); JPanel panel = new JPanel(); JLabel aLabel = new JLabel(“Howdy”); panel.add(aLabel); somePanel.add(“aFirstCard”, panel); … aDeck.show(somePanel, “aFirstCard”);

  45. More CardLayout details • Initially, the first card you add is the one that is displayed. • We can move amongst the cards with other methods besides show: -first(Container) -next(Container) -prev(Container) -last(Container)

  46. Layout Review • What is a quicker way of doing BoxLayout in a panel? • What special pieces do we use in BoxLayout to space components apart? What is the difference between the pieces? • How does CardLayout work?

  47. Inner Classes

  48. Inner Classes • We have already seen a little bit of inner classes (for the usual way of closing windows, etc.). • We can also define an inner class by having a second class inside of the same file, this one private. • Inner classes have access to all the methods and instance variables of the outer classes.

  49. Another way to close windows • Instead of creating an inner class that inherits from the WindowAdapter, we could instead implement the WindowListener interface. • We can implement as many interfaces as we want. • If you implement the WindowListener, you need to implement all 7 functions of WindowListener.

  50. WindowListener methods- • windowOpened(WindowEvent) • windowClosing(WindowEvent) • windowClosed(WindowEvent) • windowIconified(WindowEvent) • windowDeiconified(WindowEvent) • windowActivated(WindowEvent) • windowDeactivated(WindowEvent)

More Related