Java Reference
In-Depth Information
Display 17.5
The Color Constants
Color.MAGENTA
Color.ORANGE
Color.PINK
Color.RED
Color.WHITE
Color.YELLOW
Color.BLACK
Color.BLUE
Color.CYAN
Color.DARK_GRAY
Color.GRAY
Color.GREEN
Color.LIGHT_GRAY
The class Color is in the java.awt package.
EXAMPLE: A GUI with a Label and Color
Display 17.6 shows a class for GUIs with a label and a background color. We have
already discussed the use of color for this window. The label is used to display the text
string "Close-window button works." The label is created as follows:
JLabel aLabel = new JLabel("Close-window button works.");
The label is added to the JFrame with the method add as shown in the following line
from Display 17.6 :
add(aLabel);
The GUI class ColoredWindow in Display 17.6 programs the close-window button
as follows:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
This way, when the user clicks the close-window button, the program ends. Note
that if the program has more than one window, as it does in Display 17.6, and the
user clicks the close-window button in any one window of the class ColoredWindow ,
then the entire program ends and all windows go away.
Note that we set the title of the JFrame by making it an argument to super rather
than an argument to setTitle . This is another common way to set the title of
a JFrame .
If you run the program DemoColoredWindow in Display 17.6 , then the two
windows will be placed one on top of the other. To see both windows, you need to
use your mouse to move the top window.
Setting the Title of a JFrame
The two most common ways to set the title of a JFrame are to use the method setTitle ,
as illustrated in Display 17.4, or to give the title as an argument to the base class constructor
super , as illustrated in Display 17.6.
 
 
Search WWH ::




Custom Search