这是本学期开学的第一个作业,设计图形界面,实现圆形、矩形、三角形的周长和面积计算,要求涉及抽象、封装、继承、多态的知识点,并且每个图形在计算时都有自己的界面。由于不是专业学java的,代码写的简单粗暴,界面也做的比较丑,仅供参考*-*。
先放上代码运行结果(有点花里胡哨):
下面是代码实现:
将图形抽象成一个类shapes作为父类,在里面定义计算周长和面积的方法,其他图形直接调用(继承)父类的方法进行实现,这样就同时涉及了抽象、封装、继承、多态的知识。
package Frame;
//图形父类
public abstract class shapes {
abstract double perimeterCalculation(); //计算周长的方法
abstract double areaCalculation(); //计算面积的方法
}
package Frame;
//子类圆形
import javax.swing.JLabel;
public class cricular extends shapes{ //继承父类shapes
double radius;
cricular(double radius){
this.radius=radius;
}
public cricular(JLabel labName1) {
// TODO Auto-generated constructor stub
}
double perimeterCalculation(){ //重写方法,实现多态
return (2*radius*3.14);
}
double areaCalculation(){
return (radius*radius*3.14); //重写方法,实现多态
}
}
package Frame;
//子类矩形
import javax.swing.JLabel;
public class rectangles extends shapes{ //继承父类shapes
double edge1,edge2;
rectangles(double edge1,double edge2){
this.edge1=edge1;
this.edge2=edge2;
}
public rectangles(JLabel labName1) {
// TODO Auto-generated constructor stub
}
double perimeterCalculation(){
return (2*edge1+2*edge2);
}
double areaCalculation(){
return (edge1*edge2);
}
}
package Frame;
import javax.swing.JLabel;
public class triangles extends shapes{ //继承父类
double edg1,edg2,edg3;
triangles(double edg1,double edg2,double edg3){
this.edg1 = edg1;
this.edg2 = edg2;
this.edg3 = edg3;
}
public triangles(JLabel labName1) {
// TODO Auto-generated constructor stub
}
double perimeterCalculation(){
double s = (edg1 + edg2 + edg3) / 2;
return (Math.sqrt(s*(s-edg1)*(s-edg2)*(s-edg3))); //海伦公式,只需要知道三角形的三条边就能计算面积
}
double areaCalculation(){
return (edg1 + edg2 + edg3);
}
}
下面是主界面代码:
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
frame.setTitle("计算图形的面积和周长");
frame.setBounds(400,200,600,400);
frame.setLayout(null);
JLabel l = new JLabel("请选择图形");
l.setBounds(150, 20, 150, 30);
JLabel l1 = new JLabel(); //Label控件,用于绑定背景图片,使得整个图片平铺在窗体上
l1.setBounds(0, 0, 420, 320);
frame.setSize(420, 320);
//需要的图片
ImageIcon i1 = new ImageIcon("D:/TIM图片20200304215114.jpg");
i1.setImage(i1.getImage().getScaledInstance(420, 320, 1000));
l1.setIcon(i1);
//设置按钮
JButton btn1 = new JButton("圆形");
JButton btn2 = new JButton("矩形");
JButton btn3 = new JButton("三角形");
btn1.setBounds(120, 60, 120, 50);
btn2.setBounds(120, 130, 120, 50);
btn3.setBounds(120, 200, 120, 50);
//将按钮添加到窗口
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(l);
frame.add(l1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); //将窗口显现出来,改成false,运行后将看不到窗口
计算圆形时的窗口:
//圆形
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JFrame frame1 = new JFrame();
frame1.setLayout(null); //frame1是指圆形这个窗口,矩形、三角形的窗口就不能再用同样的名字,可以改成frame2,frame3之类的
frame1.setSize(500, 300);
frame1.setTitle("计算圆形的面积和周长");
frame1.setBounds(500,400,500,300);
//设置背景图片
ImageIcon img = new ImageIcon("D:/101.jpg"); //这样设置的背景图不会平铺整个窗口,会根据窗口大小截取部分作为背景
JLabel L2 = new JLabel(img);
frame1.getLayeredPane().setLayout(null);
L2.setBounds(0,0,500,300);
frame1.getLayeredPane().add(L2);
//设置面板
JPanel panel = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
//设置面板的尺寸和位置
panel1.setSize(400, 200);
panel2.setSize(400, 200);
panel3.setSize(400, 200);
panel1.setLocation(55,30);
panel2.setLocation(55,130);
panel3.setLocation(55,180);
//设置标签的字体、大小、位置
JLabel labName1 = new JLabel("请输入圆的半径:",SwingConstants.LEFT);
Font font = new Font("宋体",Font.PLAIN, 15);
labName1.setFont(font);
JLabel labName2 = new JLabel("圆形的周长:",SwingConstants.LEFT);
labName2.setFont(font);
JLabel labName3 = new JLabel("圆形的面积:",SwingConstants.LEFT);
labName3.setFont(font);
labName1.setBounds(10,30,150,50);
L2.add(labName1);
labName2.setBounds(20,130,150,50);
L2.add(labName2);
labName3.setBounds(20,180,150,50);
L2.add(labName3);
//创建文本框
JTextField textField1 = new JTextField(15);
JTextField textField2 = new JTextField(15);
JTextField textField3 = new JTextField(15);
textField1.setFont(new Font(null,Font.PLAIN, 20));
textField2.setFont(new Font(null,Font.PLAIN, 20));
textField3.setFont(new Font(null,Font.PLAIN, 20));
panel1.add(textField1);
panel.add(panel1);
panel2.add(textField2);
panel.add(panel2);
panel3.add(textField3);
panel.add(panel3);
//panel.add(panel1);
panel.setVisible(true);
//创建按钮
JButton btn21 = new JButton("计算");
JButton btn23 = new JButton("清空");
btn21.setBounds(140, 85, 80, 30);
btn23.setBounds(290, 85, 80, 30);
panel.add(btn21);
panel.add(btn23);
panel.setLayout(null);
panel.setOpaque(false);
frame1.setContentPane(panel);
frame1.setVisible(true);
//按钮btn21鼠标单击事件
btn21.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {;
double crs = Double.parseDouble(textField1.getText());
cricular cricularobject = new cricular(crs);
textField2.setText(String.format("%.2f",cricularobject.perimeterCalculation()));
textField3.setText(String.format("%.2f",cricularobject.areaCalculation()));
}
});
//按钮btn23鼠标单击事件
btn23.addActionListener(ee -> {
textField1.setText("");
textField2.setText("");
textField3.setText("");
});
}
});
矩形、三角形的窗口代码跟圆形的差不多,就不再赘述,只需要多添加一至两个Label标签和TextField标签并实现即可。