Java Reference
In-Depth Information
The AdapterApplet program here is essentially the same as the Anon-
ListenerApplet above but with a MouseAdapter :
...Inthe AdapterJApplet class ...
Container content - pane = getContentPane ();
// Create an instance of JPanel.
JPanel panel = new JPanel ();
// Use an anonymous MouseAdapter object for the panel
panel.addMouseListener (
new MouseAdapter () {
public void mouseEntered (MouseEvent e) {
System.out.println (e.toString ());
}
}
);
// Add the panel to the content pane.
content - pane.add (panel);
...
This saves writing a lot of code but more importantly makes the code easier to
read and understand. However, be careful that you properly spell the method that
youoverride in the adapter. Otherwise, you are just adding another method to the
subclass. It can be difficult to track down this bug in your program.
7.6 Frames and menus
The application programs that we have demonstrated so far in this course send
their output to the console. We can instead create a window frame and build a
graphical user interface in the window using the components and GUI techniques
discussed thus far with regard to applets. As with other programs, we can also
include a menu bar with drop-down menus to provide various options for the user
such as the common File menu on the left-most position holding items such as
Open file , Save file , and Exit .
An application and an applet can also open other window frames. For example,
a program may need for the user to select from a bigger set of options and settings
than a menu can provide. So it brings up a new frame with a graphical interface
for the user to make the desired settings.
In the follow sections we first discuss how to add a menu bar to an applet.
Then we look at how to open a frame from within an application or an applet.
We then discuss how to create a GUI program that can run either as an applet or
in a standalone application window.
 
Search WWH ::




Custom Search