软件构建模式之MVC框架初窥(二)
false);//////
topPanel.setLayout(new FlowLayout());
topPanel.add(jLabel1);
topPanel.add(rTextField);
topPanel.add(jButton1);
slider.setValue(10);
topPanel.add(slider);
bottomPanel.setLayout(new FlowLayout());
bottomPanel.add(jLabel2);
bottomPanel.add(cTextField);
bottomPanel.add(jLabel3);
bottomPanel.add(sTextField);
bottomPanel.add(jLabel4);
bottomPanel.add(bTextField);
// bottomPanel.add(jLabel5);//////
// bottomPanel.add(dTextField);//////
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(topPanel,BorderLayout.NORTH);
this.getContentPane().add(vp,BorderLayout.CENTER);
this.getContentPane().add(bottomPanel,BorderLayout.SOUTH);
this.setSize(640,480);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO code application logic here
MainFrame mf = new MainFrame("MVC练习 ");
mf.setVisible(true);
}
}
2.视图.位于包com.view下,全路径com.view.*
[java
/*
* BTextField.java
*/
package com.view;
import com.util.ObserverInterface;
import java.text.DecimalFormat;
import javax.swing.JTextField;
import com.model.CircleModel;
public class BTextField extends JTextField implements ObserverInterface{
private CircleModel model;
private DecimalFormat df = new DecimalFormat("######0.00");
/** Creates a new instance of RTextField */
public BTextField() {
}
public BTextField(CircleModel model){
super(10);
this.model = model;
this.update();
}
public void dataUpdate(CircleModel cm) {
this.setModel(getModel());
update();
}
private void update(){
this.setText(df.format(model.computeBallSuperficial()));
}
public CircleModel getModel() {
return model;
}
public void setModel(CircleModel model) {
this.model = model;
}
}
[java]
/*
* ControlSlider.java
*/
package com.view;
import com.controller.Controller;
import com.util.ObserverInterface;
import java.text.DecimalFormat;
import javax.swing.JSlider;
import javax.swing.JTextField;
import com.model.CircleModel;
/**
*
*/
public class ControlSlider extends JSlider implements ObserverInterface{
private CircleModel circleModel;
/** Creates a new instance of ControlSlider */
public ControlSlider() {
}
public ControlSlider(CircleModel circleModel){
super(0,Controller.MAXRADII,(int)circleModel.getRadii());
this.circleModel = circleModel;
}
public void dataUpdate(CircleModel circleModel) {
setCircleModel(circleModel);
setValue((int)Math.round(circleModel.getRadii()));
}
public CircleModel getCircleModel() {
ret