Java implementation of MVC mode

zhaozj2021-02-16  54

/*data.java* created on 2004-6-17 ** to change the template for this generated file go to * window> preferences> java> code generation> code and comments * / package mvctest2;

/ *** @author Administrator ** To change the template for this generated type comment go to * Window> Preferences> Java> Code Generation> Code and Comments * / public class Data {public int value; // student age values ​​public String Name; // Student Name}

/*MODEL.JAVA* CREATED ON 2004-6-17 ** To change the template for this generated file go to * window> preferences> java> code generation> code and comments * / package mvctest2;

/ *** @author Administrator ** To change the template for this generated type comment go to * Window> Preferences> Java> Code Generation> Code and Comments * / import java.util *;. Import java.util.Observable;

Public class model extends observable {protected arraylist data = new arraylist (); public model () {super ();} public model (int [] value, string [] name) {for (int i = 0; i

Public int size () {return data.size ();

// When the data changes, this method is called by Controller, informs each OBServer, refresh the view .public void changel (mod) {data.clear (); for (int i = 0; i

{this.adddata (Model.Getdata (i));} setchanged (); // indeicates That the model Has ChangeDNotifyObservers (this);}} / * controller.java * created on 2004-6-17 ** To change the Template for this generated file go to * window> preferences> java> code generation> code and commertes * / package mvctest2;

/ *** @author Administrator ** To change the template for this generated type comment go to * Window> Preferences> Java> Code Generation> Code and Comments * / import java.awt *;. Import javax.swing *;. / / import javax.swing.border *;. import java.awt.event *;. public class Controller extends JFrame {Model model = new Model (); TextView txtView = new TextView (model); GraphicsView graphView = new GraphicsView (model) ;

JScrollPane jScrollPane1 = new JScrollPane (); JButton jButton1 = new JButton (); JTextField jTextField1 = new JTextField (); JTextField jTextField2 = new JTextField (); JLabel jLabel1 = new JLabel (); JLabel jLabel2 = new JLabel (); JLabel jLabel3 = New jlabel (); public controller () {Try {Super.Settitle ("MVC Parrten Test"); jbinit ();} catch (exception e) {E.PrintStackTrace ();}

} private void jbinit () THROWS EXCETION {data [] data = new data [4]; data [0] = new data (); data [1] = new data (); data [2] = new data (); Data [3] = new data (); data [0] .name = "ted"; data [0] .value = 20; Data [1] .name = "joy"; data [1] .value = 14; Data [2] .name = "MARS"; DATA [2] .Value = 23; DATA [3] .name = "martian"; data [3] .value = 25; Model.Adddata (Data [0]); Model.adddata (Data [1]); Model.adddata (Data [2]); Model.Adddata (Data [3]);

// Note the following two lines: Register its observer View1 and View2.Model.addobserver (txtview) to the model; Model.Addobserver (GraphView);

THISLETCONTENTPANE (). setLayout (null); JscrollPane1.setBounds (New Rectangle (0, 0, 3, 3)); JButton1.setBounds (New Rectangle (309, 259, 101, 27); jbutton1.setText (" Update "); jButton1.addActionListener (new java.awt.event.ActionListener () {public void actionPerformed (ActionEvent e) {jButton1_actionPerformed (e);}}); jTextField1.setText (" 20 "); jTextField1.setBounds (new Rectangle (80, 254, 52, 30)); jtextfield2.settext ("14"); jtextfield2.setbounds (New Rectangle (178, 255, 50, 31)); Jlabel1.SetText ("Age:"); Jlabel1. SetBounds (New Rectangle (41, 226, 47, 23); Jlabel2.Settext ("TED"); Jlabel2.SetBounds (New Rectangle (42, 252, 35, 33)); Jlabel3.Settext ("Joy"); Jlabel3.SetBounds (New Rectangle (144, 255, 31, 31)); TXTVIEW.SETBOUNDs (New Rectangle (7, 5, 225, 208)); GraphView.setBounds (New Rectangle (234, 4, 219, 209))) This.getContentPane (). add (jscrollpane1, null); this.getContentPane (). add (jtextfield2, null); this.getContentPane (). add (jtextfield1, null); this.getContentpane (). Add (jlabel2, NULL); this.getContentPane (). Add (jlabel3 , NULL); this.getContentPane (). add (jlabel1, null); this.getContentpane (). add (jbutton1, null); this.getContentPane (). add (txtView, null); this.getContentpane (). add (graphView, null);} // Press the Update button to inform MODEL data change .void jbutton1_actionperperformed (ActionEvent E) {data [] data = new data [2]; data [0] = new data (); data [ 1] = new data (); data [0] .Name = jlabel2.getText (); data [0] .value = integer.parseint (jtextfield1.gettext ()); data [1] .name = jlabel3.gettext ); Data [1] .value = integer.parseint (jtextfield2.gettext ()); model m = new model (DATA); this.Model.changemodel (m);} public static void main (string [] args) { Controller C = New Controller (); C.setsize (475, 350); C.SetVisible (TRUE);

}} / * Graphview.java * created on 2004-6-17 ** To change the template for this generated file go to * window> preferences> java> code generation> code and comments * / package mvctest2;

/ *** @author Administrator ** To change the template for this generated type comment go to * Window> Preferences> Java> Code Generation> Code and Comments * / import javax.swing *;. Import java.awt *;. Import Javax.swing.border. *; import java.util.observer; import java.util.observable;

Public Class Graphicsview Extends JPanel Implements Observer {Model Model

public GraphicsView () {} public GraphicsView (Model model) {try {this.model = model; jbInit ();} catch (Exception e) {e.printStackTrace ();}} private void jbInit () throws Exception {this. SetBackground (color.white); this.setBorder (New TitledBorder (BorderFactory.createLineborder (Color. Black, 1), "GraphicsView");}

Public void PaintComponent (graphics g) {super.paintComponent (g); if (model == null) return; int x = 10, y = 30; int h = g.getFontMetrics (). getHeight (); int width = this .GETWIDTH (); int Height = this.getHEight (); int sy = height / (model.size () 1); int SX = Width / 2 -40; for (int i = 0; i

{G.SetColor (C); G. Drawoval (CX, CY, R, R); R = DX;} // * / g.draw3dRect (CX, CY, Value * 2, 15, true); g.setcolor (c); g.fill3dRect (CX, CY, Value * 2, 15, true); g.drawstring (data.name, 25, cy 15);}} // will be automatically called when the model data changes This method refreshes graphical public void update (Observable O, Object Arg) {this.model = (model) O; repaint ();}} / ** to change the template for thiserated File Go to * Window> Preferences> Java> Code Generation> Code and Comments * / package mvctest2;

/ *** @author Administrator ** To change the template for this generated type comment go to * Window> Preferences> Java> Code Generation> Code and Comments * / import javax.swing *;. Import java.awt *;. Import Javax.swing.border. *; import java.util.observer; import java.util.observable;

Public Class TextView Extends JPanel Implements Observer {Model Model

Public textview () {} public textview (model model) {TRY {this.model = model; jbinit ();} catch (exception e) {E.PrintStackTrace ();}} private void jbinit () throws exception {this. SetBackground (color.white); this.setBorder (New TitledBorder (BorderFactory.createLineborder (Color.Black, 1), "TextView");

Public void PainTComponent (GRAPHICS G) {Super.PaintComponent (G); if (model == null) return; int x = 20, y = 50; int h = g.GetFontMetrics (). getHeight (); for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-20684.html

New Post(0)