第4次作业类测试代码+142+王娇

类图:

代码部分:

 1 package sample;
 2 
 3 import javafx.application.Application;
 4 import javafx.event.EventHandler;
 5 import javafx.fxml.FXMLLoader;
 6 import javafx.scene.Parent;
 7 import javafx.scene.Scene;
 8 import javafx.scene.control.Alert;
 9 import javafx.scene.control.Button;
10 import javafx.scene.control.TextField;
11 import javafx.scene.input.MouseEvent;
12 import javafx.stage.Stage;
13 
14 
15 public class Main extends Application {
16 
17     @Override
18     public void start(Stage primaryStage) throws Exception {
19         Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
20         primaryStage.setTitle("计算三角形面积--王娇");
21         primaryStage.setScene(new Scene(root, 600, 400));
22         TextField mTextFiled_A = (TextField) root.lookup("#textField_A");
23         TextField mTextFiled_B = (TextField) root.lookup("#textField_B");
24         TextField mTextFiled_C = (TextField) root.lookup("#textField_C");
25         TextField mTextFiled_tan = (TextField) root.lookup("#textField_tan");
26         TextField mTextFiled_tanS = (TextField) root.lookup("#textField_tanS");
27         TextField mTextFiled_tanC = (TextField) root.lookup("#textField_tanC");
28         Button button_OK = (Button) root.lookup("#button_OK");
29         button_OK.setOnMouseClicked(new EventHandler<MouseEvent>() {
30             @Override
31             public void handle(MouseEvent event) {
32                 try {
33                     int a = Integer.parseInt(mTextFiled_A.getText());
34                     int b = Integer.parseInt(mTextFiled_B.getText());
35                     int c = Integer.parseInt(mTextFiled_C.getText());
36                     String result = triangle(a, b, c);
37                     mTextFiled_tan.setText(result);
38                     if (!result.equals("边的值不在范围内!") && !result.equals("不构成三角形")) {
39                         mTextFiled_tanS.setText(tanS(a, b, c) + "");
40                         mTextFiled_tanC.setText(tanC(a, b, c) + "");
41                     } else {
42                         mTextFiled_tanS.setText("0");
43                         mTextFiled_tanC.setText("0");
44                     }
45                 }
46                 catch (Exception e){
47                     Alert alert = new Alert(Alert.AlertType.INFORMATION);
48                     alert.setHeaderText("请输入数字");
49                     alert.show();
50                 }
51             }
52         });
53         primaryStage.show();
54     }
55 
56     public static String triangle(int a, int b, int c) {
57         if (!((1 <= a && a <= 100) && (1 <= b && b <= 100) && (1 <= c && c <= 100)))
58             return "边的值不在范围内!";
59         if (!((a < b + c) && (b < a + c) && (c < a + b)))
60             return "不构成三角形";
61         if ((a == b) || (b == c) || (c == a))
62             if (a == b && b == c)
63                 return "等边三角形";
64             else
65                 return "等腰三角形";
66         else {
67             if ((a * a == b * b + c * c) || (b * b == a * a + c * c) || (c * c == a * a + b * b))
68                 return "直角三角形";
69             return "一般三角形";
70         }
71     }
72 
73     public static double tanS(int a, int b, int c) {
74         int p = (a + b + c) / 2;
75         return (Math.sqrt(p * (p - a) * (p - b) * (p - c))) ;
76     }
77 
78     public static double tanC(int a, int b, int c) {
79         return (a + b + c) ;
80     }
81 
82     public static void main(String[] args) {
83         launch(args);
84     }
85 }

界面控制:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button id="button_OK" layoutX="246.0" layoutY="135.0" mnemonicParsing="false" text="OK" />
      <Button layoutX="305.0" layoutY="134.0" mnemonicParsing="false" text="Cancel" />
      <TextField id="textField_A" layoutX="134.0" layoutY="94.0" prefHeight="23.0" prefWidth="62.0" />
      <TextField id="textField_B" layoutX="296.0" layoutY="95.0" prefHeight="23.0" prefWidth="62.0" />
      <TextField id="textField_C" layoutX="445.0" layoutY="95.0" prefHeight="23.0" prefWidth="55.0" />
      <TextField id="textField_tan" editable="false" layoutX="226.0" layoutY="189.0" />
      <Text layoutX="125.0" layoutY="203.0" strokeType="OUTSIDE" strokeWidth="0.0" text="这个三角形是:" wrappingWidth="87.0" />
      <Text layoutX="148.0" layoutY="249.0" strokeType="OUTSIDE" strokeWidth="0.0" text="面积是:" />
      <Text layoutX="314.0" layoutY="249.0" strokeType="OUTSIDE" strokeWidth="0.0" text="周长是:" />
      <Text layoutX="148.0" layoutY="63.0" strokeType="OUTSIDE" strokeWidth="0.0" text="请输入三角形的三条边;" />
      <Text layoutX="110.0" layoutY="110.0" strokeType="OUTSIDE" strokeWidth="0.0" text="a:" />
      <Text layoutX="275.0" layoutY="111.0" strokeType="OUTSIDE" strokeWidth="0.0" text="b:" />
      <Text layoutX="425.0" layoutY="109.0" strokeType="OUTSIDE" strokeWidth="0.0" text="c:" />
      <TextField id="textField_tanS" editable="false" layoutX="196.0" layoutY="233.0" prefHeight="23.0" prefWidth="62.0" />
      <TextField id="textField_tanC" editable="false" layoutX="365.0" layoutY="234.0" prefHeight="23.0" prefWidth="62.0" />
   </children>
</Pane>

界面:

三角形,面积,周长点击设置只读,不可编辑。

输入非数字

输入正确

posted @ 2017-05-03 16:13  王娇(wj)  阅读(272)  评论(0编辑  收藏  举报