import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; public class CounterPanel extends JPanel implements ActionListener { private static final String COMMAND_UP = "up"; private static final String COMMAND_DOWN = "down"; private static final String COMMAND_RESET = "reset"; private BasicCounter counter; private JButton countUpButton; private JButton countDownButton; private JButton resetButton; //private JLabel valueLabel; private CounterView counterView; public CounterPanel(BasicCounter counter){ this.counter = counter; countUpButton = new JButton("count up"); countUpButton.setActionCommand(COMMAND_UP); countUpButton.addActionListener(this); countDownButton = new JButton("count down"); countDownButton.setActionCommand(COMMAND_DOWN); countDownButton.addActionListener(this); resetButton = new JButton("reset"); resetButton.setActionCommand(COMMAND_RESET); resetButton.addActionListener(this); //valueLabel = new JLabel(); counterView = new CounterView(); initGui(); dumpCounterStatus(counter); } private void initGui () { //valueLabel.setHorizontalAlignment(SwingConstants.CENTER); //valueLabel.setText(""+counter.getCurrentValue()); counterView.updateView(counter.getCurrentValue()); JPanel controlsPanel = new JPanel(new GridLayout(1,3,5,5)); controlsPanel.add(countDownButton); controlsPanel.add(resetButton); controlsPanel.add(countUpButton); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints gc = new GridBagConstraints(); setLayout(gridbag); gc.gridx = 0; gc.gridy = 0; gc.insets = new Insets(15,0,15,0); gridbag.setConstraints(counterView,gc); add(counterView); gc.gridx = 0; gc.gridy = 1; gc.insets = new Insets(0,5,5,5); gridbag.setConstraints(controlsPanel ,gc); add(controlsPanel); /*gc.gridx = 0; gc.gridy = 2; gc.insets = new Insets(0,5,5,5); gridbag.setConstraints(valueLabel,gc); add(valueLabel);*/ } public void actionPerformed(ActionEvent e) { if (e.getActionCommand()==COMMAND_UP) { counter.countUp(); } if (e.getActionCommand()==COMMAND_DOWN){ counter.countDown(); } if (e.getActionCommand()==COMMAND_RESET) { counter.reset(); } //valueLabel.setText(""+counter.getCurrentValue()); counterView.updateView(counter.getCurrentValue()); dumpCounterStatus(counter); } private void dumpCounterStatus (BasicCounter counter) { System.out.println("\nCounter status:"); System.out.println("Current value: " + counter.getCurrentValue()); if (counter instanceof LimitedCounter) { LimitedCounter limetedCounter = (LimitedCounter)counter; System.out.println("Maximum value: " + limetedCounter.getMaximumValue()); System.out.println("Minimum value: " + limetedCounter.getMinimumValue()); System.out.println("Is at maximum: " + limetedCounter.isAtMaximum()); System.out.println("Is at minimum: " + limetedCounter.isAtMinimum()); } } private static class CounterView extends JPanel { // for use with negative numbers private JLabel[] numbers = new JLabel[4]; private DecimalFormat numberFormat = new DecimalFormat("+,000;-,000"); // for use with positive numbers only //private JLabel[] numbers = new JLabel[3]; //private DecimalFormat numberFormat = new DecimalFormat("000"); public CounterView () { setLayout(new FlowLayout(FlowLayout.CENTER,2,0)); Font font = new Font("Monospace", Font.BOLD, 16); for (int i=0; i