import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// class extends JFrame to make a GUI and creates a Counter
// object from the Counter class
public class Lab2 extends JFrame {
private Counter aCounter = new Counter(); // counter object used by this class
private JLabel counterLabel = new JLabel("0"); // label that displays the count
private JButton incrementButton = new JButton("+"); // button to incrememnt the count
private JButton decrementButton = new JButton("-"); // button to decrememnt the count
private JButton resetButton = new JButton("Reset"); // button to set the count to 0
// action listener objects to pass through
// addActionListener methods of each Button
private IncrementButtonHandler ibHandler = new IncrementButtonHandler();
private DecrementButtonHandler dbHandler = new DecrementButtonHandler();
private ResetButtonHandler rbHandler = new ResetButtonHandler();
private Container c = getContentPane(); // container to hold contents of the content pane
private Dimension size; // dimension to hold the preferred size of each component
private Font labelFont = new Font(Font.SANS_SERIF, Font.BOLD, 100); // label font object
private Font buttonFont = new Font(Font.MONOSPACED, Font.BOLD, 25); // + and - font object
private static final int WIDTH = 140; // width of the window
private static final int HEIGHT = 205; // height of the window
// default constructor that calls methods to set up the frame
public Lab2()
{
setFrameProperties();
setComponentProperties();
addComponents();
addListeners();
}
// method to set the properties of the components
public void setComponentProperties()
{
counterLabel.setFont(labelFont);
size = counterLabel.getPreferredSize();
counterLabel.setBounds(40, 10, size.width, size.height);
decrementButton.setFont(buttonFont);
size = decrementButton.getPreferredSize();
decrementButton.setBounds(10, 125, size.width, size.height);
incrementButton.setFont(buttonFont);
size = incrementButton.getPreferredSize();
incrementButton.setBounds(75, 125, size.width, size.height);
size = resetButton.getPreferredSize();
resetButton.setBounds(35, 5, size.width, size.height);
}
// method to initialize the frame properties
public void setFrameProperties()
{
setVisible(true);
setTitle("Counter");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
}
// method to add the components in the content pane
public void addComponents()
{
c.setLayout(null);
c.add(counterLabel);
c.add(incrementButton);
c.add(decrementButton);
c.add(resetButton);
}
// method to add the listeners to the buttons
public void addListeners()
{
incrementButton.addActionListener(ibHandler);
decrementButton.addActionListener(dbHandler);
resetButton.addActionListener(rbHandler);
}
// action listener for the incrementButton
public class IncrementButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
aCounter.incrementCount();
counterLabel.setText(Integer.toString(aCounter.getCount()));
size = counterLabel.getPreferredSize();
if (aCounter.getCount() < 10)
counterLabel.setBounds(40, 10, size.width, size.height);
else
counterLabel.setBounds(10, 10, size.width, size.height);
}
}
// action listener for the decrementButton
public class DecrementButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
aCounter.decrementCount();
counterLabel.setText(Integer.toString(aCounter.getCount()));
size = counterLabel.getPreferredSize();
if (aCounter.getCount() < 10)
counterLabel.setBounds(40, 10, size.width, size.height);
else
counterLabel.setBounds(10, 10, size.width, size.height);
}
}
// action listener for the resetButton
public class ResetButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
aCounter.resetCount();
counterLabel.setText(Integer.toString(aCounter.getCount()));
counterLabel.setBounds(40, 10, size.width, size.height);
}
}
// main method instantiates the GUI
public static void main(String[] args)
{
Lab2 lab2GUI = new Lab2();
}
}