chapter 14- more swing, better looking applications

59
Chapter 14- More Swing, Better looking applications.

Upload: helga

Post on 13-Jan-2016

40 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 14- More Swing, Better looking applications

Chapter 14- More Swing, Better looking applications.

Page 2: Chapter 14- More Swing, Better looking applications

Overview

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

Page 3: Chapter 14- More Swing, Better looking applications

Menus

Page 4: Chapter 14- More Swing, Better looking applications

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.

Page 5: Chapter 14- More Swing, Better looking applications

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.

Page 6: Chapter 14- More Swing, Better looking applications

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.

Page 7: Chapter 14- More Swing, Better looking applications

JMenuItem example

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

Page 8: Chapter 14- More Swing, Better looking applications

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.

Page 9: Chapter 14- More Swing, Better looking applications

JMenu exampleJMenu 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);

Page 10: Chapter 14- More Swing, Better looking applications

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.

Page 11: Chapter 14- More Swing, Better looking applications

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);

//orgetContentPane().add(theBar);

Page 12: Chapter 14- More Swing, Better looking applications

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)?

Page 13: Chapter 14- More Swing, Better looking applications

Icons

Page 14: Chapter 14- More Swing, Better looking applications

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.

Page 15: Chapter 14- More Swing, Better looking applications

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.).

Page 16: Chapter 14- More Swing, Better looking applications

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.

Page 17: Chapter 14- More Swing, Better looking applications

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);

Page 18: Chapter 14- More Swing, Better looking applications

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?

Page 19: Chapter 14- More Swing, Better looking applications

Scroll Panes

Page 20: Chapter 14- More Swing, Better looking applications

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.

Page 21: Chapter 14- More Swing, Better looking applications

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.

Page 22: Chapter 14- More Swing, Better looking applications

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);

//orviewWindow.setVerticalScrollBarPolicy(

JScrollPane.VERTICAL_SCROLLBAR_NEVER);

getContentPane().add(viewWindow);

Page 23: Chapter 14- More Swing, Better looking applications

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.

Page 24: Chapter 14- More Swing, Better looking applications

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?

Page 25: Chapter 14- More Swing, Better looking applications

Borders

Page 26: Chapter 14- More Swing, Better looking applications

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).

Page 27: Chapter 14- More Swing, Better looking applications

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.

Page 28: Chapter 14- More Swing, Better looking applications

Border Examples

JButton arg = new JButton(“Argh!”);arg.setBorder(new EtchedBorder(Color.yellow,

Color.orange));

arg.setBorder(new LineBorder(Color.black, 5));…

Page 29: Chapter 14- More Swing, Better looking applications

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?

Page 30: Chapter 14- More Swing, Better looking applications

Look And Feel

Page 31: Chapter 14- More Swing, Better looking applications

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.

Page 32: Chapter 14- More Swing, Better looking applications

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.

Page 33: Chapter 14- More Swing, Better looking applications

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){

…}

Page 34: Chapter 14- More Swing, Better looking applications

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.

Page 35: Chapter 14- More Swing, Better looking applications

More Layouts

Page 36: Chapter 14- More Swing, Better looking applications

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.

Page 37: Chapter 14- More Swing, Better looking applications

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.

Page 38: Chapter 14- More Swing, Better looking applications

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).

Page 39: Chapter 14- More Swing, Better looking applications

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.

Page 40: Chapter 14- More Swing, Better looking applications

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);

Page 41: Chapter 14- More Swing, Better looking applications

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.

Page 42: Chapter 14- More Swing, Better looking applications

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.

Page 43: Chapter 14- More Swing, Better looking applications

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.

Page 44: Chapter 14- More Swing, Better looking applications

CardLayout exampleJPanel 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”);

Page 45: Chapter 14- More Swing, Better looking applications

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)

Page 46: Chapter 14- More Swing, Better looking applications

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?

Page 47: Chapter 14- More Swing, Better looking applications

Inner Classes

Page 48: Chapter 14- More Swing, Better looking applications

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.

Page 49: Chapter 14- More Swing, Better looking applications

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.

Page 50: Chapter 14- More Swing, Better looking applications

WindowListener methods-

– windowOpened(WindowEvent)– windowClosing(WindowEvent)– windowClosed(WindowEvent)– windowIconified(WindowEvent)– windowDeiconified(WindowEvent)– windowActivated(WindowEvent)– windowDeactivated(WindowEvent)

Page 51: Chapter 14- More Swing, Better looking applications

Closing Windows

Another way to close a single JFrame is to call the dispose() method of the JFrame. This is useful in a multiple window program, so it doesn’t close all of Java.

You can reprogram the close button to act like any button you like, you will want to reset the close button action by using setDefaultCloseOperation(WindowConstansts.DO_NOTHING_ON_CLOSE) on the frame.

Page 52: Chapter 14- More Swing, Better looking applications

Visibility and updating the GUI

If you ever want to make a particular component disappear for a while, you can setVisible(false) on the component.

Whenever you make a change to the GUI and Java doesn’t automatically refresh the GUI for you, you can force it with the validate(), repaint(), or pack() methods. Currently you probably want to use validate().

Page 53: Chapter 14- More Swing, Better looking applications

Inner Class Review

How are inner classes useful? What is the new way to do inner classes that

we just learned? If we didn’t want to control the window

through a WindowAdapter class, how could we do it?

How can we close a single window? How do we cause components to disappear? How do we force a refresh of the GUI?

Page 54: Chapter 14- More Swing, Better looking applications

Review

Page 55: Chapter 14- More Swing, Better looking applications

Review

What are the smallest items that we add to menus?

What do we add menus to? How do we get the whole menu system

to display in a JFrame (2 ways)?

Page 56: Chapter 14- More Swing, Better looking applications

Review

What class do we use if we want to create icons?

What method do we use to add an icon to an already-existing object?

What class do we use to create scrollable text areas?

Page 57: Chapter 14- More Swing, Better looking applications

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?

Page 58: Chapter 14- More Swing, Better looking applications

Review

How does BoxLayout work? What is another way of getting a

BoxLayout panel? What components do we need to use to

make BoxLayouts useful? How does CardLayout work?

Page 59: Chapter 14- More Swing, Better looking applications

Review

How are inner classes useful? What is the new way to do inner classes

that we just learned? How can we close a single window? How do we cause components to

disappear? How do we force a refresh of the GUI?