点此处查看 课程设计说明书
1.创建实体类:
// Circle.java
public class Circle {
private double r;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
@Override
public String toString() {
return "Circle [r=" + r + "]";
}
public Circle() {
super();
}
public Circle(double r) {
super();
this.r = r;
}
public double Circle_Space(){
double space=3.14*r*r;
return Math.round(space*100)/100.00;
}
public double Circle_Girth(){
double girth=2*3.14*r;
return Math.round(girth*100)/100.00;
}
}
// Column.java
public class Column {
private double r;
private double h;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
@Override
public String toString() {
return "Column [r=" + r + ", h=" + h + "]";
}
public Column() {
super();
}
public Column(double r, double h) {
super();
this.r = r;
this.h = h;
}
public double Column_bottom_Space(){
double space=3.14*r*r;
return Math.round(space*100)/100.00;
}
public double Column_Surface_Space(){
double space= 6.28*r*r+2*r*3.14*h;
return Math.round(space*100)/100.00;
}
public double Column_Volume(){
double volume= 3.14*r*r*h;
return Math.round(volume*100)/100.00;
}
}
// Rectangle.java
public class Rectangle {
private double height;
private double width;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public String toString() {
return "Rectangle [height=" + height + ", width=" + width + "]";
}
public Rectangle() {
super();
}
public Rectangle(double height, double width) {
super();
this.height = height;
this.width = width;
}
public double Rectangle_Girth(){
double girth=2*(height+width);
return Math.round(girth*100)/100.00;
}
public double Rectangle_Space(){
double space= height*width;
return Math.round(space*100)/100.00;
}
}
// Triangle.java
public class Triangle {
private double a;
private double b;
private double c;
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
@Override
public String toString() {
return "Triangle [a=" + a + ", b=" + b + ", c=" + c + "]";
}
public Triangle() {
super();
}
public Triangle(double a, double b, double c) {
super();
this.a = a;
this.b = b;
this.c = c;
}
public boolean isTriangle(){
if (a+b>c&&a+c>b&&b+c>a)
return true;
else
return false;
}
public double Triangle_Girth(){
return Math.round((a+b+c)*100)/100.00;
}
public double Triangle_Space(){
//System.out.println(a*b*c);
double p = (a+b+c) / 2;
double space=Math.pow(p*(p-a)*(p-b)*(p-c), 0.5);
return Math.round((space)*100)/100.00;
}
}
2.创建视图
// Main.java
import java.awt.*;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.s.pojo.Circle;
import com.s.pojo.Column;
import com.s.pojo.Rectangle;
import com.s.pojo.Triangle;
/**
* 默认保留两位小数
* @author Administrator
*
*/
public class Main extends JFrame implements ActionListener {
private JTextField jTextField1;
private JTextField jTextField2;
private JTextField jTextField3;
private JTextField jTextField4;
private JTextField flag;
private JPanel Panel,jp;
/**
* 组合框
* @return
*/
public JPanel getJComboBox() {
JPanel jPanel = new JPanel();
String str[] = {"请单击下拉列表选择",
"矩形的基本计算",
"圆的基本运算",
"三角形的基本运算",
"圆柱的基本运算",
"退出系统"};
JComboBox jComboBox = new JComboBox<>(str);
jComboBox.setPreferredSize(new Dimension(600, 30));
// 添加监听器,并重写方法
jComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
switch(jComboBox.getSelectedIndex()) {
case 0:
jp.setVisible(false);
Panel.removeAll();
Panel.add(getMainView());
Panel.repaint();
Panel.validate();
break;
case 1:
jp.setVisible(true);
Panel.removeAll();
Panel.add(getRectangleView());
Panel.repaint();
Panel.validate();
break;
case 2:
jp.setVisible(true);
Panel.removeAll();
Panel.add(getCircleView());
Panel.repaint();
Panel.validate();
break;
case 3:
jp.setVisible(true);
Panel.removeAll();
Panel.add(getTriangleView());
Panel.repaint();
Panel.validate();
break;
case 4:
jp.setVisible(true);
Panel.removeAll();
Panel.add(getColumnView());
Panel.repaint();
Panel.validate();
break;
case 5:
//退出系统
System.exit(0);
}
}
}
});
jPanel.add(jComboBox);
return jPanel;
}
/**
* 主面板
* @return
*/
public JPanel getMainView() {
JPanel jPanel = new JPanel();
return jPanel;
}
/**
* 获取键盘
* @return
*/
public JPanel getKeyBoard() {
JPanel jPanel = new JPanel();
jPanel.setLayout(new GridLayout(4,3));
JButton []jButton = new JButton[12];
jButton[0] = new JButton("0");
jButton[0].addActionListener(this);
jButton[1] = new JButton("1");
jButton[1].addActionListener(this);
jButton[2] = new JButton("2");
jButton[2].addActionListener(this);
jButton[3] = new JButton("3");
jButton[3].addActionListener(this);
jButton[4] = new JButton("4");
jButton[4].addActionListener(this);
jButton[5] = new JButton("5");
jButton[5].addActionListener(this);
jButton[6] = new JButton("6");
jButton[6].addActionListener(this);
jButton[7] = new JButton("7");
jButton[7].addActionListener(this);
jButton[8] = new JButton("8");
jButton[8].addActionListener(this);
jButton[9] = new JButton("9");
jButton[9].addActionListener(this);
jButton[10] = new JButton(".");
jButton[10].addActionListener(this);
jButton[11] = new JButton("BackSpace");
jButton[11].addActionListener(this);
jPanel.add(jButton[1]);
jPanel.add(jButton[2]);
jPanel.add(jButton[3]);
jPanel.add(jButton[4]);
jPanel.add(jButton[5]);
jPanel.add(jButton[6]);
jPanel.add(jButton[7]);
jPanel.add(jButton[8]);
jPanel.add(jButton[9]);
jPanel.add(jButton[0]);
jPanel.add(jButton[10]);
jPanel.add(jButton[11]);
jPanel.setPreferredSize(new Dimension(300, 240));
return jPanel;
}
/**
* 矩形的基本运算
* @return
*/
public JPanel getRectangleView() {
JPanel jPanel = new JPanel();
JLabel jLabel1 = new JLabel("请输入矩形的长:");
JLabel jLabel2 = new JLabel("请输入矩形的宽:");
JButton jButton1 = new JButton("计算结果:");
JButton jButton2 = new JButton("清空");
//jPanel.setLayout(new GridLayout(1,2));
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(5,1));
JPanel jp1 = new JPanel();
jp1.setLayout(new GridLayout(2,2));
jp1.add(jLabel1);
jp1.add(jTextField1);
jTextField1.setText("");
jp1.add(jLabel2);
jp1.add(jTextField2);
jTextField2.setText("");
JPanel jp11 = new JPanel();
jp11.add(jp1);
JPanel jp3 = new JPanel();
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
jp3.setLayout(new GridLayout(2,2));
jp3.add(new JLabel("矩形的周长为:"));
jp3.add(field1);
jp3.add(new JLabel("矩形的面积为:"));
jp3.add(field2);
JPanel jp33 = new JPanel();
jp33.add(jp3);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = jTextField4;
System.out.println(jTextField1.getText());
System.out.println(jTextField2.getText());
if(!jTextField1.getText().trim().equals("")&&
!jTextField1.getText().isEmpty() &&
!jTextField2.getText().trim().equals("") &&
!jTextField2.getText().trim().isEmpty()) {
Rectangle rectangle =new Rectangle();
rectangle.setHeight(Double.parseDouble(jTextField1.getText()));
rectangle.setWidth(Double.parseDouble(jTextField2.getText()));
field1.setText(String.valueOf(rectangle.Rectangle_Girth()));
field2.setText(String.valueOf(rectangle.Rectangle_Space()));
}
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = jTextField4;
jTextField1.setText("");
jTextField2.setText("");
field1.setText("");
field2.setText("");
}
});
JPanel jp2 = new JPanel();
jp2.add(jButton1);
jp2.add(jButton2);
jp.add(jp11);
jp.add(jp2);
jp.add(jp33);
jPanel.add(jp);
//jPanel.add(getKeyBoard());
return jPanel;
}
/**
* 圆的基本运算
* @return
*/
public JPanel getCircleView() {
JPanel jPanel = new JPanel();
JLabel jLabel1 = new JLabel("请输入圆的半径:");
JButton jButton1 = new JButton("计算结果:");
JButton jButton2 = new JButton("清空");
//jPanel.setLayout(new GridLayout(1,2));
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(6,1));
JPanel jp1 = new JPanel();
jp1.setLayout(new GridLayout(1,2));
jp1.add(jLabel1);
jp1.add(jTextField1);
jTextField1.setText("");
JPanel jp11 = new JPanel();
jp11.add(jp1);
JPanel jp3 = new JPanel();
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
jp3.setLayout(new GridLayout(2,2));
jp3.add(new JLabel("圆的周长为:"));
jp3.add(field1);
jp3.add(new JLabel("圆的面积为:"));
jp3.add(field2);
JPanel jp33 = new JPanel();
jp33.add(jp3);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = jTextField4;
System.out.println(jTextField1.getText());
if(!jTextField1.getText().trim().equals("")&&
!jTextField1.getText().isEmpty()) {
Circle circle = new Circle();
circle.setR(Double.parseDouble(jTextField1.getText()));
field1.setText(String.valueOf(circle.Circle_Girth()));
field2.setText(String.valueOf(circle.Circle_Space()));
}
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = jTextField4;
jTextField1.setText("");
field1.setText("");
field2.setText("");
}
});
JPanel jp2 = new JPanel();
jp2.add(jButton1);
jp2.add(jButton2);
jp.add(jp11);
jp.add(jp2);
jp.add(jp33);
jPanel.add(jp);
//jPanel.add(getKeyBoard());
return jPanel;
}
/**
* 三角形的基本运算
* @return
*/
public JPanel getTriangleView() {
JPanel jPanel = new JPanel();
JLabel jLabel1 = new JLabel("请输入三角形的边:");
JLabel jLabel2 = new JLabel("请输入三角形的边:");
JLabel jLabel3 = new JLabel("请输入三角形的边:");
JButton jButton1 = new JButton("计算结果:");
JButton jButton2 = new JButton("清空");
//jPanel.setLayout(new GridLayout(1,2));
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(4,1));
JPanel jp1 = new JPanel();
jp1.setLayout(new GridLayout(3,2));
jp1.add(jLabel1);
/**
*将JTextFiled初始化为空
*
*
*/
jp1.add(jTextField1);
jTextField1.setText("");
jp1.add(jLabel2);
jp1.add(jTextField2);
jTextField2.setText("");
jp1.add(jLabel3);
jp1.add(jTextField3);
jTextField3.setText("");
JPanel jp11 = new JPanel();
jp11.add(jp1);
JPanel jp3 = new JPanel();
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
jp3.setLayout(new GridLayout(2,2));
jp3.add(new JLabel("三角形的周长为:"));
jp3.add(field1);
jp3.add(new JLabel("三角形的面积为:"));
jp3.add(field2);
JPanel jp33 = new JPanel();
jp33.add(jp3);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = jTextField4;
System.out.println(jTextField1.getText());
System.out.println(jTextField2.getText());
if(!jTextField1.getText().trim().equals("")&&
!jTextField1.getText().isEmpty() &&
!jTextField2.getText().trim().equals("") &&
!jTextField2.getText().trim().isEmpty() &&
!jTextField3.getText().trim().equals("") &&
!jTextField3.getText().trim().isEmpty()) {
Triangle triangle = new Triangle();
triangle.setA(Double.parseDouble(jTextField1.getText()));
triangle.setB(Double.parseDouble(jTextField2.getText()));
triangle.setC(Double.parseDouble(jTextField3.getText()));
if(!triangle.isTriangle()){
JOptionPane.showMessageDialog(null, "非三角形!", "标题【出错啦】", JOptionPane.ERROR_MESSAGE);
}
else{
field1.setText(String.valueOf(triangle.Triangle_Girth()));
field2.setText(String.valueOf(triangle.Triangle_Space()));
}
}
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = jTextField4;
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
field1.setText("");
field2.setText("");
}
});
JPanel jp2 = new JPanel();
jp2.add(jButton1);
jp2.add(jButton2);
jp.add(jp11);
jp.add(jp2);
jp.add(jp33);
jPanel.add(jp);
//jPanel.add(getKeyBoard());
return jPanel;
}
/**
* 圆柱的基本运算
* @return
*/
public JPanel getColumnView() {
JPanel jPanel = new JPanel();
JLabel jLabel1 = new JLabel("请输入圆柱的半径:");
JLabel jLabel2 = new JLabel("请输入圆柱的高:");
JButton jButton1 = new JButton("计算结果:");
JButton jButton2 = new JButton("清空");
//jPanel.setLayout(new GridLayout(1,2));
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(4,1));
JPanel jp1 = new JPanel();
jp1.setLayout(new GridLayout(2,2));
jp1.add(jLabel1);
jp1.add(jTextField1);
jTextField1.setText("");
jp1.add(jLabel2);
jp1.add(jTextField2);
jTextField2.setText("");
JPanel jp11 = new JPanel();
jp11.add(jp1);
JPanel jp3 = new JPanel();
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
JTextField field3 = new JTextField();
jp3.setLayout(new GridLayout(3,2));
jp3.add(new JLabel("圆柱的底面积为:"));
jp3.add(field1);
jp3.add(new JLabel("圆柱的表面积为:"));
jp3.add(field2);
jp3.add(new JLabel("圆柱的体积为:"));
jp3.add(field3);
JPanel jp33 = new JPanel();
jp33.add(jp3);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = jTextField4;
System.out.println(jTextField1.getText());
System.out.println(jTextField2.getText());
if(!jTextField1.getText().trim().equals("")&&
!jTextField1.getText().isEmpty() &&
!jTextField2.getText().trim().equals("") &&
!jTextField2.getText().trim().isEmpty()) {
Column column= new Column();
column.setR(Double.parseDouble(jTextField1.getText()));
column.setH(Double.parseDouble(jTextField2.getText()));
field1.setText(String.valueOf(column.Column_bottom_Space()));
field2.setText(String.valueOf(column.Column_Surface_Space()));
field3.setText(String.valueOf(column.Column_Volume()));
}
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = jTextField4;
jTextField1.setText("");
jTextField2.setText("");
field1.setText("");
field2.setText("");
field3.setText("");
}
});
JPanel jp2 = new JPanel();
jp2.add(jButton1);
jp2.add(jButton2);
jp.add(jp11);
jp.add(jp2);
jp.add(jp33);
jPanel.add(jp);
//jPanel.add(getKeyBoard());
return jPanel;
}
/**
* 构造方法
*/
public Main() {
jTextField1 = new JTextField();
jTextField2 = new JTextField();
jTextField3 = new JTextField();
jTextField4 = new JTextField();
Panel = new JPanel();
//焦点监听器
jTextField1.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
}
@Override
public void focusGained(FocusEvent e) {
flag = jTextField1;
}
});
jTextField2.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
}
@Override
public void focusGained(FocusEvent e) {
flag = jTextField2;
}
});
jTextField3.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
}
@Override
public void focusGained(FocusEvent e) {
flag = jTextField3;
}
});
flag = jTextField4;
Panel.add(getMainView());
this.setLayout(new BorderLayout());
this.add(getJComboBox(),"North");
this.add(Panel,"Center");
jp = new JPanel();
jp.add(getKeyBoard());
jp.setVisible(false);
this.add(jp,"East");
this.setResizable(false); //不可改变大小
this.setTitle("常见几何图形的计算器"); //设置标题
this.setSize(600, 320);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((size.width-this.getWidth())/2,(size.height-this.getHeight())/2);//设置位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
// 点击监听器
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="1"){
if(flag.getText().equals("0")) {
flag.setText("1");
}else {
flag.setText(flag.getText()+"1");
}
} else if(e.getActionCommand()=="2"){
if(flag.getText().equals("0")) {
flag.setText("2");
}else {
flag.setText(flag.getText()+"2");
}
} else if(e.getActionCommand()=="3"){
if(flag.getText().equals("0")) {
flag.setText("3");
}else {
flag.setText(flag.getText()+"3");
}
} else if(e.getActionCommand()=="4"){
if(flag.getText().equals("0")) {
flag.setText("4");
}else {
flag.setText(flag.getText()+"4");
}
} else if(e.getActionCommand()=="5"){
if(flag.getText().equals("0")) {
flag.setText("5");
}else {
flag.setText(flag.getText()+"5");
}
} else if(e.getActionCommand()=="6"){
if(flag.getText().equals("0")) {
flag.setText("6");
}else {
flag.setText(flag.getText()+"6");
}
} else if(e.getActionCommand()=="7"){
if(flag.getText().equals("0")) {
flag.setText("7");
}else {
flag.setText(flag.getText()+"7");
}
} else if(e.getActionCommand()=="8"){
if(flag.getText().equals("0")) {
flag.setText("8");
}else {
flag.setText(flag.getText()+"8");
}
} else if(e.getActionCommand()=="9"){
if(flag.getText().equals("0")) {
flag.setText("9");
}else {
flag.setText(flag.getText()+"9");
}
} else if(e.getActionCommand()=="0"){
if(!flag.getText().equals("0")) {
flag.setText(flag.getText()+"0");
}
} else if(e.getActionCommand()=="."){
if(flag.getText().equals("")) {
flag.setText(flag.getText()+"0.");
}
if(flag.getText().indexOf(".") == -1) {
flag.setText(flag.getText()+".");
}
} else if(e.getActionCommand()=="BackSpace"){
if(!flag.getText().equals("")) {
flag.setText(flag.getText().substring(0, flag.getText().length()-1));
}
}
}
public static void main(String[] args) {
new Main();
}
}