类测试代码的具体要求如下:
界面操作说明补充:
点击OK,如果输入有效,进行相应的数值计算;如果数值不满足约束,则弹出错误说明,统一为“输入有误,请重新输入”,然后回到初始输入状态。
点击Cancle,表示重置,清空前面的数据,回到初始状态。
(1)设计三角形完整程序
已经完成的方法是: String triangle(int a,int b,int c)
现在要求继续增加新的功能:
建立界面,至少包含以下元素,但不限于此:
完成面积的方法:float triangleArea(int a,int b,int c) ,完成周长的方法:int perimeter(int a,int b,int c)
1、类图:
2、代码及实现结果:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.awt.EventQueue; 2 import java.awt.Font; 3 import java.awt.Toolkit; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 import javax.swing.JButton; 7 import javax.swing.JFrame; 8 import javax.swing.JLabel; 9 import javax.swing.JOptionPane; 10 import javax.swing.JTextField; 11 import java.util.Scanner; 12 13 public class screen { 14 15 private JFrame frame; 16 17 private final JLabel lab1 = new JLabel("请输入三角形的三条边:"); 18 private final JLabel a = new JLabel("a: "); 19 private final JLabel b = new JLabel("b: "); 20 private final JLabel c = new JLabel("c: "); 21 private final JLabel result_1 = new JLabel("这个三角形是:"); 22 private final JLabel result_2 = new JLabel("面积是:"); 23 private final JLabel result_3 = new JLabel("周长是:"); 24 25 26 private final JTextField lengthOfa = new JTextField(); 27 private final JTextField lengthOfb = new JTextField(); 28 private final JTextField lengthOfc = new JTextField(); 29 private final JTextField lengthOf1 = new JTextField(); 30 private final JTextField lengthOf2 = new JTextField(); 31 private final JTextField lengthOf3 = new JTextField(); 32 33 private final JButton calculate = new JButton("Ok"); 34 private final JButton clean = new JButton("Cancel"); 35 36 private trangle dis; 37 38 /** 39 * Launch the application. 40 */ 41 public static void main(String[] args) { 42 EventQueue.invokeLater(new Runnable() { 43 public void run() { 44 try { 45 new screen(); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 } 50 }); 51 } 52 53 /** 54 * Create the frame. 55 * @wbp.parser.entryPoint 56 */ 57 public screen() { 58 frame = new JFrame("三角形计算程序"); 59 frame.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width/2)-256, (Toolkit.getDefaultToolkit().getScreenSize().height/2)-192, 460, 374); 60 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 61 frame.getContentPane().setLayout(null); 62 frame.setVisible(true); 63 frame.setResizable(false); 64 65 lab1.setFont(new Font("宋体", Font.PLAIN, 20)); 66 lab1.setBounds(40, 30, 350, 30); 67 frame.getContentPane().add(lab1); 68 result_1.setFont(new Font("宋体", Font.PLAIN, 20)); 69 result_1.setBounds(55, 200, 350, 30); 70 frame.getContentPane().add(result_1); 71 result_2.setFont(new Font("宋体", Font.PLAIN, 20)); 72 result_2.setBounds(55, 255, 350, 30); 73 frame.getContentPane().add(result_2); 74 result_3.setFont(new Font("宋体", Font.PLAIN, 20)); 75 result_3.setBounds(250, 255, 350, 30); 76 frame.getContentPane().add(result_3); 77 78 79 a.setFont(new Font("宋体", Font.PLAIN, 20)); 80 a.setBounds(55, 80, 30, 15); 81 b.setFont(new Font("宋体", Font.PLAIN, 20)); 82 b.setBounds(175, 80, 30, 15); 83 c.setFont(new Font("宋体", Font.PLAIN, 20)); 84 c.setBounds(295, 80, 30, 15); 85 frame.getContentPane().add(a); 86 frame.getContentPane().add(b); 87 frame.getContentPane().add(c); 88 89 lengthOfa.setBounds(85, 75, 70, 25); 90 lengthOfb.setBounds(207, 75, 70, 25); 91 lengthOfc.setBounds(324, 75, 70, 25); 92 lengthOf1.setBounds(190, 202, 200, 25); 93 lengthOf2.setBounds(130,257, 110, 25); 94 lengthOf3.setBounds(325, 257, 70, 25); 95 lengthOfa.setColumns(10); 96 lengthOfb.setColumns(10); 97 lengthOfc.setColumns(10); 98 lengthOf1.setColumns(10); 99 lengthOf2.setColumns(10); 100 lengthOf3.setColumns(10); 101 frame.getContentPane().add(lengthOfa); 102 frame.getContentPane().add(lengthOfb); 103 frame.getContentPane().add(lengthOfc); 104 frame.getContentPane().add(lengthOf1); 105 frame.getContentPane().add(lengthOf2); 106 frame.getContentPane().add(lengthOf3); 107 108 calculate.setBounds(115, 140, 75, 25); 109 frame.getContentPane().add(calculate); 110 clean.setBounds(255, 140, 75, 26); 111 frame.getContentPane().add(clean); 112 113 calculate.addActionListener(new ActionListener(){ 114 public void actionPerformed(ActionEvent arg0){ 115 if(arg0.getSource()==calculate){ 116 dis = new trangle(); 117 String[] value = new String[3]; 118 value[0] = lengthOfa.getText(); 119 value[1] = lengthOfb.getText(); 120 value[2] = lengthOfc.getText(); 121 try{ 122 int a = Integer.valueOf(value[0]).intValue();//已验证过不再进行异常处理 123 int b = Integer.valueOf(value[1]).intValue(); 124 int c = Integer.valueOf(value[2]).intValue(); 125 }catch(Exception e){ 126 JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE); 127 allClean(); 128 } 129 int a = Integer.valueOf(value[0]).intValue();//已验证过不再进行异常处理 130 int b = Integer.valueOf(value[1]).intValue(); 131 int c = Integer.valueOf(value[2]).intValue(); 132 if(dis.charge(a, b, c)){ 133 JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE); 134 allClean(); 135 } 136 else{ 137 String triangleType = dis.triangle(a, b, c); 138 if(triangleType.equals("边的值不在范围内")||triangleType.equals("不构成三角形")){ 139 JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE); 140 allClean(); 141 } 142 else{ 143 String area=String.valueOf(dis.triangleArea(a, b, c)); 144 String sum = String.valueOf(dis.perimeter(a, b, c)); 145 lengthOf1.setText(dis.triangle(a, b, c)); 146 lengthOf2.setText(area); 147 lengthOf3.setText(sum); 148 } 149 } 150 } 151 } 152 } 153 ); 154 155 clean.addActionListener(new ActionListener(){ 156 public void actionPerformed(ActionEvent arg0){ 157 if(arg0.getSource()==clean){ 158 allClean(); 159 } 160 } 161 }); 162 } 163 164 private void allClean(){ 165 lengthOfa.setText(""); 166 lengthOfb.setText(""); 167 lengthOfc.setText(""); 168 lengthOf1.setText(""); 169 lengthOf2.setText(""); 170 lengthOf3.setText(""); 171 } 172 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.math.*; 2 3 public class trangle { 4 //判断三角形类型 5 public static String triangle(int a, int b,int c) 6 { 7 //值的范围 8 if(charge(a,b,c)){ 9 return "边的值不在范围内";} 10 //能否构成三角形 11 else if(a>=b+c||b>=a+c||c>=a+b){ 12 return "不构成三角形";} 13 else{ 14 //等边三角形 15 if(a==b&&b==c){ 16 return "等边三角形";} 17 //等腰三角形 18 else if(a==b||b==c||a==c){ 19 return "等腰三角形";} 20 //直角三角形 21 else if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==b*b+a*a){ 22 return "直角三角形";} 23 //一般三角形 24 else{ 25 return"一般三角形";} 26 } 27 } 28 //三角形边界值范围 29 public static boolean charge(int a, int b,int c){ 30 if(a<1||a>100||b<1||b>100||c<1||c>100) 31 return true; 32 else 33 return false; 34 } 35 //三角形周长 36 public static int perimeter(int a, int b, int c){ 37 int sum; 38 sum=a+b+c; 39 return sum; 40 } 41 //三角形面积 42 public static float triangleArea(int a,int b,int c){ 43 float area; 44 int sum = a+b+c; 45 float p = sum/2; 46 double i = p*(p-a)*(p-b)*(p-c); 47 area = (float)Math.sqrt(i); 48 return area; 49 } 50 51 }
实验结果图: