The Department of Computer Science & Engineering
|
|
STUART C. SHAPIRO: CSE
115 C
|
We will use Containers, Components, and EventHandlers
java.awt.Container
is a subclass of java.awt.Component, so every container
is also a component.
Every component must go inside a container.
Every container has or inherits the public Component
add(Component comp) method.
javax.swing.JFrame
javax.swing.JFrame is a Container that must be the
outermost Container of any GUI or other graphic display.
Everything else goes inside the JFrame.
To create a Frame, use javax.swing.Jframe frame = new
javax.swing.JFrame();
or javax.swing.Jframe frame =
new javax.swing.JFrame("Title");
- Arrange for the program to terminate when the frame's close button
is clicked, with
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
- Add components to the frame with
frame.add(component);
- Adjust the size of the frame to the size of its components with
frame.pack();
- Finally, make the frame and its components visible by
frame.setVisible(true);
java.awt.LayoutManager
containers.Columnnew containers.Column()
containers.Rownew containers.Row()
javax.swing.JLabeljavax.swing.JLabel("Label")
javax.swing.JButtonjavax.swing.JButton("Title")
javax.swing.JSliderjavax.swing.JSlider(int min, int max, int value)
Among the properties of a DrawingCanvas are a color and a dimension.
The color should be drawn from the static variables of java.awt.Color, for example java.awt.Color.green
The dimension must be an instance of java.awt.Dimension.
You should create an instance of java.awt.Dimension using
the constructor java.awt.Dimension(int
width, int height), where the width and height are measured
in pixels. The default width and height of a new DrawingCanvas are
both zero.
Constructor: graphics.DrawingCanvas()
Some Methods
For example,
voidsetColor(java.awt.Color c) java.awt.ColorgetColor() voidsetDimension(java.awt.Dimension d) java.awt.DimensiongetDimension()
graphics.DrawingCanvas canvas = graphics.DrawingCanvas(); canvas.setDimension(new java.awt.Dimension(300,500)); canvas.setColor(java.awt.Color.green);
graphics.Rectangle is a graphical object that looks
like a rectangle, has a Color and Dimension, and can be
added to a DrawingCanvas at a particular Location. The
color and dimension of a rectangle are like those for a
DrawingCanvas.
The Location of a rectangle is relative to its containing
DrawingCanvas, and must be an instance of java.awt.Point.
You should create an instance of java.awt.Point using the
constructor java.awt.Point(int
x, int y), where x and y are measured in pixels from the
origin, (0,0). The origin is at the top left-hand corner of the
DrawingCanvas. x is the distance from the orgin toward the right. y
is the distance from the origin down the screen. The default location
of a new Rectangle is (0,0).
Constructor: graphics.Rectangle()
Some Methods
voidsetColor(java.awt.Color c) java.awt.ColorgetColor() voidsetDimension(java.awt.Dimension d) java.awt.DimensiongetDimension() voidsetLocation(java.awt.point d) java.awt.pointgetLocation() - Ellipses
Agraphics.Ellipseis a graphical object that looks like an ellipse, has a Color and Dimension, and can be added to a DrawingCanvas at a particular Location. The color, dimension, and location of a rectangle are like those for a Rectangle.Constructor:
graphics.Ellipse()
Some Methods
voidsetColor(java.awt.Color c) java.awt.ColorgetColor() voidsetDimension(java.awt.Dimension d) java.awt.DimensiongetDimension() voidsetLocation(java.awt.point d) java.awt.pointgetLocation()