|
|
|
1000 Java Tips ebook
|
|
 

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!. |
|
The Java Lesson 27: The Component class
|
JavaFAQ Home » Java Lessons by Jon Huhtala

Java Lesson 27 by
Jon Huhtala: The Component2> class
The Component class
-
Is abstract so it can never be directly instantiated
-
Has been extensively subclassed
to be the root of all non-menu related AWT components. Each subclass of Component inherits the same set
of features and behaves in much the same way. Commonly used subclasses are:
| Button |
| Canvas |
| Checkbox |
| Choice |
| Container |
| Label |
| List |
| Scrollbar |
| TextComponent |
Controlling position and
size
-
Component objects always occupy a rectangle on
the screen
-
Component position is always relative to the
containing object (not to the overall screen) and is measured in pixels from
the top-left corner. The top-left pixel has x and y coordinates of (0, 0) with
x increasing to the right and y increasing downward.
-
Some commonly used methods related to component
position and size are:
|
Method |
Usage |
|
getBounds() |
Gets the current size and location of the
component |
|
getHeight() |
Gets the current height of the
component |
|
getLocation() |
Gets the current location of the
component |
|
getSize() |
Gets the current size of the
component |
|
getWidth() |
Gets the current width of the
component |
|
getX() |
Gets the current x coordinate of the
component's origin |
|
getY() |
Gets the current y coordinate of the
component's origin |
|
setBounds() |
Moves and resizes the component |
|
setLocation() |
Moves the component |
|
setSize() |
Resizes the
component |
Notes:
-
Many of these methods are
overloaded to accept a range of parameter types and values.
-
If a layout manager is
controlling the container, these methods may be ineffective. Layout managers
will be presented later.
Controlling appearance
-
Each component can have its own background
color, foreground (text) color, and font. If not specified, these settings
will be the same as the component's container. If the container settings are
not specified, the JVM will provide default settings.
-
Some commonly used methods related to component
appearance are:
|
Method |
Usage |
|
getBackground() |
Gets the current background color of the
component |
|
getFont() |
Gets the current font of the
component |
|
getForeground() |
Gets the current foreground color of the
component |
|
setBackground() |
Sets the background color of the
component |
|
setFont() |
Sets the font of the component |
|
setForeground() |
Sets the foreground color of the
component |
Controlling user access and
component visibility
-
Under program control, each component can be
enabled, disabled, hidden, or visible
-
Some commonly used methods related to user
access and component visibility are:
|
Method |
Usage |
|
isEnabled() |
Determines whether the component is
enabled |
|
isVisible() |
Determines whether the component is
visible |
|
requestFocus() |
Requests that this be the active
component |
|
setEnabled() |
Enables or disables the
component |
|
setVisible() |
Shows or hides the
component |
Rendering (drawing) the
component
-
Each component is automatically rendered
(drawn) as needed by the JVM in response to window minimizing, maximizing,
overlaying, etc.
-
Under program control, the following methods
can be used to modify and render the component:
|
Method |
Usage |
|
getGraphics() |
Gets a graphics context for modifying the
component |
|
repaint() |
Repaints (renders) the
component |
Note: Drawing with a Graphics object will be
presented later.
A sample program
The following is the
source code of a small Java program to create and display a window having a two
buttons of different colors:
import
java.awt.*; import java.awt.event.*;
public class App extends Frame
implements WindowListener, ActionListener {
Button
stop; Button go;
public static void main(String[]
args) { App myWindow = new App("Stop and
go"); myWindow.setSize(200,150);
myWindow.setBackground(Color.black);
myWindow.setVisible(true); }
public App(String title)
{ super(title); setLayout(new
FlowLayout());
addWindowListener(this); stop = new
Button("STOP");
stop.setBackground(Color.red);
stop.setForeground(Color.white); stop.setFont(new
Font("SanSerif", Font.BOLD, 24));
stop.setEnabled(false); add(stop);
stop.addActionListener(this); go = new
Button("GO");
go.setBackground(Color.green);
go.setForeground(Color.white); go.setFont(new
Font("SanSerif", Font.BOLD, 24));
go.setEnabled(true); add(go);
go.addActionListener(this); }
public void
actionPerformed(ActionEvent e) { if
(e.getSource().equals(stop)) {
stop.setEnabled(false);
go.setEnabled(true); } else
{
go.setEnabled(false);
stop.setEnabled(true); } }
public void windowClosing(WindowEvent e) {
dispose(); System.exit(0); }
public void windowOpened(WindowEvent e) {} public void
windowActivated(WindowEvent e) {} public void
windowIconified(WindowEvent e) {} public void
windowDeiconified(WindowEvent e) {} public void
windowDeactivated(WindowEvent e) {} public void
windowClosed(WindowEvent e) {} }
Notes:
-
The Button references stop and go are instance variables of the class
-
The main() method sets the frame's size, its
background color, and makes it visible
-
As part of its work, the class
constructor instantiates each button, sets the button's background color,
foreground color, font, accessibility, adds the button to the frame, and
registers its ActionListener. The stop button is initially disabled and the go button is initially enabled.
The Color and Font classes will be covered in
a later lesson.
-
The actionPerformed() method is automatically called
when the user clicks either button and receives an ActionEvent object as a parameter. The getSource() method of the ActionEvent object returns a
reference to the object that was acted upon. The referenced object is
immediately compared to the stop button to determine if it was clicked. The logic of this
sample simply flips which button is currently enabled.
Lab exercise for Ferris
students
E-mail your answers to this
assignment no later than
the due date listed in the class schedule.
Review questions
-
If the someComponent.getX() returns 0 and someComponent.getY() returns 0, which one of the following statements are true?
-
the top-left corner of
someComponent is in the top-left
corner of the screen
-
the top-left corner of
someComponent is in the top-left
corner of its container
-
someComponent will not be visible
-
someComponent has no size
-
the bottom-left corner of
someComponent is in the bottom-left
corner of its container
-
Assume that fiddle and twiddle reference properly instantiated components. Which
one of the following statements would assure that component twiddle uses exactly the same font as
component fiddle?
-
twiddle.Font = fiddle.Font;
-
twiddle.setFont(fiddle.Font);
-
twiddle.Font = fiddle.getFont();
-
twiddle.setFont(fiddle.getFont());
-
fiddle.setFont(twiddle.getFont());
-
A Label is constructed and given a foreground color of red. No
background color is specified. It is then added to a Frame that has a foreground color of white and a background
color of black. Which one statement is true of the Label?
-
its text will appear with
red letters on a black background
-
its text will appear with
red letters on a white background
-
its text will appear with
red letters on a background whose color is unpredictable
-
both its text color and
background color are unpredictable
-
a run-time error will
occur because no background color was specified
-
Which one of the following
statements will hide a component whose reference is showTime?
-
showTime.isVisible(false);
-
showTime.setEnabled(false);
-
showTime.setVisible(false);
-
showTime.setHidden(true);
-
showTime.isHidden(true); Printer Friendly Page
Send to a Friend
..
Search here again if you need more info!
|
|
|