Java Reference
In-Depth Information
14.
private
JLabel valueLabel;
15.
16.
public
CounterInternalPanel() {
17.
counter =
new
CounterModel();
18.
19.
BorderLayout bordLay =
new
BorderLayout();
20.
this
.setLayout(bordLay);
21.
22.
JButton upButton
=
new
JButton("Up");
23.
JButton downButton =
new
JButton("Down");
24.
valueLabel =
new
JLabel(""+counter.getValue(),SwingConstants.CENTER);
25.
26.
this
.add(upButton,BorderLayout.WEST);
27.
this
.add(downButton,BorderLayout.EAST);
28.
this
.add(valueLabel,BorderLayout.CENTER);
29.
30.
// Definition of the listener, no reference
31.
// to ”this” is passed on.
32.
InternalCounterListener countList =
33.
new
InternalCounterListener();
34.
upButton.addActionListener(countList);
35.
downButton.addActionListener(countList);
36.
}
37.
38.
public void
increment(){
39.
counter.increment();
40.
valueLabel.setText(""+counter.getValue());
41.
}
42.
43.
public void
decrement(){
44.
counter.decrement();
45.
valueLabel.setText(""+counter.getValue());
46.
}
47.
48.
// Internal Listener Class
49.
class
InternalCounterListener
implements
ActionListener{
50.
51.
//The constructor is empty
52.
public
InternalCounterListener() {
53.
}
54.
55.
public void
actionPerformed(ActionEvent evt){
56.
String actionCommand = evt.getActionCommand();
57.
if
(actionCommand.equals("Up")){
58.
increment();
59.
}
60.
else if
(actionCommand.equals("Down")){
Search WWH ::
Custom Search