Using Components, the GUI Building Blocks |
The Button class provides a default button implementation. The onscreen appearance of Buttons depends on the platform they're running on. If you want your program's buttons to look the same for every platform or to otherwise have a special look, you should create a Canvas subclass to implement this look; you can't change the look using a Button subclass. The only facets of a Button's appearance that you can change without creating your own class are the font and text it displays and its foreground and background colors.Below is an applet that displays three buttons. When you click the left button, it disables the middle button (and itself, since it's no longer useful) and enables the right button. Similarly, when you click the right button, it enables the middle button and the left button, and disables itself.
Below is the code that creates the buttons and reacts to button clicks. (Here's the whole program.)
//In initialization code: b1 = new Button(); b1.setLabel("Disable middle button"); b2 = new Button("Middle button"); b3 = new Button("Enable middle button"); b3.disable(); public boolean handleEvent(Event e) { Object target = e.target; System.out.println("Event received: " + e); if (e.id == Event.ACTION_EVENT) { if (target == b1) { b2.disable(); b1.disable(); b3.enable(); } else if (target == b3) { b2.enable(); b1.enable(); b3.disable(); } } return super.handleEvent(e); }
Using Components, the GUI Building Blocks |