1.类图:
2.结果:
3.代码及界面:
1 package test; 2 public class Triangle { 3 public String triangle(int a,int b,int c){ 4 String str=null; 5 if(a+b>c){ 6 if((a == b)||(b == c)&&(a!=c)) 7 { 8 str = "等腰三角形"; 9 }10 else if(a == b&& b== c)11 {12 str = "等边三角形";13 }else if(a*a+b*b==c*c){14 str = "直角三角形";15 }else16 str="一般三角形"; 17 }18 else {19 str = "不构成三角形";20 }21 return str;22 }23 public float triangleArea(int a,int b,int c){24 float p=(a+b+c)/2;25 float s=(float)Math.sqrt(p*(p-a)*(p-b)*(p-c));26 return s;27 } 28 public int Perimeter(int a,int b,int c){29 return a+b+c;30 }31 }
界面代码:
1 package test; 2 3 import java.awt.EventQueue; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 import javax.swing.JFrame; 7 import javax.swing.JLabel; 8 import javax.swing.JOptionPane; 9 import javax.swing.JTextField; 10 import javax.swing.JButton; 11 import java.awt.event.WindowEvent; 12 import java.util.Arrays; 13 import java.awt.event.WindowAdapter; 14 15 public class FrameTriangle { 16 private JFrame frame = new JFrame("三角形计算程序"); 17 private JButton b1 = new JButton("OK"); 18 private JButton b2 = new JButton("Cancel"); 19 private JLabel lab1 = new JLabel("请输入三角形的三条边:"); 20 private JLabel length1 = new JLabel("a:"); 21 private JLabel length2 = new JLabel("b:"); 22 private JLabel length3 = new JLabel("c:"); 23 private JLabel define = new JLabel("这个三角形是:"); 24 private JLabel area = new JLabel("面积是:"); 25 private JLabel perimeter = new JLabel("周长是:"); 26 private JTextField a = new JTextField(); 27 private JTextField b = new JTextField(); 28 private JTextField c = new JTextField(); 29 private JTextField judge = new JTextField(); 30 private JTextField s = new JTextField(); 31 private JTextField per = new JTextField(); 32 Triangle t=new Triangle(); 33 public FrameTriangle(){ 34 b1.addActionListener(new ActionListener(){ 35 @Override 36 public void actionPerformed(ActionEvent e){ 37 try{ 38 int[] num; 39 num = new int[3]; 40 num[0]=Integer.parseInt(a.getText()); 41 num[1]=Integer.parseInt(b.getText()); 42 num[2]=Integer.parseInt(c.getText()); 43 if(num[0]>100||num[0]<1||num[1]>100||num[1]<1||num[2]>100||num[2]<1){ 44 JOptionPane.showMessageDialog(null,"边的值不在范围内"); 45 return; 46 } 47 Arrays.sort(num); 48 judge.setText(t.triangle(num[0],num[1],num[2])); 49 s.setText(""+t.triangleArea(num[0],num[1],num[2])); 50 per.setText(""+t.Perimeter(num[0],num[1],num[2])); 51 }catch(Exception ex){ 52 JOptionPane.showMessageDialog(null,"请输入整数"); 53 } 54 } 55 }); 56 b2.addActionListener(new ActionListener(){ 57 @Override 58 public void actionPerformed(ActionEvent e) { 59 a.setText(""); 60 b.setText(""); 61 c.setText(""); 62 judge.setText(""); 63 s.setText(""); 64 per.setText(""); 65 }}); 66 frame.addWindowListener(new WindowAdapter(){ 67 public void windowClosing(WindowEvent e){ 68 System.exit(1); 69 } 70 }); 71 frame.setLayout(null); 72 lab1.setBounds(55,10,350,30); 73 length1.setBounds(55,60,30,15); 74 length2.setBounds(145,60,30,15); 75 length3.setBounds(225,60,30,15); 76 define.setBounds(55,130,300,30); 77 area.setBounds(55,180,50,20); 78 perimeter.setBounds(180,180,50,20); 79 a.setBounds(70,55,50,25); 80 b.setBounds(160,55,50,25); 81 c.setBounds(240,55,50,25); 82 judge.setBounds(140,130,150,30); 83 s.setBounds(100,180,55,25); 84 per.setBounds(230,180,55,25); 85 b1.setBounds(55,90,100,30); 86 b2.setBounds(175,90,100,30); 87 frame.add(lab1); 88 frame.add(length1); 89 frame.add(length2); 90 frame.add(length3); 91 frame.add(define); 92 frame.add(area); 93 frame.add(perimeter); 94 frame.add(a); 95 frame.add(b); 96 frame.add(c); 97 frame.add(judge); 98 frame.add(s); 99 frame.add(per);100 frame.add(b1);101 frame.add(b2);102 frame.setSize(500,400);103 frame.setVisible(true); 104 } 105 public static void main(String[] args){106 EventQueue.invokeLater(new Runnable() {107 public void run() {108 try{109 FrameTriangle window = new FrameTriangle();110 window.frame.setVisible(true);111 }catch (Exception e){112 e.printStackTrace();113 }114 }115 });116 } 117 }