package exampleLab10_FinalReview; import javax.swing.*; import java.awt.FlowLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeListener; /** * This is a really "clever" class that could take advantage of the MVC pattern. * * Everything is in the constructor! * * 1. Draw a UML diagram for the MVC compound pattern. * 2. (optional) Select what patterns may be helpful in implementing it. * 3. Select some state for the Controller * 4. Select some state for the Model * 5. Re-implement the program. * * (20-30 pts/100 pt final) */ public class MVCCandidate extends JFrame { int x = 0; public MVCCandidate() throws HeadlessException { setTitle("fun!"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLayout(new FlowLayout()); final JButton b = new JButton("inc"); final JLabel l = new JLabel("000"); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("inc")) { x++; } else if(e.getActionCommand().equals("dec")) { x--; } l.setText(x+""); } }); add(b); add(l); JMenuBar bar = new JMenuBar(); JMenu m = new JMenu("menu"); JMenuItem i; ActionListener a = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { b.setActionCommand(((JMenuItem)e.getSource()).getActionCommand()); } }; i = new JMenuItem("inc"); i.addActionListener(a); m.add(i); i = new JMenuItem("dec"); i.addActionListener(a); m.add(i); bar.add(m); setJMenuBar(bar); pack(); setVisible(true); } public static void main(String[] args) { new MVCCandidate(); } }