Java Reference
In-Depth Information
We can make up our own class, StatusBar for instance, that will define a status bar. Ideally we would
design a class for a generic status bar and customize it for Sketcher, but we will take the simple
approach of designing a class that is specific to Sketcher. The JPanel class would be a good base for
our StatusBar class since it represents a panel, and we can add objects representing status bar panes
to it. We can use the JLabel class as a base for defining status bar panes and add sunken borders to
them for a distinct appearance.
Let's start with a status bar at the bottom of Sketcher with two panes to show the current element type
and color. Then we will know exactly what we are about to draw. We can start by defining the
StatusBar class that will represent the status bar in the application window, and we'll define the
StatusPane class as an inner class to StatusBar .
Try It Out - Defining a Status Bar Class
Here's an initial stab at the definition for the StatusBar class:
// Class defining a status bar
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
class StatusBar extends JPanel implements Constants {
// Constructor
public StatusBar() {
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 3));
setBackground(Color.LIGHT _ GRAY);
setBorder(BorderFactory.createLineBorder(Color.DARK _ GRAY));
setColorPane(DEFAULT _ ELEMENT _ COLOR);
setTypePane(DEFAULT _ ELEMENT _ TYPE);
add(colorPane); // Add color pane to status bar
add(typePane); // Add type pane to status bar
}
// Set color pane label
public void setColorPane(Color color) {
// Code to set the color pane text...
}
// Set type pane label
public void setTypePane (int elementType) {
// Code to set the type pane text....
}
// Panes in the status bar
private StatusPane colorPane = new StatusPane("BLUE");
private StatusPane typePane = new StatusPane("LINE");
// Class defining a status bar pane
class StatusPane extends Jlabel {
public StatusPane(String text) {
setBackground(Color.LIGHT _ GRAY); // Set background color
setForeground(Color.BLACK);
Search WWH ::




Custom Search