JAVA MVC一个非常简单的实现
用Java实现以下功能:
- 当用户在图形化用户界面输入一个圆的半径时,程序将显示该圆的周长与面积,并画出该圆(如图中右面所示);
- 当用户在图形化用户界面上拖动表示圆半径的标尺时,自动显示圆的半径、周长和面积;并在图形界面上画出图形;
- 假设程序需再扩充一项功能:要求在程序界面上显示该半径的球体的表面积,试分析如何实现这一功能,并实现该功能。
MODEL
package CreateCircle.Model;
public class circle {
private double r;
public circle(double R){r = R;}
public circle(){}
public double getR(){
return r;
}
public double getAround(){
double res = 3.14 * r * 2;
res = (double) Math.round(res * 100)/100;
return res;
}
public double getSize(){
double res = 3.14 * r * r;
res = (double) Math.round(res * 100)/100;
return res;
}
public double getSurfaceSize(){
double res = 4 * 3.14 * r * r;
res = (double) Math.round(res * 100)/100;
return res;
}
public void setR(double R){
r = R;
}
}
VIEW
package CreateCircle.View;
import CreateCircle.Model.circle;
import javax.swing.*;
import java.awt.*;
public class View{
public final JFrame jf = new JFrame("MVC");
public Box box = Box.createVerticalBox();
public JPanel panel1 = new JPanel();
public JPanel panel2 = new JPanel(){
@Override
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
circle circle = new circle();
circle.setR(Double.parseDouble(jtR.getText()));
int R = (int) circle.getR();
graphics.drawOval(400 - R,100 - R,2 * R,2 * R);
graphics.drawOval(400 - R / 2,100 - R,R,2 * R);
graphics.drawOval(400 - R,100 - R / 2,2 * R,R);
repaint(1);
}
};
public final JLabel jlR = new JLabel("半径: ");
public final JLabel jlC = new JLabel("周长: ");
public final JLabel jlA = new JLabel("面积: ");
public final JLabel jlS = new JLabel("该半径球体的表面积: ");
public final JTextField jtR = new JTextField(6);
public final JTextField jtC = new JTextField(6);
public final JTextField jtA = new JTextField(6);
public final JTextField jtS = new JTextField(6);
public final JSlider slider =new JSlider(0,10,0);
public void CreateWindow(){
jf.setBounds(600, 300, 800, 600); //设定窗口的尺寸
jtR.setEditable(true);
jtR.setText("1");
slider.setMajorTickSpacing(5);
slider.setMinorTickSpacing(1);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
panel1.setVisible(true);
jf.setLayout(null);
jf.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
//Container c = jf.getContentPane(); //获取窗口的容器
panel1.add(jlR); //添加组件
panel1.add(jtR);
panel1.add(jlC);
panel1.add(jtC);
panel1.add(jlA);
panel1.add(jtA);
panel1.add(jlS);
panel1.add(jtS);
panel1.add(slider);
box.add(panel1);
box.add(panel2);
jf.add(box);
jf.setContentPane(box);
jf.setVisible(true); //使得该对象指向的窗体可见
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设定窗口被关闭时的默认动作
}
}
CONTROLLER
package CreateCircle.Controller;
import CreateCircle.Model.circle;
import CreateCircle.View.View;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
public class Controller{
public View window = new View();
public circle circle = new circle();
JPanel panel2 = new JPanel();
/*public void Paint(){
panel2 = new JPanel(){
public void paint(Graphics graphics){
super.paintComponent(graphics);
circle circle = new circle();
circle.setR(Double.parseDouble(window.jtR.getText()));
int R = (int) circle.getR();
graphics.drawOval(getWidth() / 2 - R,getHeight() / 2 - R,2 * R,2 * R);
repaint(1);
}
};
window.box.add(panel2);
}*/
public void design(){
window.jtR.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
try
{
Controller controller = new Controller();
circle.setR(Double.parseDouble(window.jtR.getText()));
window.jtC.setText(Double.toString(circle.getAround()));
window.jtA.setText(Double.toString(circle.getSize()));
window.jtS.setText(Double.toString(circle.getSurfaceSize()));
//Paint();
} catch (NumberFormatException numberFormatException) {
numberFormatException.printStackTrace();
}
}
@Override
public void removeUpdate(DocumentEvent e) {
try
{
circle.setR(Double.parseDouble(window.jtR.getText()));
window.jtC.setText(Double.toString(circle.getAround()));
window.jtA.setText(Double.toString(circle.getSize()));
window.jtS.setText(Double.toString(circle.getSurfaceSize()));
//Paint();
} catch (NumberFormatException numberFormatException) {
System.out.println("tip:正在使用滑尺/输入值格式错误");
}
}
@Override
public void changedUpdate(DocumentEvent e) {
try
{
circle.setR(Double.parseDouble(window.jtR.getText()));
window.jtC.setText(Double.toString(circle.getAround()));
window.jtA.setText(Double.toString(circle.getSize()));
window.jtS.setText(Double.toString(circle.getSurfaceSize()));
//Paint();
} catch (NumberFormatException numberFormatException) {
numberFormatException.printStackTrace();
}
}
});
window.slider.addChangeListener(e -> {
try{
circle.setR(window.slider.getValue());
window.jtR.setText(Double.toString(circle.getR()));
} catch (Exception exception) {
exception.printStackTrace();
}
});
}
}
DEMO
package CreateCircle;
import CreateCircle.Controller.Controller;
public class demo {
public static void main(String[] arg){
Controller controller = new Controller();
controller.window.CreateWindow();
controller.design();
}
}
总结:MVC基本实现了,但还是有些令人不满意的地方,比如画圆的函数没有放在controller里。我尝试在controller里用Paint方法实现画圆,但不知是什么原因,Paint方法生成的panel2始终不能正确的在程序中显示出来。