标签:style class code c java ext
import java.util.Observable; class Sphere extends Observable { .... public void setRadius(double r) { myRadius = r; setChanged(); // Indicates that the model has changed notifyObservers(); } .... } |
import java.util.Observer; import java.util.Observable; public class TextView extends JPanel implements Observer { ...... public void update(Observable o, Object arg) { Sphere balloon = (Sphere)o; radiusIn.setText(“ ”+f3.format(balloon.getRadius())); volumeOut.setText(“ ”+f3.format(balloon.volume())); surfAreaOut.setText(“ ” + f3.format(balloon.surfaceArea())); } ...... } |
public SphereWindow() { super(“Spheres: volume and surface area”); model = new Sphere(0, 0, 100); TextView view = new TextView(); model.addObserver(view); view.update(model, null); view.addActionListener(this); Container c = getContentPane(); c.add(view); } public void actionPerformed(ActionEvent e) { JTextField t = (JTextField)e.getSource(); double r = Double.parseDouble(t.getText()); model.setRadius(r); } |
public SphereWindow() { super(“Spheres: volume and surface area”); model = new Sphere(0, 0, 100); TextView tView = new TextView(); model.addObserver(tView); tView.addActionListener(this); tView.update(model, null); GraphicsView gView = new GraphicsView(); model.addObserver(gView); gView.update(model, null); Container c = getContentPane(); c.setLayout(new GridLayout(1, 2)); c.add(tView); c.add(gView); } |
public SphereWindow() { super(“Spheres: volume and surface area”); Sphere model = new Sphere(0, 0, 100); TextController tController = new TextController(model); GraphicsController gController = new GraphicsController(model); Container c = getContentPane(); c.setLayout(new GridLayout(1, 2)); c.add(tController.getView()); c.add(gController.getView()); } |
标签:style class code c java ext
原文地址:http://www.cnblogs.com/zuo-zijing/p/3730352.html