第4次测试作业代码+160+曾元鹏
类图情况:
界面及功能情况:
public class Client { public static void main(String[] args)throws Exception { new Menu(); } }
public class Menu { private JFrame frame = new JFrame("三角形计算程序"); private JButton calculate = new JButton("OK"); private JButton rest = new JButton("Cancel"); private JLabel lab1 = new JLabel("请输入三角形的三条边:"); private JLabel lab2 = new JLabel("a:"); private JLabel lab3 = new JLabel("b:"); private JLabel lab4 = new JLabel("c:"); private JLabel lab5 = new JLabel("这个三角形是:"); private JLabel lab6 = new JLabel("面积是:"); private JLabel lab7 = new JLabel("周长是:"); private JTextField texta = new JTextField(); private JTextField textb = new JTextField(); private JTextField textc = new JTextField(); private JTextField texttrangle = new JTextField(); private JTextField textarea = new JTextField(); private JTextField textperimeter = new JTextField(); public Menu() { texttrangle.setEnabled(false); textarea.setEnabled(false); textperimeter.setEnabled(false); Font fnt = new Font("Serief", Font.BOLD, 15); frame.setLayout(null); lab1.setFont(fnt); lab1.setBounds(20,20,200,50); lab2.setFont(fnt); lab2.setBounds(30,60,20,20); texta.setBounds(55, 60, 50, 25); lab3.setFont(fnt); lab3.setBounds(120,60,20,20); textb.setBounds(140, 60, 50, 25); lab4.setFont(fnt); lab4.setBounds(200,60,20,20); textc.setBounds(220, 60, 50, 25); calculate.setBounds(60, 100, 70, 30); rest.setBounds(150, 100,75, 30); lab5.setFont(fnt); lab5.setBounds(30,143,100,20); texttrangle.setBounds(145, 140, 120, 25); lab6.setFont(fnt); lab6.setBounds(30,175,60,20); textarea.setBounds(95, 175, 60, 25); lab7.setFont(fnt); lab7.setBounds(165,175,60,20); textperimeter.setBounds(220,175, 60, 25); frame.setSize(270, 280); frame.setLocation(300,200); calculate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (arg0.getSource()==calculate) { try { int a = Integer.parseInt(texta.getText()) ; int b = Integer.parseInt(textb.getText()) ; int c = Integer.parseInt(textc.getText()) ; String result = Sort.triangle(a, b, c); if(result.equals("不构成三角形") || result.equals("边的值不在范围内")) { texttrangle.setText(result); textarea.setText("0"); textperimeter.setText("0"); JOptionPane.showMessageDialog(null, result); }else{ float area = Area.triangleArea(a, b, c); int length = Perimeter.perimeter(a, b, c); texttrangle.setText(result); textarea.setText(String.format("%.2f", area)); textperimeter.setText(String.valueOf(length)); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "输入有误,请重新输入"); } } if (arg0.getSource()==rest) { texta.setText(" "); textb.setText(" "); textc.setText(" "); textarea.setText(" "); texttrangle.setText(" "); textperimeter.setText(" "); JOptionPane.showMessageDialog(null, "已重置"); } } }); frame.add(lab1); frame.add(lab2); frame.add(texta); frame.add(lab3); frame.add(textb); frame.add(lab4); frame.add(textc); frame.add(calculate); frame.add(rest); frame.add(lab5); frame.add(texttrangle); frame.add(lab6); frame.add(textarea); frame.add(lab7); frame.add(textperimeter); frame.setVisible(true); } }
public class Sort { static String t1="边的值不在范围内"; static String t2="不构成三角形"; static String t3="等边三角形"; static String t4="等腰三角形"; static String t5="直角三角形"; static String t6="一般三角形"; public static String triangle(int a,int b,int c){ if(a<1 || a>100 ||b<1 || b>100|| c<1|| c>100) { return t1; } else if(a>=b+c ||b>=a+c||c>=a+b) { return t2; } else if(a==b &&b==c &&c==a){ return t3; } else if(a==b ||b==c ||c==a){ return t4; } else if(a*a+b*b==c*c ||b*b+c*c==a*a ||c*c+a*a==b*b){ return t5; } else { return t6; } } }
public class Area { public static float triangleArea(int a,int b,int c) { float s = (a+b+c)/2; float result =(float) Math.sqrt(s*(s-a)*(s-b)*(s-c)); return result; } }
public class Perimeter { public static int perimeter(int a,int b,int c) { return a+b+c; } }