This example shows the use of a simple button, but demonstrates
some differences between the AWT button and the KVM Button. In this
example, we bring up a Simple Spotlet, with two simple Buttons. One
is an exit Button, which you saw in the HelloWorld example, and the
other is a test button, showing how the penUp and penDown events
work.
The full source code of the
example
Things to note on the KVM Button:
- The constructor for a Button takes an x,y pair specifying the
top-left corner of the button and either a String or a Bitmap (but not
both)
- In the paint() method of the Spotlet, the Button must be
explicitly drawn. With no layout managers as in the AWT toolkit, it
is up to you to determine when a component should be drawn. In
addition, it is also up to you whether to clear the entire screen, or
clear a small region before painting new data. The code below clears
the entire screen before drawing two strings and two buttons on the
display:
private void paint() {
g.clearScreen();
g.drawString(" HelloButton Demo ", 34, 0, Graphics.INVERT);
g.drawString(outputString, 50, 80);
// Draw GUI controls and buttons
exitButton.paint();
testButton.paint();
- You don't register actionListeners with KVM Buttons, instead you
merely test to see if a penDown or penUp event occured in a Buttons
area. You do this by using the pressed(int x, int y) method to see if
the pen event occured in the component. This in fact is a common test
to see which GUI component a penDown/Up event occurs in.
if (testButton.pressed(x,y)) {
... react to the testButton being pressed here ...
}
- When you press a KVM Button, the button color inverts for an
instant and then clears, it does not stay inverted as long as you
hold the pen down. This also differs slightly from AWT behavior.
When you run this example you can see the sequence of button
press/release events.