Java Reference
In-Depth Information
4. Write a “skeleton” GUI program that implements the WindowListener inter-
face. Write code for each of the methods in Display 18.1 that simply prints out
a message identifying which event occurred. Print the message out in a text field.
Note that your program will not end when the close-window button is clicked (but
will instead simply send a message to the text field saying that the windowClosing
method has been invoked). Include a button labeled Exit that the user can click
to end the program.
5. Enhance the face drawing in Display 18.17 in the following ways: Add color so the
eyes are blue and the mouth is red. When the face winks, the line that represents
a closed eye is black not blue. Add a nose and a brown handlebar mustache. Add
buttons labeled " Smile " and " Frown ". When the " Frown " button is clicked, the
face shows a frown (upside down smile); when the "Smile" button is clicked, the
face shows a smile. When the user clicks the close-window button, a window pops
up to ask if the user is sure he or she wants to exit, as in Display 18.2.
6. Write a GUI program to sample different fonts. The user enters a line of text in a
text field. The user then selects a font from a font menu. Offer the three guaranteed
fonts and at least two other fonts. The user also selects any desired style modifiers
(bold and/or italic) from a style menu, and selects point size from a point size menu
that offers the following choices: 9, 10, 12, 14, 16, 24, and 32. There is also a
" Display " button. When the " Display " button is clicked, the text is displayed in
the font, style, and point size chosen.
7. The MouseListener interface allows you to retrieve mouse events. A program
implements this interface in a manner similar to the WindowListener interface.
For example, the following program creates a JFrame and outputs the X and Y
coordinates of any mouse clicks within the JFrame . The MouseListener interface
requires the implementing class to define the mouseClicked, mouseEntered,
mousePressed, mouseReleased, and mouseExited methods. In the example,
only the mouseClicked method has been completed.
VideoNote
Solution to
Programming
Project 18.7
import javax.swing.JFrame;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class MouseDemo extends JFrame implements MouseListener
{
public void mouseClicked (MouseEvent e)
{
System.out.println(e.getX() + " " + e.getY());
}
public void mouseEntered (MouseEvent e) {}
public void mousePressed (MouseEvent e) {}
public void mouseReleased (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
Search WWH ::




Custom Search