Java实现一个模型、两个视图和两个控制器的功能软件,即采用MVC模式或者说是观察者模式,本程序通过输入球体半径,显示球体形状,面积体积等Sphere.javapackageModel;importjava.util.Observable;publicclassSphereextendsObservable{ privatedoubleradius;//球体半径 privatedoublearea;//球体面积 privatedoublevolume;//球体体积 publicSphere() { radius=100d; area=4*Math.PI*Math.pow(radius,2); volume=4*Math.PI*Math.pow(radius,3)/3; } publicdoublegetRadius() { returnradius; } publicdoublegetArea() { returnarea; } publicdoublegetVolume() { returnvolume; } publicvoidsetRadius(doubleradius) { this.radius=radius; this.area=4*Math.PI*Math.pow(radius,2); this.volume=4*Math.PI*Math.pow(radius,3)/3; this.setChanged(); this.notifyObservers(); } }textView.javapackageView;importjava.util.Observer;importjava.util.Observable;importjava.text.NumberFormat;importjavax.swing.*;importController.TextController;importModel.Sphere;importjava.awt.*;importjava.awt.event.*;publicclassTextViewextendsJPanelimplementsObserver{ privateJLabelradiusLab;//提示用户输入球体半径 privateJTextFieldradiusTextField;//接受用户输入球体半径 //privateJLabelradiusRang; privateJLabelareaLab;//显示球体面积 privateJTextFieldareaTextField;//显示输入球体半径对应的面积 privateJLabelvolumeLab;//显示球体体积 privateJTextFieldvolumeTextField;//显示输入球体半径对应的体积 public TextView() { try{ Init(); } catch(Exceptione){ e.printStackTrace(); } } privatevoidInit()throwsException{ radiusLab=newJLabel("球体半径");radiusLab.setForeground(newColor(0,165,168));//radiusRang=newJLabel("[0-200]"); radiusTextField=newJTextField(12); radiusTextField.setForeground(newColor(223,100,158)); radiusTextField.setBackground(newColor(210,204,230)); areaLab=newJLabel("球体面积"); areaLab.setForeground(newColor(0,165,168)); areaTextField=newJTextField(12); areaTextField.setBackground(newColor(193,219,219)); areaTextField.setEditable(fal
1