码迷,mamicode.com
首页 > 其他好文 > 详细

第4次作业类测试代码+028+刘孝天

时间:2017-05-02 23:50:32      阅读:419      评论:0      收藏:0      [点我收藏+]

标签:div   layout   manage   ext   round   method   ada   final   tco   

一:类图

技术分享

二:代码:

 1:定义接口

 1 package jframeTest;
 2 /*
 3  * @author lxt
 4  * @date 2017年5月2日下午4:22:35
 5  * @Param
 6  */
 7 public interface InteUtil {
 8 
 9     public int perimeter(int a,int b,int c);
10     public float triangleArea(int a,int b,int c);
11     public boolean IsNotInt(String str);
12 }

 2:定义接口实现类

 1 package jframeTest;
 2 import java.math.BigDecimal;
 3 /*
 4  * @author lxt
 5  * @date 2017年5月2日下午4:12:22
 6  * @Param
 7  */
 8 public class TriangleTest implements InteUtil {
 9     public static String triangle(int a, int b, int c){
10         String str=null;
11         if((a>=1&&a<=100)&&(b>=1&&b<=100)&&(c>=1&&c<=100)){
12             if(a<(b+c)&&b<(a+c)&&c<(a+b)){    
13                 if(a==b&&a==c){
14                     str="等边三角形";
15                 }else if(a==b||a==c||b==c){
16                     str="等腰三角形";
17                 }else if(a*a+b*b==c*c||b*b+c*c==a*a||c*c+a*a==b*b){
18                     str="直角三角形";
19                 }else{
20                     str="一般三角形";
21                 }
22             }else{
23                 str="不能构成三角形";
24             }
25         }else{
26             str="边的值不在范围内";
27         }
28         return str;
29     }
30     
31     /* (non-Javadoc)
32      * @see jframeTest.InteUtil#IsNotInt(int, int, int)
33      * 判断输入的是否为数字
34      */
35      public boolean IsNotInt(String str)
36         {
37          if(str==null)
38            {
39             return false;
40            }
41          else{
42             try{
43               int num=Integer.valueOf(str);
44               return true;
45             }catch (Exception e) {
46               return false;
47                }
48            }
49        }
50 
51     /* (non-Javadoc)
52      * @see jframeTest.InteUtil#perimeter(int, int, int)
53      * 计算三角形的周长
54      */
55     public int perimeter(int a, int b, int c) {
56         // TODO Auto-generated method stub
57         int per=a+b+c;
58         return per;
59     }
60     /* (non-Javadoc)
61      * @see jframeTest.InteUtil#triangleArea(int, int, int)
62      * 计算三角形的面积
63      */
64     public float triangleArea(int a, int b, int c) {
65         // TODO Auto-generated method stub
66          double i = ((a+b+c)*0.5);
67          float j= (float) (i*(i-a)*(i-b)*(i-c));
68          float str=(float)Math.sqrt(j); 
69          BigDecimal bigDecimal = new BigDecimal(str);  
70          float area = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();  
71          return area;
72     }
73 }

 3:定义窗体类

  1 package jframeTest;
  2 
  3 import java.awt.Color;
  4 import java.awt.EventQueue;
  5 import javax.swing.JFrame;
  6 import javax.swing.JOptionPane;
  7 import javax.swing.JPanel;
  8 import java.awt.BorderLayout;
  9 import javax.swing.JButton;
 10 import javax.swing.JTextField;
 11 import java.awt.Font;
 12 import javax.swing.UIManager;
 13 import java.awt.event.MouseAdapter;
 14 import java.awt.event.MouseEvent;
 15 import javax.swing.JLabel;
 16 
 17 /*
 18  * @author lxt
 19  * @date 2017年5月2日下午3:53:05
 20  * @Param
 21  */
 22 public class Jframe {
 23 
 24     private JFrame jframe;
 25     private static TriangleTest text;
 26     private JTextField texta;
 27     private JTextField textb;
 28     private JTextField textc;
 29     private JTextField perimeter;
 30     private JTextField number;
 31     private JTextField outarea;
 32            
 33     public void Triangle() {
 34         
 35         jframe = new JFrame();
 36         jframe.setForeground(Color.green);
 37         jframe.setTitle("欢迎使用三角形管理系统");
 38         jframe.setBounds(300, 250, 400, 230);
 39         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
 40         JPanel jpanel = new JPanel();
 41         jpanel.setBounds(0, 0, 400, 280);
 42         jframe.getContentPane().add(jpanel, BorderLayout.CENTER);      
 43         
 44         JLabel input = new JLabel("请输入三角形的三条边:");
 45         input.setForeground(Color.blue);
 46         input.setFont(new Font("宋体", Font.BOLD, 14));
 47         input.setBounds(30, 100, 300, 30);
 48         jpanel.add(input);            
 49         JPanel jp = new JPanel();
 50         jpanel.add(jp);
 51         JLabel jpa = new JLabel("a:");
 52         jp.add(jpa);
 53         jpa.setFont(new Font("宋体", Font.BOLD, 14));
 54         jpa.setSize(30, 30);
 55         texta = new JTextField();
 56         jp.add(texta);
 57         texta.setColumns(5);
 58         texta.setBackground(Color.yellow);
 59             
 60         JLabel jpb = new JLabel("b:");
 61         jp.add(jpb);
 62         jpb.setFont(new Font("宋体", Font.BOLD, 14));
 63         jpb.setSize(20, 20);
 64         textb = new JTextField();
 65         jp.add(textb);
 66         textb.setColumns(5);
 67         textb.setBackground(Color.yellow);
 68         JLabel jpc = new JLabel("c:");
 69         jp.add(jpc);
 70         jpc.setFont(new Font("宋体", Font.BOLD, 14));
 71         jpc.setSize(20, 20);
 72         
 73         textc = new JTextField();
 74         jp.add(textc);
 75         textc.setColumns(5);
 76         textc.setBackground(Color.yellow);    
 77         JPanel bto = new JPanel();
 78         bto.setSize(500, 90);
 79         jpanel.add(bto);
 80             
 81         //用final修饰,供内部类使用
 82         final JButton btok = new JButton("OK");
 83         btok.setSize(60, 60);
 84         btok.setBackground(Color.GREEN);
 85         btok.setFont(new Font("宋体", Font.PLAIN, 14));
 86         bto.add(btok);
 87             
 88         final JButton btc = new JButton("Cancel");
 89         btc.setSize(60, 60);
 90         btc.setBackground(Color.GREEN);
 91         btc.setFont(new Font("宋体", Font.PLAIN, 14));
 92         bto.add(btc);
 93         JPanel outpanel = new JPanel();
 94         outpanel.setBounds(0, 0, 400, 50);
 95         jpanel.add(outpanel);
 96         
 97         JLabel output = new JLabel();
 98         outpanel.add(output,BorderLayout.SOUTH);
 99         output.setFont(new Font("宋体",Font.BOLD, 16));
100         output.setText("这个三角形是:");
101         number = new JTextField();
102         number.setFont(new Font("宋体", Font.PLAIN, 15));
103         outpanel.add(number);
104         number.setBackground(Color.yellow);
105         number.setEditable(false);
106         number.setColumns(18);
107         
108         JPanel jpout = new JPanel();
109         jpanel.add(jpout);
110         JLabel outare = new JLabel();
111         jpout.add(outare);
112         outare.setFont(new Font("宋体", Font.PLAIN, 15));
113         outare.setBackground(UIManager.getColor("Button.background"));
114         outare.setText("面积为:");
115         
116         outarea = new JTextField();
117         outarea.setFont(new Font("宋体", Font.PLAIN, 15));
118         outarea.setEditable(false);
119         jpout.add(outarea);
120         outarea.setColumns(9);
121         outarea.setBackground(Color.yellow);
122         
123         JLabel outper = new JLabel();
124         jpout.add(outper);
125         outper.setFont(new Font("宋体", Font.PLAIN, 15));
126         outper.setBackground(UIManager.getColor("Button.background"));
127         outper.setText("周长为:");
128         
129         perimeter = new JTextField();
130         perimeter.setFont(new Font("宋体", Font.PLAIN, 15));
131         perimeter.setEditable(false);
132         jpout.add(perimeter);
133         perimeter.setColumns(9);
134         perimeter.setBackground(Color.yellow);
135             
136         btc.addMouseListener(new MouseAdapter() {
137         public void mouseClicked(MouseEvent event) {
138            Object object = event.getSource();
139               if(object == btc){
140                texta.setText("");
141                textb.setText("");
142                textc.setText("");
143                number.setText("");
144                outarea.setText("");
145                perimeter.setText("");
146                   }
147                 }
148             });
149         
150     btok.addMouseListener(new MouseAdapter() {
151     public void mouseClicked(MouseEvent event) {
152       Object object = event.getSource();
153         if(object == btok){         
154            int aa,bb,cc;
155            //获取输入框内的文本
156            String a=texta.getText();
157            String b=textb.getText();
158            String c=textc.getText();    
159            //p判断输入边的值是否合法
160            if(text.IsNotInt(a)&&text.IsNotInt(b)&&text.IsNotInt(c))
161            {
162                 aa = Integer.parseInt(a);
163                 bb = Integer.parseInt(b);
164                 cc = Integer.parseInt(c);
165                 //如果输入边的值可构成三角形则计算面积和周长
166                 if (!(TriangleTest.triangle(aa, bb, cc)).equals("边的值不在范围内")&&!(TriangleTest.triangle(aa, bb, cc)).equals("不能构成三角形"))
167                 {
168                     String pe=String.valueOf(text.perimeter(aa, bb, cc));
169                     String ar=String.valueOf(text.triangleArea(aa, bb, cc));
170                     String re=TriangleTest.triangle(aa, bb, cc);
171                     perimeter.setText(pe);
172                     outarea.setText(ar);
173                     number.setText(re);
174                 }
175                 else{
176                //否则将面积和周长置为空
177                     String result=TriangleTest.triangle(aa, bb, cc);
178                     number.setText(result);
179                     outarea.setText("");
180                     perimeter.setText("");
181                     perimeter.setText("");
182                     }
183           }
184            else{
185              // 输入的数值不合法,将输入框置为空
186             JOptionPane.showMessageDialog(null, "输入有误,请重请输入");
187             new JFrame();
188             texta.setText("");
189             textb.setText("");
190             textc.setText("");
191             number.setText("");
192             outarea.setText("");
193             perimeter.setText("");
194                 }
195             }
196         }
197       });
198      }
199     
200     public static void main(String[] args) {
201         text=new TriangleTest();
202         EventQueue.invokeLater(new Runnable() {
203         public void run() {               
204             Jframe window = new Jframe();
205             //设置窗体的可见性
206             window.jframe.setVisible(true);     
207           }
208        });
209     }
210     public Jframe() {
211         Triangle();
212     }
213 }

三:运行结果

 1:

 技术分享

 2:

 技术分享

 3:

 技术分享

4:

 技术分享

 

第4次作业类测试代码+028+刘孝天

标签:div   layout   manage   ext   round   method   ada   final   tco   

原文地址:http://www.cnblogs.com/sky-tian/p/6798910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!