第4次作业类测试代码_061_陈小兰

1.类图:

 

 

 

 

 

2.结果:

 

 

 

 

 

 

3.代码

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.JTextField;
import javax.swing.JButton;


public class TriangleJudge{

    protected static final Object Submit = null;
    protected static final Object Cancle = null;
    private JFrame frame;
    private JTextField SideaText;
    private JTextField SidebText;
    private JTextField SidecText;
    private JTextField TypeText;
    private JTextField AreaText;
    private JTextField PerimeterText;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TriangleJudge window = new TriangleJudge();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TriangleJudge() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame("三角形判断程序");
        frame.setBounds(100, 100, 450, 350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        JLabel titleLabel = new JLabel("请输入三角形的三条边:");
        titleLabel.setFont(new Font("宋体",Font.BOLD,15));
        titleLabel.setBounds(21, 10, 270, 26);
        frame.getContentPane().add(titleLabel);
        
        JLabel Sidea = new JLabel("a:");
        Sidea.setFont(new Font("宋体", Font.PLAIN, 13));
        Sidea.setBounds(31, 46, 54, 15);
        frame.getContentPane().add(Sidea);
        
        SideaText = new JTextField();
        SideaText.setBounds(56, 46, 66, 21);
        frame.getContentPane().add(SideaText);
        SideaText.setColumns(10);
        
        JLabel Sideb = new JLabel("b:");
        Sideb.setFont(new Font("宋体", Font.PLAIN, 13));
        Sideb.setBounds(157, 49, 54, 15);
        frame.getContentPane().add(Sideb);
        
        
        SidebText = new JTextField();
        SidebText.setColumns(10);
        SidebText.setBounds(187, 46, 66, 21);
        frame.getContentPane().add(SidebText);
        
        JLabel Sidec = new JLabel("c:");
        Sidec.setFont(new Font("宋体", Font.PLAIN, 13));
        Sidec.setBounds(304, 49, 54, 15);
        frame.getContentPane().add(Sidec);
        
        SidecText = new JTextField();
        SidecText.setColumns(10);
        SidecText.setBounds(325, 43, 66, 21);
        frame.getContentPane().add(SidecText);
        
        JButton OK = new JButton("OK");
        OK.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                if(e.getSource()==OK)
                {
                     String strs_a = SideaText.getText();
                     String strs_b = SidebText.getText();
                     String strs_c = SidecText.getText();
                    Triangle triangle = new Triangle();
                    //当输入满足条件
                    if (triangle.isNumber(strs_a)&&triangle.isNumber(strs_b)&&triangle.isNumber(strs_c)) {
                        //将字符型转变为整数型
                        int s_a = Integer.parseInt(strs_a);
                        int s_b = Integer.parseInt(strs_b);
                        int s_c = Integer.parseInt(strs_c);
                        //计算结果
                        String Type = triangle.triangle(s_a, s_b, s_c);
                        float Area = triangle.triangleArea(s_a, s_b, s_c);
                        int Perimeter = triangle.perimeter(s_a, s_b, s_c);
                        //设置内容
                        TypeText.setText(String.valueOf(Type));
                        AreaText.setText(String.valueOf(Area));
                        PerimeterText.setText(String.valueOf(Perimeter));
                    }
    
                    else
                    {
                         JOptionPane.showMessageDialog(null, "输入有误,请重请输入正确整数");
                         new JFrame();
                         SideaText.setText("");
                         SidebText.setText("");
                         SidecText.setText("");
                    }
                }            
            }
        });
        OK.setBounds(65, 92, 93, 23);
        frame.getContentPane().add(OK);

        JButton Cancel = new JButton("Cancel");
        Cancel.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                if(e.getSource()==Cancel)
                {
                    SideaText.setText("");
                    SidebText.setText("");
                    SidecText.setText("");
                    TypeText.setText("");
                    AreaText.setText("");
                    PerimeterText.setText("");
                }
                
            }
        });
        Cancel.setBounds(233, 92, 93, 23);
        frame.getContentPane().add(Cancel);
        
        JLabel Typelable = new JLabel("这个三角形是:");
        Typelable.setBounds(23, 146, 150, 29);
        frame.getContentPane().add(Typelable);
        
        JLabel Arealabel = new JLabel("面积是:");
        Arealabel.setBounds(56, 184, 150, 29);
        frame.getContentPane().add(Arealabel);
        
        JLabel Perimeterlabel = new JLabel("周长是:");
        Perimeterlabel.setBounds(56, 221, 150, 29);
        frame.getContentPane().add(Perimeterlabel);
        
        TypeText = new JTextField();
        TypeText.setBounds(125, 153, 200, 23);
        frame.getContentPane().add(TypeText);
        TypeText.setColumns(10);
        TypeText.setEnabled(false);
        TypeText.setDisabledTextColor(Color.BLACK);
        
        AreaText = new JTextField();
        AreaText.setColumns(10);
        AreaText.setBounds(125, 191, 200, 23);
        frame.getContentPane().add(AreaText);
        AreaText.setEnabled(false);
        AreaText.setDisabledTextColor(Color.BLACK);
        
        PerimeterText = new JTextField();
        PerimeterText.setColumns(10);
        PerimeterText.setBounds(125, 226, 200, 23);
        frame.getContentPane().add(PerimeterText);
        PerimeterText.setEnabled(false);
        PerimeterText.setDisabledTextColor(Color.BLACK);
    }
}
View Code
import java.util.Scanner;
public class Triangle {
    
    //判断是否输入的是整数
        public boolean isNumber(String num) {
                if (!num.matches("^[0-9]*$")) {
                    return false;
                } 
                    return true;
        }
    //判断三角形的类型
    public static String triangle(int a, int b, int c) {
        String s = null;
        if ((a < 1 || a > 100) || (b < 1 || b > 100) || (c < 1 || c > 100)) {
            s = "边的值不在范围内!";
        }
        
        else if (a < b + c && b < a + c && c < a + b) {// 两边之和大于第三边可构成三角形
              if (a == b || a == c || b == c) {// 等腰三角形
                if (a == b && b == c) {// 等边三角形
                    s = "可以组成等边三角形";
                } else {
                    s = "可以组成等腰三角形";
                }
              }
              else if (a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a) {//直角三角形
              s="可以组成直角三角形";
            }
            else s = "可以组成普通三角形";

            }
            
        // 不能构成三角形
        else{
            s = "不能够组成三角形!";
        }
        
        return s;
    }
    
    //计算面积
    public static float triangleArea(int a,int b,int c)
    {
        double l=(a+b+c)*0.5;
        return (float)Math.sqrt(l*(l-a)*(l-b)*(l-c));
    }
    
    //计算周长
    public static int perimeter(int a,int b,int c)
    {
        return a+b+c;
    }
    
    
}
View Code

 

posted on 2017-05-03 21:57  陈小兰  阅读(282)  评论(0编辑  收藏  举报

导航