Dieser Beitrag ist vor einiger Zeit auf meiner alten Webseite erschienen. Nach der Schließung dieser, ist er nun hier.
In folgendem Beitrag wird die Funktionsweise des Java MVC Pattern erklärt.
Die Main-Methode
1 2 3 4 5 6 7 8 9 |
public class MVCEnergie { /** * @param args */ public static void main(String[] args) { new MVCController(); } } |
Das Model
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 |
import java.util.Observable; public class MVCDecInc extends Observable { private int counter = 0; public int getCounter() { return counter; } public void inc(){ counter += 1; System.out.println(this.counter); setChanged(); notifyObservers(this); } public void dec(){ counter -= 1; System.out.println(this.counter); setChanged(); notifyObservers(this); } public String counterToString(){ return Integer.toString(this.counter); } } |
Der Controller
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 |
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MVCController implements ActionListener { private MVCDecInc decinc; private MVCView view, view2, view3; public MVCController(){ // mein Model this.decinc = new MVCDecInc(); // meine Views this.view = new MVCView(this); this.view2 = new MVCView(this); this.view3 = new MVCView(this); // Observer zu den Modellen hinzufügen. this.decinc.addObserver(this.view); this.decinc.addObserver(this.view2); this.decinc.addObserver(this.view3); this.view.setVisible(true); this.view2.setVisible(true); this.view3.setVisible(true); } @Override public void actionPerformed(ActionEvent arg0) { System.out.println("ACTION: " + arg0.getActionCommand().toString()); if(arg0.getActionCommand().equalsIgnoreCase(MVCStatics.ACTION_DEC)) this.decinc.dec(); if(arg0.getActionCommand().equalsIgnoreCase(MVCStatics.ACTION_INC)) this.decinc.inc(); } } |
Die View
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 |
import java.util.Observable; import java.util.Observer; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.SpringLayout; import javax.swing.border.EmptyBorder; public class MVCView extends JFrame implements Observer { private static final long serialVersionUID = 1L; private JPanel contentPane; private JLabel label = new JLabel("0"); /** * Create the frame. */ public MVCView(MVCController control) { setTitle("Java MVC Beispiel"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 152, 150); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); SpringLayout sl_contentPane = new SpringLayout(); contentPane.setLayout(sl_contentPane); JSplitPane splitPane = new JSplitPane(); sl_contentPane.putConstraint(SpringLayout.EAST, splitPane, 0, SpringLayout.EAST, contentPane); splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); splitPane.setOneTouchExpandable(true); sl_contentPane.putConstraint(SpringLayout.NORTH, splitPane, 0, SpringLayout.NORTH, contentPane); sl_contentPane.putConstraint(SpringLayout.WEST, splitPane, 0, SpringLayout.WEST, contentPane); contentPane.add(splitPane); JButton btnDec = new JButton("dec"); btnDec.addActionListener(control); btnDec.setActionCommand(MVCStatics.ACTION_DEC); splitPane.setLeftComponent(btnDec); JButton btnInc = new JButton("inc"); btnInc.addActionListener(control); btnInc.setActionCommand(MVCStatics.ACTION_INC); splitPane.setRightComponent(btnInc); JLabel lblAktZahl = new JLabel("Akt. Zahl:"); sl_contentPane.putConstraint(SpringLayout.NORTH, lblAktZahl, 1, SpringLayout.SOUTH, splitPane); sl_contentPane.putConstraint(SpringLayout.WEST, lblAktZahl, 0, SpringLayout.WEST, splitPane); contentPane.add(lblAktZahl); sl_contentPane.putConstraint(SpringLayout.SOUTH, label, 0, SpringLayout.SOUTH, contentPane); sl_contentPane.putConstraint(SpringLayout.EAST, label, 0, SpringLayout.EAST, splitPane); contentPane.add(label); } @Override public void update(Observable arg0, Object arg1) { if(arg1 instanceof MVCDecInc){ this.label.setText(((MVCDecInc) arg1).counterToString()); System.out.println("Text wurde gesetzt"); } } } |
Die Statics
1 2 3 4 5 6 |
public class MVCStatics { // Actionscommands public static final String ACTION_DEC = "action_dec"; public static final String ACTION_INC = "action_inc"; } |