第4次作业类测试代码+117+陈俊超
1.类图
2.完成界面和相应的功能
3.代码
1 package caculate; 2 3 import java.text.DecimalFormat; 4 5 public class Caculate { 6 public String triangle(int a, int b, int c) 7 { 8 String result; 9 if(a<1 || a>100 || b<1 || b>100 || c<1 || c>100) { 10 result = "边的值不在范围内!"; 11 } 12 else if(a>=b+c || b>=a+c || c>=a+b) { 13 result = "不构成三角形!"; 14 } 15 else if(a==b && b==c){ 16 result = "等边三角形"; 17 } 18 else if((a-b)*(a-c)*(b-c)==0) { 19 result = "等腰三角形"; 20 } 21 else if(a*a + b*b == c*c || a*a + c*c == b*b || c*c + b*b == a*a){ 22 result = "直角三角形"; 23 } 24 else { 25 result = "一般三角形"; 26 } 27 return result; 28 } 29 //计算面积 30 public String mianji(int a,int b,int c) 31 { 32 String temp = triangle(a, b, c); 33 //保留两位小数 34 DecimalFormat df = new DecimalFormat("#.00"); 35 float result=0; 36 float p; 37 String mianjiresult = null; 38 if(!temp.equals("边的值不在范围内!") &&!temp.equals("不构成三角形!")) 39 { 40 //海伦公式 41 p=(float) ((a+b+c)/2.0); 42 result=(float) (Math.sqrt(p*(p-a)*(p-b)*(p-c))); 43 mianjiresult=df.format(result); 44 } 45 return mianjiresult; 46 } 47 48 //计算周长 49 public int zhouchang(int a,int b,int c) 50 { 51 String temp = triangle(a, b, c); 52 int result=0; 53 if(!temp.equals("边的值不在范围内!") &&!temp.equals("不构成三角形!")) 54 { 55 result=a+b+c; 56 } 57 return result; 58 59 } 60 }
1 package caculate; 2 3 import java.awt.EventQueue; 4 5 import javax.swing.JFrame; 6 import javax.swing.JOptionPane; 7 import javax.swing.JPanel; 8 import javax.swing.JLabel; 9 import java.awt.Font; 10 import javax.swing.JTextField; 11 import javax.swing.JButton; 12 import java.awt.event.ActionListener; 13 import java.awt.event.ActionEvent; 14 15 public class Triangle extends JFrame { 16 17 private JPanel contentPane; 18 private JTextField atext; 19 private JTextField btext; 20 private JTextField ctext; 21 private JTextField resulttext; 22 private JTextField mianji; 23 private JTextField zhouchang; 24 25 /** 26 * Launch the application. 27 */ 28 public static void main(String[] args) { 29 EventQueue.invokeLater(new Runnable() { 30 public void run() { 31 try { 32 Triangle frame = new Triangle(); 33 frame.getContentPane().setLayout(null); 34 frame.setVisible(true); 35 } catch (Exception e) { 36 e.printStackTrace(); 37 } 38 } 39 }); 40 } 41 42 /** 43 * Create the frame. 44 */ 45 public Triangle() { 46 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 47 setBounds(100, 100, 450, 300); 48 contentPane = new JPanel(); 49 contentPane.setLayout(null); 50 setContentPane(contentPane); 51 52 JLabel lab = new JLabel("\u8BF7\u8F93\u5165\u4E09\u89D2\u5F62\u7684\u4E09\u6761\u8FB9\uFF1A"); 53 lab.setFont(new Font("黑体", Font.PLAIN, 14)); 54 contentPane.add(lab); 55 lab.setBounds(129, 10, 196, 32); 56 57 JLabel laba = new JLabel("a:"); 58 laba.setFont(new Font("黑体", Font.PLAIN, 14)); 59 contentPane.add(laba); 60 laba.setBounds(64, 73, 36, 23); 61 62 JLabel labb = new JLabel("b:"); 63 labb.setFont(new Font("黑体", Font.PLAIN, 14)); 64 labb.setBounds(187, 73, 36, 23); 65 contentPane.add(labb); 66 67 JLabel labc = new JLabel("c:"); 68 labc.setFont(new Font("黑体", Font.PLAIN, 14)); 69 labc.setBounds(307, 73, 36, 23); 70 contentPane.add(labc); 71 72 atext = new JTextField(); 73 atext.setBounds(84, 73, 66, 21); 74 contentPane.add(atext); 75 atext.setColumns(10); 76 77 btext = new JTextField(); 78 btext.setBounds(206, 73, 66, 21); 79 contentPane.add(btext); 80 btext.setColumns(10); 81 82 ctext = new JTextField(); 83 ctext.setBounds(329, 73, 66, 21); 84 contentPane.add(ctext); 85 ctext.setColumns(10); 86 87 final JButton btnOk = new JButton("OK"); 88 btnOk.addActionListener(new ActionListener() { 89 public void actionPerformed(ActionEvent e) { 90 if(e.getSource()==btnOk) 91 { 92 Caculate cacu =new Caculate(); 93 int a=0,b=0,c=0; 94 String A,B,C; 95 A=atext.getText().toString(); 96 B=btext.getText().toString(); 97 C=ctext.getText().toString(); 98 99 //判断是否为数字 100 if(A.matches("[0-9]+") && B.matches("[0-9]+") && C.matches("[0-9]+")) 101 { 102 a = Integer.parseInt(A); 103 b = Integer.parseInt(B); 104 c = Integer.parseInt(C); 105 resulttext.setText(cacu.triangle(a,b,c)); 106 mianji.setText(cacu.mianji(a,b,c)+""); 107 zhouchang.setText(cacu.zhouchang(a,b,c)+""); 108 } 109 else 110 { 111 resulttext.setText("输入有误,请重新输入"); 112 } 113 } 114 } 115 }); 116 btnOk.setFont(new Font("黑体", Font.PLAIN, 14)); 117 btnOk.setBounds(118, 119, 79, 23); 118 contentPane.add(btnOk); 119 120 final JButton btnCancel = new JButton("CANCEL"); 121 btnCancel.addActionListener(new ActionListener() { 122 public void actionPerformed(ActionEvent e) { 123 if(e.getSource()==btnCancel) 124 { 125 atext.setText(""); 126 btext.setText(""); 127 ctext.setText(""); 128 resulttext.setText(""); 129 mianji.setText(""); 130 zhouchang.setText(""); 131 JOptionPane.showMessageDialog(null,"已重置"); 132 } 133 } 134 }); 135 btnCancel.setFont(new Font("黑体", Font.PLAIN, 14)); 136 btnCancel.setBounds(246, 119, 79, 23); 137 contentPane.add(btnCancel); 138 139 JLabel label = new JLabel("\u8FD9\u4E2A\u4E09\u89D2\u5F62\u662F:"); 140 label.setFont(new Font("黑体", Font.PLAIN, 14)); 141 label.setBounds(84, 164, 113, 23); 142 contentPane.add(label); 143 144 resulttext = new JTextField(); 145 resulttext.setBounds(183, 165, 136, 21); 146 contentPane.add(resulttext); 147 resulttext.setColumns(10); 148 149 JLabel label_1 = new JLabel("\u9762\u79EF\u662F:"); 150 label_1.setFont(new Font("黑体", Font.PLAIN, 14)); 151 label_1.setBounds(64, 208, 54, 15); 152 contentPane.add(label_1); 153 154 mianji = new JTextField(); 155 mianji.setBounds(111, 205, 66, 21); 156 contentPane.add(mianji); 157 mianji.setColumns(10); 158 159 JLabel label_2 = new JLabel("\u5468\u957F\u662F:"); 160 label_2.setFont(new Font("黑体", Font.PLAIN, 14)); 161 label_2.setBounds(233, 208, 54, 15); 162 contentPane.add(label_2); 163 164 zhouchang = new JTextField(); 165 zhouchang.setBounds(283, 205, 66, 21); 166 contentPane.add(zhouchang); 167 zhouchang.setColumns(10); 168 } 169 }