// VariablesDemo2.java
// Simple GUI with a panel demonstrating the paint method
// and the difference between static and instance variables.
//
// Intended to be used with VariablesDemo2Panel.java
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JTextField;
/**
* Window with a custom panel that keeps
* counts of (1) number of objects of its
* class and (2) number of times the panel's
* paint method has been called.
*
* @author D. Nixon
*/
public class VariablesDemo2 extends JFrame
{
private JTextField topField;
private JTextField bottomField;
/**
* Creates this window at the specified
* location on the screen, without yet
* making it appear.
*
* @param pixelsFromLeft distance between left side
* of this window and left side of screen
* @param pixelsFromTop distance between top of this
* window and top of screen
*/
public VariablesDemo2(int pixelsFromLeft,
int pixelsFromTop)
{
// Size of this window in pixels:
setSize(330, 250);
// Set this window's distance from
// left and top of screen:
setLocation(pixelsFromLeft, pixelsFromTop);
// Prepare to add text fields and panel:
Container contentPane = getContentPane();
// Create and add text field at top:
topField = new JTextField();
topField.setEditable(false);
contentPane.add(topField, BorderLayout.NORTH);
// Create and add text field at bottom:
bottomField = new JTextField();
bottomField.setEditable(false);
contentPane.add(bottomField, BorderLayout.SOUTH);
// Create and add our custom panel:
VariablesDemo2Panel panel = new VariablesDemo2Panel(this);
contentPane.add(panel, BorderLayout.CENTER);
// Enable program to quit when window closes:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // constructor
/**
* Displays a string in this window's top text field.
*
* @param text the string to be displayed.
*/
public void setTopText(String text)
{
topField.setText(text);
} // method setTopText
/**
* Displays a string in this window's bottom text field.
*
* @param text the string to be displayed.
*/
public void setBottomText(String text)
{
bottomField.setText(text);
} // method setBottomText
/**
* Main method
*
* @param args command-line arguments - none needed
*/
public static void main(String[] args)
{
// Create windows without yet making them
// visible (and thus not yet painting their
// panels):
VariablesDemo2 window1 = new VariablesDemo2(50, 50);
VariablesDemo2 window2 = new VariablesDemo2(350, 250);
VariablesDemo2 window3 = new VariablesDemo2(650, 450);
// Make windows visible, thereby also
// painting their panels for the first time:
window1.setVisible(true);
window2.setVisible(true);
window3.setVisible(true);
} // method main
} // class VariablesDemo2