-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller1.java
76 lines (61 loc) · 2.17 KB
/
Controller1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package mvcexample;
/**
*
* @author savi
*/
// Controller 1: displays views 1 and 2
// (displaying A and B components of the model)
// Offers three buttons: clear views, increment A in the model
// and quit - see below
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Controller1 extends JFrame
implements ActionListener {
private Model model;
private View1 view1;
private View2 view2;
private JButton clearViews; // For direct message to views
private JButton incA; // To prompt the model to "modify" itself (A component)
private JButton quit; // As it says
// Constructor
public Controller1(Model model) {
// Record reference to the model
this.model = model;
// Configure the window
setTitle("Controller1");
setLocation(40,40);
setSize(350,150);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout()); // The default is that JFrame uses BorderLayout
// Set up input GUI
clearViews = new JButton("Clear views");
window.add(clearViews);
clearViews.addActionListener(this);
incA = new JButton("Increment A");
window.add(incA);
incA.addActionListener(this);
quit = new JButton("Quit");
window.add(quit);
quit.addActionListener(this);
// Create views
view1 = new View1(this, model);
window.add(view1);
view2 = new View2(this, model);
window.add(view2);
// Display the frame
setVisible(true);
} // constructor
// Button click handling:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearViews) {
view1.clear();
view2.clear();
}
else if (e.getSource() == incA)
model.modifyA(); // The model will trigger the views to update themselves
else if (e.getSource() == quit)
System.exit(0);
} // actionPerformed
} // class Controller1