Chapter 11

Exercise 11.1

The placement of the text is always centred vertically.

Exercise 11.2

The parameter is used as the title for the frame.

Exercise 11.3

The code to create a button:

   JButton button = new JButton("I am a button. I can display some text. You can also click me.");
contentPane.add(button);

Exercise 11.4

The code to create another button:

   JButton anotherButton = new JButton("I am also a button.");
contentPane.add(anotherButton);

Only the last button is visible. It looks like it replaces the first button that was added.

Exercise 11.5

Nothing happens when selecting a menu item.

Exercise 11.6

The method to create the menubar now looks like this:

    private void makeMenuBar() 
{
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu fileMenu = new JMenu("File");
menubar.add(fileMenu);
JMenuItem openItem = new JMenuItem("Open");
fileMenu.add(openItem);
JMenuItem quitItem = new JMenuItem("Quit");
fileMenu.add(quitItem);
JMenu helpMenu = new JMenu("Help");
menubar.add(helpMenu);
JMenuItem about = new JMenuItem("About ImageViewer");
helpMenu.add(about);
}

Exercise 11.15

When resizing after an image is loaded it does not resize the image, but only the frame.

Resizing and then opening an image does not have any effect because the loading an image resizes the frame to fit the new image.

Exercise 11.16

The panel now only shows the label with the version number. It does not show any image.

Exercise 11.17

The calculator project is using BorderLayout and GridLayout.

Exercise 11.18

The BlueJ editor window is made using BorderLayout and BoxLayout. BorderLayout is used for the main panel, the toolbar is using BoxLayout, and the status panel is using BorderLayout.

Exercise 11.19

The Use Library Class dialog is using BorderLayout and FlowLayout.

Exercise 11.22

Solutions to exercises which contains programming can be found in the projects on the CD (imageviewer1-0, imageviewer2-0...)

Exercise 11.23

The frame.repaint() method updates the display of the image after we have modified the contents of the image.

Exercise 11.25

It displays the following message in the status label: No image loaded.

Exercise 11.26

The darker method get the width and height of the image, and then goes through each pixel and changes the colour of the pixels, applying the darker() method belonging to the class OFImage.

Exercise 11.32

static void showMessageDialog(Component parentComponent, Object message)
Brings up an information-message dialog titled "Message".

static void showMessageDialog(Component parentComponent, Object message, String title, int messageType)
Brings up a dialog that displays a message using a default icon determined by the messageType parameter.

static void showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)
Brings up a dialog displaying a message, specifying all parameters.

We should use the second methods as this allows us to specify a proper title for the dialog.

Exercise 11.34

The documentation for the JTextArea: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTextField.html

An ActionListener is notified when the Enter key is pressed.

The user can be prevented from typing in the text field via the method setEditable(boolean b).

Exercise 11.50

Creating a GUI with JFC/Swing

Exercise 11.51

BorderLayout
BoxLayout
CardLayout
FlowLayout
GridBagLayout
GridLayout
SpringLayout
OverlayLayout

Exercise 11.52

See: How to Use Sliders

Exercise 11.53

See: How to Use Tabbed Panes

Exercise 11.54

See: JSpinner documentation

Exercise 11.55

See: ProgressBarDemo.java

Exercise 11.56

See: ProgressBarDemo.java

Exercise 11.68-11.73

Download: soundplayer.zip

Exercise 11.74

Download: zuul-with-images.zip