首页 > 代码库 > 第4次作业类测试代码+085+潘亭

第4次作业类测试代码+085+潘亭

一、类图设计如下

技术分享

二、界面如下

技术分享

功能演示

  1、输入错误

  技术分享

  2、不构成三角形

  技术分享

  3、一般三角形

  技术分享

  面积默认保留两位小数

  4、直角三角形

  技术分享

  5、等腰三角形

  技术分享

  6、等边三角形

  技术分享

  7、cancel演示

  技术分享

  防止程序崩溃,默认重置为0

三、代码部分

  1、Triangle类

技术分享
 1 package visualTriangle;
 2 
 3 public class Triangle {
 4     
 5     //judge the fields
 6     public static boolean Check(int num) 
 7     {
 8         if(num<1 || num>100)
 9         {
10             return false;
11         }
12         return true;
13     }
14     
15     //judge shape
16     public static String triangle(int side1,int side2,int side3)
17     {
18         int temp;
19         if(side1>side2) 
20             {temp=side1;side1=side2;side2=temp;}
21         if(side2>side3) 
22             {temp=side2;side2=side3;side3=temp;}
23         
24         String result="三角形";
25         if(side1+side2<=side3)
26             return "不构成"+result;
27         else if(side1==side2 && side2==side3)
28             return "等边"+result;
29         else if(side1==side2 || side2==side3 || side1==side3)
30             return "等腰"+result;
31         else if(side1*side1+side2*side2==side3*side3)
32             return "直角"+result;
33         else
34             return "普通"+result;
35     }
36     
37     //calculate Area
38     public static float triangleArea(int side1,int side2,int side3)
39     {
40         double p=(side1+side2+side3)*0.5;
41         return (float)Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
42     }
43     //calculate Grith
44     public static int perimeter(int side1,int side2,int side3)
45     {
46         return side1+side2+side3;
47     }
48 }
View Code

   2、TriangleUI类

