Java Reference
In-Depth Information
String label = a.getActionCommand();
if(label.equals(“Red”))
{
container.setBackground(Color.RED);
}
else if(label.equals(“Blue”))
{
container.setBackground(Color.BLUE);
}
else if(label.equals(“White”))
{
container.setBackground(Color.WHITE);
}
}
}
Within the actionPerformed() method of the ColorChanger class, the label
on the button is obtained by invoking the getActionCommand() on the
ActionEvent object:
String label = a.getActionCommand();
The action command of a button by default is the label. The if/else block
sets the background of a Container to the color represented by the button's
label. The Container in this example is the JFrame window ButtonDemo.
Figure 13.3 shows the output of the ButtonDemo program.
Figure 13.3
Clicking a button changes the background color of the window.
Search WWH ::




Custom Search