第4次作业类测试代码+105032014118+陈元可

类测试代码的具体要求如下:

  设计三角形完整程序

     已经完成的方法是:  String triangle(int a,int b,int c) 

     现在要求继续增加新的功能:

     建立界面,至少包含以下元素,但不限于此:

 

完成面积的方法:float triangleArea(int a,int b,int c) ,完成周长的方法:int perimeter(int a,int b,int c)

一、类图

 

二、功能界面及代码

TriangleFrame界面代码:

  1 package test01;
  2 
  3 import java.awt.EventQueue;
  4 import javax.swing.JFrame;
  5 import javax.swing.JOptionPane;
  6 import javax.swing.JPanel;
  7 import java.awt.BorderLayout;
  8 import javax.swing.JButton;
  9 import javax.swing.JTextField;
 10 import java.awt.Font;
 11 import javax.swing.UIManager;
 12 import java.awt.event.MouseAdapter;
 13 import java.awt.event.MouseEvent;
 14 import javax.swing.JLabel;
 15 
 16 public class TriangleFrame {
 17 
 18     private JFrame frm;
 19     private JTextField textField_b;
 20     private JTextField textField_a;
 21     private JTextField textField_c;
 22     private JTextField textField_is;
 23     private JTextField textField_area;
 24     private JTextField textField_peri;
 25 
 26 
 27     public static void main(String[] args) {
 28         EventQueue.invokeLater(new Runnable() {
 29             public void run() {
 30                 try {
 31                     TriangleFrame window = new TriangleFrame();
 32                     window.frm.setVisible(true);
 33                 } catch (Exception e) {
 34                     e.printStackTrace();
 35                 }
 36             }
 37         });
 38     }
 39 
 40 
 41     public TriangleFrame() {
 42         initialize();
 43     }
 44 
 45 
 46     private void initialize() {
 47 
 48         frm = new JFrame();
 49         frm.setBackground(UIManager.getColor("Button.background"));
 50         frm.setFont(new Font("Dialog", Font.PLAIN, 12));
 51         frm.setForeground(UIManager.getColor("Button.background"));
 52         frm.setTitle("三角形计算程序");
 53         frm.setBounds(400, 300, 400, 250);
 54         frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 55         
 56         JPanel panel = new JPanel();
 57         panel.setBounds(0, 0, 400, 250);
 58         panel.setBackground(UIManager.getColor("Button.background"));
 59         frm.getContentPane().add(panel, BorderLayout.CENTER);
 60         
 61         JLabel label_title = new JLabel("请输入三角形的三条边:");
 62         label_title.setBackground(UIManager.getColor("Button.background"));
 63         label_title.setFont(new Font("宋体", Font.BOLD, 16));
 64         label_title.setBounds(20, 100, 200, 20);
 65         panel.add(label_title);
 66         
 67         JPanel panel_1 = new JPanel();
 68         panel.add(panel_1);
 69         
 70         JLabel lblA = new JLabel("a:");
 71         panel_1.add(lblA);
 72         lblA.setFont(new Font("宋体", Font.BOLD, 20));
 73         lblA.setSize(30, 30);
 74         
 75         textField_a = new JTextField();
 76         panel_1.add(textField_a);
 77         textField_a.setColumns(6);
 78         
 79         JLabel lblB = new JLabel(" b:");
 80         panel_1.add(lblB);
 81         lblB.setFont(new Font("宋体", Font.BOLD, 20));
 82         lblB.setSize(30, 30);
 83         
 84         textField_b = new JTextField();
 85         panel_1.add(textField_b);
 86         textField_b.setColumns(6);
 87         
 88         JLabel lblC = new JLabel(" c:");
 89         panel_1.add(lblC);
 90         lblC.setFont(new Font("宋体", Font.BOLD, 20));
 91         lblC.setSize(30, 30);
 92         
 93         textField_c = new JTextField();
 94         panel_1.add(textField_c);
 95         textField_c.setColumns(6);
 96         
 97         JPanel panel_btn = new JPanel();
 98         panel_btn.setSize(200, 80);
 99         panel.add(panel_btn);
100         
101         JButton btnOK = new JButton("OK");
102         btnOK.setSize(50, 50);
103         btnOK.setFont(new Font("宋体", Font.PLAIN, 20));
104         panel_btn.add(btnOK);
105         
106         JButton btnCancle = new JButton("Cancel");
107         btnOK.setSize(50, 50);
108         btnCancle.setFont(new Font("宋体", Font.PLAIN, 20));
109         panel_btn.add(btnCancle);
110         btnCancle.addMouseListener(new MouseAdapter() {
111             @Override
112             public void mouseClicked(MouseEvent arg0) {
113                 Object object = arg0.getSource();
114                 if(object == btnCancle){
115                     textField_a.setText("");
116                     textField_b.setText("");
117                     textField_c.setText("");
118                     textField_is.setText("");
119                     textField_area.setText("");
120                     textField_peri.setText("");
121                 }
122             }
123         });
124         btnOK.addMouseListener(new MouseAdapter() {
125             @Override
126             public void mouseClicked(MouseEvent arg0) {
127                 Object object = arg0.getSource();
128                 if(object == btnOK){
129                     int i_a,i_b,i_c;
130                     String a=textField_a.getText();
131                     String b=textField_b.getText();
132                     String c=textField_c.getText();
133                     if(test01.isNumber(a) && test01.isNumber(b) && test01.isNumber(c))
134                     {
135                          i_a = Integer.parseInt(a);
136                          i_b = Integer.parseInt(b);
137                          i_c = Integer.parseInt(c);
138                          if (!(test01.triangle(i_a, i_b, i_c)).equals("边的值不在范围内")&&!(test01.triangle(i_a, i_b, i_c)).equals("不构成三角形")) {
139                              String result=test01.triangle(i_a, i_b, i_c);
140                              textField_is.setText(result);
141                              String area=String.valueOf(test01.triangleArea(i_a, i_b, i_c));
142                              textField_area.setText(area);
143                              String peri=String.valueOf(test01.perimeter(i_a, i_b, i_c));
144                              textField_peri.setText(peri);
145                          }
146                          else{
147                              String result=test01.triangle(i_a, i_b, i_c);
148                              textField_is.setText(result);
149                              textField_area.setText("");
150                              textField_peri.setText("");
151                              textField_peri.setText("");
152                          }
153                     }
154                     else{
155                         JOptionPane.showMessageDialog(null, "输入有误,请重请输入正确整数");
156                         new JFrame();
157                         textField_a.setText("");
158                         textField_b.setText("");
159                         textField_c.setText("");
160                         textField_is.setText("");
161                         textField_area.setText("");
162                         textField_peri.setText("");
163                     }
164                 }
165             }
166         });
167         
168         JPanel panel_result1 = new JPanel();
169         panel_result1.setBounds(0, 0, 400, 50);
170         panel.add(panel_result1);
171         
172         JLabel j_is = new JLabel();
173         panel_result1.add(j_is,BorderLayout.SOUTH);
174         j_is.setFont(new Font("宋体", Font.PLAIN, 15));
175         j_is.setBackground(UIManager.getColor("Button.background"));
176         j_is.setText("这个三角形是:");
177         
178         textField_is = new JTextField();
179         textField_is.setFont(new Font("宋体", Font.PLAIN, 15));
180         panel_result1.add(textField_is);
181         textField_is.setEditable(false);
182         textField_is.setColumns(15);
183         
184         JPanel panel_result2 = new JPanel();
185         panel.add(panel_result2);
186         
187         JLabel j_area = new JLabel();
188         panel_result2.add(j_area);
189         j_area.setFont(new Font("宋体", Font.PLAIN, 15));
190         j_area.setBackground(UIManager.getColor("Button.background"));
191         j_area.setText("面积是:");
192         
193         textField_area = new JTextField();
194         textField_area.setFont(new Font("宋体", Font.PLAIN, 15));
195         textField_area.setEditable(false);
196         panel_result2.add(textField_area);
197         textField_area.setColumns(10);
198         
199         JLabel j_peri = new JLabel();
200         panel_result2.add(j_peri);
201         j_peri.setFont(new Font("宋体", Font.PLAIN, 15));
202         j_peri.setBackground(UIManager.getColor("Button.background"));
203         j_peri.setText("周长是:");
204         
205         textField_peri = new JTextField();
206         textField_peri.setFont(new Font("宋体", Font.PLAIN, 15));
207         textField_peri.setEditable(false);
208         panel_result2.add(textField_peri);
209         textField_peri.setColumns(10);
210         
211     }
212 }
View Code