package visualTriangle; import java.awt.Color; import java.awt.Font; import java.awt.FontFormatException; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.management.relation.RelationNotification; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; public class TriangleUI { public static void initUI() { JFrame frame = new JFrame("Triangle"); frame.setLayout(null); //labels JLabel title = new JLabel("请输入三角形的三条边:"); JLabel lSide1 = new JLabel("a:"); JLabel lSide2 = new JLabel("b:"); JLabel lSide3 = new JLabel("c:"); JLabel lShape = new JLabel("这个三角形是:"); JLabel lArea = new JLabel("面积是:"); JLabel lGrith = new JLabel("周长是:"); //input test fields JTextField tSide1 = new JTextField(); JTextField tSide2 = new JTextField(); JTextField tSide3 = new JTextField(); JTextField tShape = new JTextField(); JTextField tArea = new JTextField(); JTextField tGrith = new JTextField(); //buttons JButton bSubmit = new JButton("Ok"); JButton bCancel = new JButton("Cancel"); //ok listener bSubmit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==bSubmit) { int side1=Integer.parseInt(tSide1.getText()); int side2=Integer.parseInt(tSide2.getText()); int side3=Integer.parseInt(tSide3.getText()); if(Triangle.Check(side1) && Triangle.Check(side2) && Triangle.Check(side3)) { String re=Triangle.triangle(side1, side2, side3); tShape.setText(re); if("不构成三角形".equals(re)) { tArea.setText("无"); tGrith.setText("无"); } else { float area=Triangle.triangleArea(side1, side2, side3); DecimalFormat df = new DecimalFormat("#.00"); tArea.setText(df.format(area)); tGrith.setText(""+Triangle.perimeter(side1, side2, side3)); } } else { JOptionPane.showMessageDialog(null, "输入有误,请重请输入正确整数"); tSide1.setText("0"); tSide2.setText("0"); tSide3.setText("0"); } } } }); //cancel listener bCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==bCancel) { tSide1.setText("0"); tSide2.setText("0"); tSide3.setText("0"); tShape.setText(""); tArea.setText(""); tGrith.setText(""); } } }); Font ft = new Font("宋体",Font.BOLD,24); //labels settings title.setFont(ft); title.setBounds(62, 62, 500, 25); lSide1.setFont(ft); lSide1.setBounds(80, 124, 50, 25); lSide2.setFont(ft); lSide2.setBounds(230, 124, 50, 25); lSide3.setFont(ft); lSide3.setBounds(380, 124, 50, 25); lShape.setFont(ft); lShape.setBounds(62, 248, 200, 25); lArea.setFont(ft); lArea.setBounds(62, 310, 150, 25); lGrith.setFont(ft); lGrith.setBounds(270, 310, 150, 25); //textFileds settings tSide1.setBounds(130, 124, 50, 25); tSide2.setBounds(280, 124, 50, 25); tSide3.setBounds(430, 124, 50, 25); tShape.setBounds(230, 248, 150, 25); tShape.setFont(ft); tShape.setEnabled(false); tArea.setBounds(150, 310, 100, 25); tArea.setFont(ft); tArea.setEnabled(false); tGrith.setBounds(360, 310, 100, 25); tGrith.setFont(ft); tGrith.setEnabled(false); //button settings bSubmit.setBounds(180, 187, 80, 31); bCancel.setBounds(320, 187, 80, 31); //add to frame frame.add(title); frame.add(lSide1);frame.add(lSide2);frame.add(lSide3); frame.add(tSide1);frame.add(tSide2);frame.add(tSide3); frame.add(bSubmit);frame.add(bCancel); frame.add(lShape);frame.add(tShape); frame.add(lArea);frame.add(tArea); frame.add(lGrith);frame.add(tGrith); //frame configures frame.setSize(600, 490); frame.setLocation(300,300); frame.setVisible(true); } }
技术分享
  1 package visualTriangle;
  2 
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.awt.FontFormatException;
  6 import java.awt.GridLayout;
  7 import java.awt.event.ActionEvent;
  8 import java.awt.event.ActionListener;
  9 import java.text.DecimalFormat;
 10 
 11 import javax.management.relation.RelationNotification;
 12 import javax.swing.JButton;
 13 import javax.swing.JFrame;
 14 import javax.swing.JLabel;
 15 import javax.swing.JOptionPane;
 16 import javax.swing.JTextField;
 17 
 18 public class TriangleUI {
 19     
 20     public static void initUI() {
 21         JFrame frame = new JFrame("Triangle");
 22         frame.setLayout(null);
 23         //labels
 24         JLabel title = new JLabel("请输入三角形的三条边:");
 25         JLabel lSide1 = new JLabel("a:");
 26         JLabel lSide2 = new JLabel("b:");
 27         JLabel lSide3 = new JLabel("c:");
 28         JLabel lShape = new JLabel("这个三角形是:");
 29         JLabel lArea = new JLabel("面积是:");
 30         JLabel lGrith = new JLabel("周长是:");    
 31         //input test fields
 32         JTextField tSide1 = new JTextField();
 33         JTextField tSide2 = new JTextField();
 34         JTextField tSide3 = new JTextField();
 35         JTextField tShape = new JTextField();
 36         JTextField tArea = new JTextField();
 37         JTextField tGrith = new JTextField();    
 38         //buttons
 39         JButton bSubmit = new JButton("Ok");
 40         JButton bCancel = new JButton("Cancel");
 41         //ok listener
 42         bSubmit.addActionListener(new ActionListener() 
 43         {
 44             @Override
 45             public void actionPerformed(ActionEvent e) 
 46             {
 47                 if(e.getSource()==bSubmit)
 48                 {
 49                     int side1=Integer.parseInt(tSide1.getText());
 50                     int side2=Integer.parseInt(tSide2.getText());
 51                     int side3=Integer.parseInt(tSide3.getText());
 52                     
 53                     if(Triangle.Check(side1) && Triangle.Check(side2) && Triangle.Check(side3))
 54                     {
 55                         String re=Triangle.triangle(side1, side2, side3);
 56                         tShape.setText(re);
 57                         if("不构成三角形".equals(re))
 58                         {
 59                             tArea.setText("无");
 60                             tGrith.setText("无");
 61                         }
 62                         else
 63                         {
 64                             float area=Triangle.triangleArea(side1, side2, side3);
 65                             DecimalFormat df = new DecimalFormat("#.00");
 66                             tArea.setText(df.format(area));
 67                             tGrith.setText(""+Triangle.perimeter(side1, side2, side3));
 68                         }        
 69                     }
 70                     else
 71                     {
 72                          JOptionPane.showMessageDialog(null, "输入有误,请重请输入正确整数");
 73                          tSide1.setText("0");
 74                          tSide2.setText("0");
 75                          tSide3.setText("0");
 76                     }
 77                 }            
 78             }
 79         });
 80         //cancel listener
 81         bCancel.addActionListener(new ActionListener() 
 82         {
 83             @Override
 84             public void actionPerformed(ActionEvent e) 
 85             {
 86                 if(e.getSource()==bCancel)
 87                 {
 88                     tSide1.setText("0");
 89                     tSide2.setText("0");
 90                     tSide3.setText("0");
 91                     tShape.setText("");
 92                     tArea.setText("");
 93                     tGrith.setText("");
 94                 }
 95                 
 96             }
 97         });
 98         
 99         Font ft = new Font("宋体",Font.BOLD,24);
100         //labels settings
101         title.setFont(ft);
102         title.setBounds(62, 62, 500, 25);
103         lSide1.setFont(ft);
104         lSide1.setBounds(80, 124, 50, 25);
105         lSide2.setFont(ft);
106         lSide2.setBounds(230, 124, 50, 25);
107         lSide3.setFont(ft);
108         lSide3.setBounds(380, 124, 50, 25);
109         lShape.setFont(ft);
110         lShape.setBounds(62, 248, 200, 25);
111         lArea.setFont(ft);
112         lArea.setBounds(62, 310, 150, 25);
113         lGrith.setFont(ft);
114         lGrith.setBounds(270, 310, 150, 25);
115         
116         //textFileds settings
117         tSide1.setBounds(130, 124, 50, 25);
118         tSide2.setBounds(280, 124, 50, 25);
119         tSide3.setBounds(430, 124, 50, 25);
120         tShape.setBounds(230, 248, 150, 25);
121         tShape.setFont(ft);
122         tShape.setEnabled(false);
123         tArea.setBounds(150, 310, 100, 25);
124         tArea.setFont(ft);
125         tArea.setEnabled(false);
126         tGrith.setBounds(360, 310, 100, 25);
127         tGrith.setFont(ft);
128         tGrith.setEnabled(false); 
129         //button settings
130         bSubmit.setBounds(180, 187, 80, 31);
131         bCancel.setBounds(320, 187, 80, 31);        
132         //add to frame
133         frame.add(title);
134         frame.add(lSide1);frame.add(lSide2);frame.add(lSide3);
135         frame.add(tSide1);frame.add(tSide2);frame.add(tSide3);
136         frame.add(bSubmit);frame.add(bCancel);
137         frame.add(lShape);frame.add(tShape);
138         frame.add(lArea);frame.add(tArea);
139         frame.add(lGrith);frame.add(tGrith);
140         //frame configures
141         frame.setSize(600, 490);
142         frame.setLocation(300,300);
143         frame.setVisible(true);
144     }
145     
146 }
View Code

 

第4次作业类测试代码+085+潘亭