使用开闭原则、MVC结构、GUI(可用选项卡窗格P214【例11-4】),求底为三角形、梯形、园的柱形的体积。
时间: 2023-06-17 17:04:24 浏览: 206
根据开闭原则,我们应该设计一个可扩展的程序,以便今后可能添加更多的形状。因此,我们可以定义一个抽象的 Shape 类,它有一个计算体积的抽象方法,每个具体的形状都可以继承这个类并实现这个方法。
MVC结构中,我们将程序分为三个部分:模型、视图和控制器。模型负责处理计算逻辑;视图负责显示界面;控制器负责协调模型和视图之间的交互。
GUI界面我们可以使用选项卡窗格来展示不同的形状,用户可以选择要计算的形状并输入相应的参数,程序将计算出体积并显示在界面上。
以下是Java代码实现:
Shape.java
```java
public abstract class Shape {
public abstract double getVolume();
}
```
TrianglePrism.java
```java
public class TrianglePrism extends Shape {
private double base;
private double height;
private double length;
public TrianglePrism(double base, double height, double length) {
this.base = base;
this.height = height;
this.length = length;
}
public double getVolume() {
return 0.5 * base * height * length;
}
}
```
TrapezoidPrism.java
```java
public class TrapezoidPrism extends Shape {
private double base1;
private double base2;
private double height;
private double length;
public TrapezoidPrism(double base1, double base2, double height, double length) {
this.base1 = base1;
this.base2 = base2;
this.height = height;
this.length = length;
}
public double getVolume() {
return 0.5 * (base1 + base2) * height * length;
}
}
```
Cylinder.java
```java
public class Cylinder extends Shape {
private double radius;
private double height;
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double getVolume() {
return Math.PI * radius * radius * height;
}
}
```
View.java
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class View {
private JFrame frame;
private JTabbedPane tabbedPane;
private JPanel trianglePanel;
private JPanel trapezoidPanel;
private JPanel cylinderPanel;
private JTextField triangleBaseField;
private JTextField triangleHeightField;
private JTextField triangleLengthField;
private JTextField trapezoidBase1Field;
private JTextField trapezoidBase2Field;
private JTextField trapezoidHeightField;
private JTextField trapezoidLengthField;
private JTextField cylinderRadiusField;
private JTextField cylinderHeightField;
private JLabel triangleVolumeLabel;
private JLabel trapezoidVolumeLabel;
private JLabel cylinderVolumeLabel;
public View() {
frame = new JFrame("Calculate Volume");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbedPane = new JTabbedPane();
trianglePanel = new JPanel();
trianglePanel.setLayout(new GridLayout(4, 2));
trianglePanel.add(new JLabel("Base:"));
triangleBaseField = new JTextField();
trianglePanel.add(triangleBaseField);
trianglePanel.add(new JLabel("Height:"));
triangleHeightField = new JTextField();
trianglePanel.add(triangleHeightField);
trianglePanel.add(new JLabel("Length:"));
triangleLengthField = new JTextField();
trianglePanel.add(triangleLengthField);
JButton triangleButton = new JButton("Calculate");
triangleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double base = Double.parseDouble(triangleBaseField.getText());
double height = Double.parseDouble(triangleHeightField.getText());
double length = Double.parseDouble(triangleLengthField.getText());
TrianglePrism triangle = new TrianglePrism(base, height, length);
triangleVolumeLabel.setText("Volume: " + triangle.getVolume());
}
});
trianglePanel.add(triangleButton);
triangleVolumeLabel = new JLabel("Volume:");
trianglePanel.add(triangleVolumeLabel);
tabbedPane.addTab("Triangle", trianglePanel);
trapezoidPanel = new JPanel();
trapezoidPanel.setLayout(new GridLayout(4, 2));
trapezoidPanel.add(new JLabel("Base1:"));
trapezoidBase1Field = new JTextField();
trapezoidPanel.add(trapezoidBase1Field);
trapezoidPanel.add(new JLabel("Base2:"));
trapezoidBase2Field = new JTextField();
trapezoidPanel.add(trapezoidBase2Field);
trapezoidPanel.add(new JLabel("Height:"));
trapezoidHeightField = new JTextField();
trapezoidPanel.add(trapezoidHeightField);
trapezoidPanel.add(new JLabel("Length:"));
trapezoidLengthField = new JTextField();
trapezoidPanel.add(trapezoidLengthField);
JButton trapezoidButton = new JButton("Calculate");
trapezoidButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double base1 = Double.parseDouble(trapezoidBase1Field.getText());
double base2 = Double.parseDouble(trapezoidBase2Field.getText());
double height = Double.parseDouble(trapezoidHeightField.getText());
double length = Double.parseDouble(trapezoidLengthField.getText());
TrapezoidPrism trapezoid = new TrapezoidPrism(base1, base2, height, length);
trapezoidVolumeLabel.setText("Volume: " + trapezoid.getVolume());
}
});
trapezoidPanel.add(trapezoidButton);
trapezoidVolumeLabel = new JLabel("Volume:");
trapezoidPanel.add(trapezoidVolumeLabel);
tabbedPane.addTab("Trapezoid", trapezoidPanel);
cylinderPanel = new JPanel();
cylinderPanel.setLayout(new GridLayout(3, 2));
cylinderPanel.add(new JLabel("Radius:"));
cylinderRadiusField = new JTextField();
cylinderPanel.add(cylinderRadiusField);
cylinderPanel.add(new JLabel("Height:"));
cylinderHeightField = new JTextField();
cylinderPanel.add(cylinderHeightField);
JButton cylinderButton = new JButton("Calculate");
cylinderButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double radius = Double.parseDouble(cylinderRadiusField.getText());
double height = Double.parseDouble(cylinderHeightField.getText());
Cylinder cylinder = new Cylinder(radius, height);
cylinderVolumeLabel.setText("Volume: " + cylinder.getVolume());
}
});
cylinderPanel.add(cylinderButton);
cylinderVolumeLabel = new JLabel("Volume:");
cylinderPanel.add(cylinderVolumeLabel);
tabbedPane.addTab("Cylinder", cylinderPanel);
frame.add(tabbedPane);
frame.pack();
frame.setVisible(true);
}
}
```
Controller.java
```java
public class Controller {
private View view;
public Controller(View view) {
this.view = view;
}
public static void main(String[] args) {
View view = new View();
Controller controller = new Controller(view);
}
}
```
阅读全文