利用5个标签,4个文本框,2个按钮,自己设计布局实现输入三边求三角形的面积

 利用5个标签,4个文本框,2个按钮,自己设计布局实现输入三边求三角形的面积,当点击“判断三边”时,若能构成三角形,在标签5上显示“可以构成三角形!”,如不能构成三角形,在标签5上显示“不能构成三角形,请重新设置三边”,只有满足能够构成三角形条件时,点击“计算面积”按钮,将面积显示在第四个文本框中。当点击“退出程序” 按钮时,能够退出应用程序。

Code:
  1.  //方案一  
  2. import javax.swing.*;  
  3. import java.awt.*;  
  4. import java.awt.event.*;  
  5. import javax.swing.border.*;  
  6.   
  7. class WindowBox extends Frame  
  8. implements ActionListener  
  9. {  
  10.    Box baseBox,boxV1,boxV2;//三个盒式容器  
  11.    Label la;               //一个标签  
  12.    Button b1,b2,b3;        //三个按钮  
  13.    TextField t1,t2,t3,t4;  //三个文本域  
  14.    WindowBox(String s)  
  15.    {  
  16.       super(s);            //调用父类(Frame)构造函数  
  17.         
  18.       boxV1=Box.createVerticalBox();               //列型盒式布局  
  19.       boxV1.add(new Label("请输入三角形第一条边:                     "));//空格重要,用于布局  
  20.       boxV1.add(Box.createVerticalStrut(10));      //垂直支撑  
  21.       boxV1.add(new Label("请输入三角形第二条边:"));  
  22.       boxV1.add(Box.createVerticalStrut(10));  
  23.       boxV1.add(new Label("请输入三角形第三条边:"));  
  24.       boxV1.add(Box.createVerticalStrut(10));  
  25.       boxV1.add(new Label("三角形面积是:"));  
  26.       boxV1.add(Box.createVerticalStrut(10));  
  27.       la=new Label();                              //标签初始状态为空  
  28.       boxV1.add(la);  
  29.       boxV1.add(Box.createVerticalStrut(10));  
  30.       b1=new Button("计算面积");  
  31.       boxV1.add(b1);  
  32.       b1.addActionListener(this);                  //监视器  
  33.         
  34.       boxV2=Box.createVerticalBox();               //列型盒式布局  
  35.       t1=new TextField(12);  
  36.       boxV2.add(t1);  
  37.       boxV2.add(Box.createVerticalStrut(10));  
  38.       t2=new TextField(12);  
  39.       boxV2.add(t2);  
  40.       boxV2.add(Box.createVerticalStrut(10));  
  41.       t3=new TextField(12);  
  42.       boxV2.add(t3);  
  43.       boxV2.add(Box.createVerticalStrut(10));  
  44.       t4=new TextField(12);  
  45.       boxV2.add(t4);  
  46.       boxV2.add(Box.createVerticalStrut(10));  
  47.       b2=new Button("判断三边");  
  48.       boxV2.add(b2);  
  49.       b2.addActionListener(this);  
  50.       boxV2.add(Box.createVerticalStrut(10));  
  51.       b3=new Button("退出程序");  
  52.       boxV2.add(b3);  
  53.       b3.addActionListener(this);  
  54.         
  55.       baseBox=Box.createHorizontalBox();           //行型盒式布局  
  56.       baseBox.add(boxV1);  
  57.       baseBox.add(Box.createHorizontalStrut(10));  //水平支撑  
  58.       baseBox.add(boxV2);  
  59.       setLayout(new FlowLayout());  
  60.       add(baseBox);  
  61.         
  62.       setBounds(200,200,380,250);  
  63.       setVisible(true);  
  64.    }  
  65.      
  66.    public void actionPerformed(ActionEvent e)      //处理整件的接口  
  67.    {  boolean boo=false;  
  68.       int a=0,b=0,c=0;  
  69.       a=Integer.parseInt(t1.getText());              
  70.       b=Integer.parseInt(t2.getText());       
  71.       c=Integer.parseInt(t3.getText());  
  72.       if(a+b>c&&a+c>b&&b+c>a)  //判断三边  
  73.          boo=true;  
  74.       else  
  75.          boo=false;  
  76.       if(e.getSource()==b2)      
  77.       {  
  78.          if(boo)  
  79.              la.setText("可以构成三角形!");   
  80.          else  
  81.              la.setText("不能构成一个三角形,请重新设置三边");  
  82.              t4.setText("");    //清空  
  83.       }   
  84.         
  85.       else if(e.getSource()==b1)//计算面积  
  86.       {    
  87.          if(boo)    
  88.           {  
  89.              double p=(a+b+c)/2.0;  
  90.            double area=Math.sqrt(p*(p-a)*(p-b)*(p-c));  
  91.            String s=String.valueOf(area);  
  92.            t4.setText(s);  
  93.           }  
  94.       }  
  95.         
  96.       else if(e.getSource()==b3)//退出  
  97.       {  
  98.         System.exit(0);   
  99.       }  
  100.    }  
  101. }  
  102.    
  103. public class triangle1  
  104. {  
  105.       public static void main(String[] args)  
  106.       {  
  107.          new WindowBox("求三角形面积");  
  108.       }  
  109. }  
  110. //方案二  
  111. import javax.swing.*;  
  112. import java.awt.*;  
  113. import java.awt.event.*;  
  114. import javax.swing.border.*;  
  115.   
  116. class WindowBox extends Frame  
  117. implements ActionListener  
  118. {  
  119.    Box baseBox,boxV1,boxV2;//三个盒式容器  
  120.    Label la;               //一个标签  
  121.    Button b1,b2,b3;        //三个按钮  
  122.    TextField t1,t2,t3,t4;  //三个文本域  
  123.    WindowBox(String s)  
  124.    {  
  125.       super(s);            //调用父类(Frame)构造函数  
  126.         
  127.       boxV1=Box.createVerticalBox();               //列型盒式布局  
  128.       boxV1.add(new Label("请输入三角形第一条边:                     "));//空格重要,用于布局  
  129.       boxV1.add(Box.createVerticalStrut(10));      //垂直支撑  
  130.       boxV1.add(new Label("请输入三角形第二条边:"));  
  131.       boxV1.add(Box.createVerticalStrut(10));  
  132.       boxV1.add(new Label("请输入三角形第三条边:"));  
  133.       boxV1.add(Box.createVerticalStrut(10));  
  134.       boxV1.add(new Label("三角形面积是:"));  
  135.       boxV1.add(Box.createVerticalStrut(10));  
  136.       la=new Label();                              //标签初始状态为空  
  137.       boxV1.add(la);  
  138.       boxV1.add(Box.createVerticalStrut(10));  
  139.       b1=new Button("计算面积");  
  140.       boxV1.add(b1);  
  141.       b1.addActionListener(this);                  //监视器  
  142.         
  143.       boxV2=Box.createVerticalBox();               //列型盒式布局  
  144.       t1=new TextField(12);  
  145.       boxV2.add(t1);  
  146.       boxV2.add(Box.createVerticalStrut(10));  
  147.       t2=new TextField(12);  
  148.       boxV2.add(t2);  
  149.       boxV2.add(Box.createVerticalStrut(10));  
  150.       t3=new TextField(12);  
  151.       boxV2.add(t3);  
  152.       boxV2.add(Box.createVerticalStrut(10));  
  153.       t4=new TextField(12);  
  154.       boxV2.add(t4);  
  155.       boxV2.add(Box.createVerticalStrut(10));  
  156.       b2=new Button("判断三边");  
  157.       boxV2.add(b2);  
  158.       b2.addActionListener(this);  
  159.       boxV2.add(Box.createVerticalStrut(10));  
  160.       b3=new Button("退出程序");  
  161.       boxV2.add(b3);  
  162.       b3.addActionListener(this);  
  163.         
  164.       baseBox=Box.createHorizontalBox();           //行型盒式布局  
  165.       baseBox.add(boxV1);  
  166.       baseBox.add(Box.createHorizontalStrut(10));  //水平支撑  
  167.       baseBox.add(boxV2);  
  168.       setLayout(new FlowLayout());  
  169.       add(baseBox);  
  170.         
  171.       setBounds(200,200,380,250);  
  172.       setVisible(true);  
  173.    }  
  174.      
  175.    public void actionPerformed(ActionEvent e)      //处理整件的接口  
  176.    {     
  177.       int a=0,b=0,c=0;  
  178.       a=Integer.parseInt(t1.getText());              
  179.       b=Integer.parseInt(t2.getText());       
  180.       c=Integer.parseInt(t3.getText());  
  181.       Trangle t=new Trangle(a,b,c);  
  182.         
  183.       if(e.getSource()==b2)      
  184.       {  
  185.          if(t.boo)  
  186.              la.setText("可以构成三角形!");   
  187.          else  
  188.              la.setText("不能构成一个三角形,请重新设置三边");  
  189.              t4.setText("");    //清空  
  190.       }   
  191.         
  192.       else if(e.getSource()==b1)//计算面积  
  193.       {    
  194.            String str=String.valueOf(t.Area());  
  195.            t4.setText(str);  
  196.             
  197.       }  
  198.         
  199.       else if(e.getSource()==b3)//退出  
  200.       {  
  201.         System.exit(0);   
  202.       }    
  203.    }  
  204. }  
  205.   
  206.    
  207. class Trangle  
  208. {  
  209.     double sideA,sideB,sideC;  
  210.     boolean boo;  
  211.     public Trangle(double a,double b,double c)  
  212.     {  
  213.         sideA=a;sideB=b;sideC=c;  
  214.         if(a+b>c&&a+c>b&&c+b>a)   
  215.             boo=true;  
  216.         else  
  217.             boo=false;  
  218.     }  
  219.     public double Area()  
  220.     {  
  221.         if(boo)  
  222.         {  
  223.            double p=(sideA+sideB+sideC)/2.0;  
  224.            double area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));  
  225.            return area;  
  226.         }  
  227.         else  
  228.            return 0;  
  229.     }  
  230.        
  231. }  
  232.    
  233. public class triangle2  
  234. {  
  235.       public static void main(String[] args)  
  236.       {  
  237.          new WindowBox("求三角形面积");  
  238.       }  
  239. }  

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值