![]() |
![]() |
![]() |
![]() |
![]() |
|
From this point forward, the methods of the MovieApplication class
access the meter and movie instance variables via getters and
setters so as to benefit from data abstraction, as explained in
Chapter 13:
import javax.swing.*;
import java.awt.event.*;
public class MovieApplication extends JFrame {
public static void main (String argv []) {
new MovieApplication("Movie Application");
}
// Declare instance variables:
private Meter meter;
private Movie movie;
// Define constructor
public MovieApplication(String title) {
super(title);
// Construct view instances:
setMeter(new Meter(0, 30));
// Construct model instances:
setMovie(new Movie (5, 5, 5, "On to Java"));
// Connect views visually
getContentPane().add("Center", getMeter());
// Connect window listener, size and show
addWindowListener(new LocalWindowListener());
setSize(300, 100);
show();
}
// Define getters and setters
public void setMeter (Meter m) {meter = m;}
public Meter getMeter() {return meter;}
public void setMovie (Movie m) {movie = m;}
public Movie getMovie() {return movie;}
// Define window adapter
private class LocalWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0); return;
}}}