test01类代码:

 1 package test01;
 2 
 3 public class test01 {
 4 
 5     //判断是否是整数
 6     public static boolean isNumber(String s)
 7     {
 8         if(s==null)
 9         {
10             return false;
11         }
12         for(int i = 0; i < s.length(); i++)
13         {
14             if(Character.isDigit(s.charAt(i)) == false)
15             {
16                 return false;
17             }           
18         }
19         return true;
20     }
21     
22     //判断三角形
23     public static String triangle(int a, int b, int c)
24     {
25         String result; 
26         
27         if(a<1 || a>100 || b<1 || b>100 || c<1 || c>100)
28         {
29             result = "边的值不在范围内!";        
30         }
31         if((a<b+c)&&( b<a+c)&&( c<a+b)){
32             if((a==b)||(a==c)||(b==c)){
33                 if((a==b)&&(b==c)) 
34                     result="等边三角形";
35                 else
36                     result="等腰三角形";
37             }
38             else if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+c*c==a*a))
39                 result="直角三角形";
40             else 
41                 result="一般三角形";
42         }
43         else{
44             result="不能构成三角形";
45     }
46         return result;
47 
48     }
49 
50     //返回三角形的周长
51     public static int perimeter(int a,int b,int c){
52         return a+b+c;
53     }
54     
55     //返回三角形的面积
56     public static float triangleArea(int a,int b,int c){
57         float s = (float) (0.5*perimeter( a, b, c));
58         return (float) Math.sqrt((s*(s-a)*(s-b)*(s-c)));
59     }
60 }
View Code

 

三、实验结果

    

   

 

posted @ 2017-05-02 23:10  名山学长  阅读(165)  评论(0编辑  收藏  举报