Chapter 17


Exercise 1 Write a program that creates a frame with a File menu. The menu should have two items, Save... and Exit. When Save... is selected, the code should display a file dialog box, configured for saving a file. When the user has specified a file via the dialog box, your code should output the name of the file. All the information you need is on the API page for java.awt.FileDialog.

Solution 1 Here's the code:

import java.awt.*; import java.awt.event.*; class SaverFrame extends Frame implements ActionListener {   private MenuItem  saveMI, exitMI;   public SaverFrame()   {     // Build menu.     MenuBar mbar = new MenuBar();     Menu fileMenu = new Menu("File");     saveMI = new MenuItem("Save...");     saveMI.addActionListener(this);     fileMenu.add(saveMI);     exitMI = new MenuItem("Exit");     exitMI.addActionListener(this);     fileMenu.add(exitMI);     mbar.add(fileMenu);     setMenuBar(mbar);     setSize(300, 150);   }   public void actionPerformed(ActionEvent e)   {     if (e.getSource() == exitMI)       System.exit(0);     FileDialog dia = new FileDialog(this, "Save Your Work",                                      FileDialog.SAVE);     dia.setVisible(true);     String fileName = dia.getFile();     if (fileName == null)       System.out.println("You canceled the dialog.");     else       System.out.println("You chose file " + fileName + "                           in " + dia.getDirectory());   }   public static void main(String[] args)   {     (new SaverFrame()).setVisible(true);   } }

The following illustration shows the file dialog, configured for saving.

click to expand

Exercise 2 The FileDialog class has a setDirectory() method that controls which directory the dialog box will display. Look up the method description in the API to become familiar with how it works. Modify the final project code so that when the file dialog box appears, it displays one of the directories on your computer where you have stored some of your own Java source code. This will make it easier to display your own work.

Solution 2 Let's say you want the dialog to display the directory C:\MyCode\Ch7_Exercises. In actionPerformed(), change the code that constructs the file dialog, which in its original form looks like this:

public void actionPerformed(ActionEvent e) {   if (e.getSource() == openMI)   {     if (dialog == null)       dialog = new FileDialog(this, "Source File",                               FileDialog.LOAD);     dialog.setVisible(true);  // Modal  …

Add the setDirectory() call immediately after the dialog is constructed, before it is made visible:

public void actionPerformed(ActionEvent e) {   if (e.getSource() == openMI)   {     if (dialog == null)     {       dialog = new FileDialog(this, "Source File",                               FileDialog.LOAD);       dialog.setdirectory("C:\\MyCode\\Ch7_Exercises");     dialog.setVisible(true);  // Modal  …
Tip

Remember that in Java literal strings, single backslashes are escape characters that have special significance. That's why the argument to the setDirectory() call is C:\\MyCode\\Ch7_Exercises and not C:\MyCode\Ch7_Exercises.

Exercise 3 Write an application that displays a canvas subclass in a frame, at Center. The frame does not contain any other components.

Use the following code as the paint() method for the canvas subclass:

1. public void paint(Graphics G) 2. { 3.   g.setFont(new Font("Serif", Font.PLAIN, 24)); 4.   g.setColor(Color.blue); 5.   g.drawString("Look at this!", 0, 0); 6. }

Run the program. Do you see what you expected to see? How do you explain the results?

Now change line 5 to this:

g.drawString("A bluejay in a quagmire", 0, 0);

Now do you see what you expected to see? Again, how do you explain the results?

Solution 3 Here's the code:

import java.awt.*; class TextCanvas extends Canvas {   public void paint(Graphics g)   {     g.setFont(new Font("Serif", Font.PLAIN, 24));     g.setColor(Color.blue);     g.drawString("A bluejay in a quagmire", 0, 0);   }   public static void main(String[] args)   {     Frame fr = new Frame();     TextCanvas tc = new TextCanvas();     fr.add(tc, "Center");     fr.setSize(250, 250);     fr.setVisible(true);   } }

The y-coordinate argument of the drawString() method of class Graphics specifies the vertical position of the baseline of the text. If the baseline is 0, you will only see those parts of the text that descend below the baseline. In the string "Look at this!", there are no descenders. In "A jay in a quagmire", there is one occurrence of each character that descends: j, y, q, and g. The following illustration shows the GUI with the misplaced baseline. You can see the bottom portions of those letters, hanging down from the top of the canvas.

Exercise 4 The FancySrcCanvas class has an array of Java keywords. In that array, throws comes before throw. Otherwise, the list is alphabetical. Why does throws comes before throw?

Solution 4 The code that looks for keywords checks every position in every source line to see if it begins with a keyword. If it finds a match, it overpaints the keyword in the appropriate color. If throw came before throws, consider what would happed to the following line:

void printPaycheck(Employee emp) throws IOException

The code would overpaint throw, but the s would remain black.

Exercise 5 There are several situations in which the project code would improperly draw text in the keyword color. How many of these situations can you name?

Solution 5 The keyword-finding code ignored line comments—that is, comments beginning with a double slash. But it does nothing about comments that begin with slash-star (/*) and end with star-slash (/*). Any keyword that appeared in such a comment would be overpainted in the keyword color. The following line would look especially strange:

/* Let's go forward despite stiff competition. */

The "for" in "forward" and the "if" in "stiff" would appear in the keyword color.

Keywords might coincidentally appear in literal strings, for example:

System.out.println("while I was dreaming …");

Lastly, keywords might be embedded in the names of classes, variables, or methods:

class Republic extends Country { … }

Exercise 6 How would you modify the project code so that null, true, and false are not rendered in the keyword color?

Solution 6 Delete them from the keywords array in FancySrcCanvas.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net