Skip to content

Commit 8a0ea7f

Browse files
committed
(Model)-View-Controller Beispiel
1 parent c8cfc63 commit 8a0ea7f

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Fach_3_Swing.ch.ibw.swing.events;
2+
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
public class RetoEventController implements ActionListener {
7+
8+
RetoEventView view;
9+
10+
public RetoEventController(RetoEventView view) {
11+
this.view = view;
12+
}
13+
14+
@Override
15+
public void actionPerformed(ActionEvent actionEvent) {
16+
System.out.println("command: " +actionEvent.getActionCommand());
17+
if (actionEvent.getActionCommand() == RetoEventView.OKBUTTON ) {
18+
view.aktualisiereAnzeige("OK gedrueckt");
19+
} else if (actionEvent.getActionCommand() == RetoEventView.CANCELBUTTON ) {
20+
view.aktualisiereAnzeige("CANCEL gedrueckt");
21+
}
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package Fach_3_Swing.ch.ibw.swing.events;
2+
3+
import javax.swing.*;
4+
5+
/**
6+
* Created by user on 22.01.2017.
7+
*/
8+
public class RetoEventTestRahmen {
9+
public static void main(String[] args) {
10+
RetoEventView r = new RetoEventView();
11+
r.setTitle("Test");
12+
r.setSize(240,160);
13+
r.setVisible(true);
14+
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15+
}
16+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package Fach_3_Swing.ch.ibw.swing.events;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
import java.awt.event.ActionEvent;
6+
import java.awt.event.ActionListener;
7+
8+
/**
9+
* Created by user on 22.01.2017.
10+
*/
11+
public class RetoEventView extends JFrame {
12+
13+
public static final String OKBUTTON = "okPressed";
14+
public static final String CANCELBUTTON = "cancelPressed";
15+
16+
// neuer Controller instanzieren, wir geben ihm als Parameter uns selber (view) mit
17+
// damit kann der Controller dann Daten direkt zu uns zurueckgeben
18+
private RetoEventController cntl = new RetoEventController(this);
19+
20+
JLabel anzeige;
21+
public void aktualisiereAnzeige(String anzeigeText) {
22+
this.anzeige.setText(anzeigeText);
23+
}
24+
25+
//Constructor
26+
RetoEventView() {
27+
// 2 Rows, 1 Spalte
28+
this.setLayout(new GridLayout(2,1));
29+
30+
JButton okButton = new JButton("ok");
31+
// ACTIONLISTENER!
32+
okButton.addActionListener(cntl);
33+
okButton.setActionCommand(OKBUTTON);
34+
35+
JButton cancelButton = new JButton("cancel");
36+
// ACTIONLISTENER!
37+
cancelButton.addActionListener(cntl);
38+
cancelButton.setActionCommand(CANCELBUTTON);
39+
40+
anzeige = new JLabel();
41+
anzeige.setText("-");
42+
43+
// aufs Layout
44+
this.add(okButton);
45+
this.add(cancelButton);
46+
this.add(anzeige);
47+
}
48+
49+
// //--------------------------------------------------------------------
50+
// //als innere klasse
51+
// private class RetoEventController implements ActionListener {
52+
//
53+
// @Override
54+
// public void actionPerformed(ActionEvent actionEvent) {
55+
// System.out.println("command: " +actionEvent.getActionCommand());
56+
// if (actionEvent.getActionCommand() == RetoEvent.OKBUTTON ) {
57+
//
58+
// }
59+
// }
60+
// //--------------------------------------------------------------------
61+
// //als anonyme klasse (als Parameter in okButton.addActionListener() )
62+
// new ActionListener() {
63+
// @Override
64+
// public void actionPerformed(ActionEvent actionEvent) {
65+
// }
66+
// };
67+
68+
}

0 commit comments

Comments
 (0)