Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Ke Shan
cs349-code
Commits
b6d3d4a6
Commit
b6d3d4a6
authored
Oct 18, 2017
by
Daniel Vogel
Browse files
better modellistener method
parent
1487ab31
Changes
4
Hide whitespace changes
Inline
Side-by-side
java/7-mvc/hellomvc3b/Main.java
View file @
b6d3d4a6
...
...
@@ -21,13 +21,9 @@ public class Main {
// create View, tell it about model
View
view
=
new
View
(
model
);
// tell Model about View's listener inner class
model
.
addView
(
view
.
modelListener
);
// create second view ...
View2
view2
=
new
View2
(
model
);
// tell Model about View's listener inner class
model
.
addView
(
view2
.
modelListener
);
// create a layout panel to hold the two views
JPanel
p
=
new
JPanel
(
new
GridLayout
(
2
,
1
));
...
...
java/7-mvc/hellomvc3b/View.java
View file @
b6d3d4a6
...
...
@@ -17,8 +17,6 @@ class View extends JPanel {
// the model that this view is showing
private
Model
model
;
// model listener
public
IView
modelListener
;
public
View
(
Model
model
)
{
...
...
@@ -35,12 +33,13 @@ class View extends JPanel {
this
.
model
=
model
;
// anonymous class acts as model listener
modelListener
=
new
IView
()
{
this
.
model
.
addView
(
new
IView
()
{
public
void
updateView
()
{
System
.
out
.
println
(
"View: updateView"
);
button
.
setText
(
Integer
.
toString
(
model
.
getCounterValue
()));
}
};
});
// setup the event to go to the "controller"
// (this anonymous class is essentially the controller)
...
...
java/7-mvc/hellomvc3b/View2.java
View file @
b6d3d4a6
...
...
@@ -14,9 +14,6 @@ class View2 extends JPanel {
private
Model
model
;
private
JLabel
label
=
new
JLabel
();
// model listener
public
IView
modelListener
;
View2
(
Model
model
)
{
// create UI
setBackground
(
Color
.
WHITE
);
...
...
@@ -26,16 +23,16 @@ class View2 extends JPanel {
this
.
model
=
model
;
// anonymous class acts as model listener
modelListener
=
new
IView
()
{
this
.
model
.
addView
(
new
IView
()
{
public
void
updateView
()
{
System
.
out
.
println
(
"View2: updateView"
);
// just displays an 'X' for each counter value
String
s
=
""
;
for
(
int
i
=
0
;
i
<
model
.
getCounterValue
();
i
++)
s
=
s
+
"X"
;
label
.
setText
(
s
);
System
.
out
.
println
(
"View2: updateView"
);
// just displays an 'X' for each counter value
String
s
=
""
;
for
(
int
i
=
0
;
i
<
model
.
getCounterValue
();
i
++)
s
=
s
+
"X"
;
label
.
setText
(
s
);
}
};
}
)
;
// setup the event to go to the "controller"
// (this anonymous class is essentially the controller)
addMouseListener
(
new
MouseAdapter
()
{
...
...
java/7-mvc/triangle/model/TriangleModel.java
View file @
b6d3d4a6
...
...
@@ -5,7 +5,7 @@ import java.util.ArrayList;
import
javax.swing.undo.AbstractUndoableEdit
;
import
javax.swing.undo.UndoableEdit
;
public
class
TriangleModel
extends
Object
{
public
class
TriangleModel
{
/* A list of the model's views. */
private
ArrayList
<
IView
>
views
=
new
ArrayList
<
IView
>();
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment