Java Reference
In-Depth Information
File: its/CounterInterfaceGUI/CounterInterfacePanel.Java
1. package its.CounterInterfaceGUI;
2.
3. import javax.swing.JPanel;
4. import javax.swing.JButton;
5. import javax.swing.JLabel;
6. import java.awt.BorderLayout;
7. import javax.swing.SwingConstants;
8. import java.awt.event.ActionListener;
9. import java.awt.event.ActionEvent;
10.
11.
public class CounterInterfacePanel extends JPanel
12.
implements ActionListener{
13.
14.
private CounterModel counter;
15.
private JLabel valueLabel;
16.
17.
public CounterInterfacePanel() {
18.
counter = new CounterModel();
19.
20.
BorderLayout bordLay = new BorderLayout();
21.
this .setLayout(bordLay);
22.
23.
JButton upButton
= new JButton("Up");
24.
JButton downButton = new JButton("Down");
25.
valueLabel = new JLabel(""+counter.getValue(),SwingConstants.CENTER);
26.
27.
this .add(upButton,BorderLayout.WEST);
28.
this .add(downButton,BorderLayout.EAST);
29.
this .add(valueLabel,BorderLayout.CENTER);
30.
31.
// A CounterInterfacePanel is now
32.
// also an ActionListener. Therefore, it
33.
// is assigned to the buttons using ”this”.
34.
upButton.addActionListener( this );
35.
downButton.addActionListener( this );
36.
}
37.
38.
public void increment(){
39.
counter.increment();
40.
valueLabel.setText(""+counter.getValue());
41.
}
42.
43.
public void decrement(){
44.
counter.decrement();
Search WWH ::




Custom Search