Java Reference
In-Depth Information
Figure 10•11. Swing components in ScribblePane3
Example 10•13: ScribblePane3.java (continued)
/**
* This scribble component includes a JButton to clear the screen, and
* a JList that lets the user select a drawing color. It uses
* event listener objects to handle events from those sub-components.
**/
public class ScribblePane3 extends ScribblePane2 {
// These are colors the user can choose from
Color[] colors = new Color[] { Color.black, Color.red, Color.blue };
// These are names for those colors
String[] colorNames = new String[] { "Black", "Red", "Blue" };
// Add JButton and JList components to the panel.
public ScribblePane3() {
// Implicit super() call here invokes the superclass constructor
// Add a "Clear" button to the panel.
// Handle button events with an action listener
JButton clear = new JButton("Clear");
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { clear(); }
});
this.add(clear);
// Add a JList to allow color choices.
// Handle list selection events with a ListSelectionListener.
final JList colorList = new JList(colorNames);
colorList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
setColor(colors[colorList.getSelectedIndex()]);
}
});
this.add(colorList);
}
}
Search WWH ::




Custom Search