输出多边形面积
一、使用JavaFX Scene Builder 2.0制作界面,因为比较懒,直接偷工减料😁😁😁
界面部分非常简陋
1.引入库
//main.java 部分
public class main extends Application {@Overridepublic void start(Stage primaryStage) {try {// Read file fxml and draw interface.Parent root = FXMLLoader.load(getClass().getResource("windows.fxml"));primaryStage.setTitle("My Application");primaryStage.setScene(new Scene(root));primaryStage.show();} catch(Exception e) {e.printStackTrace();}}public static void main(String[] args) {launch(args);}}
//controller.java部分
package application;import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
//controller.java 部分
package application;import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
2.代码部分
controller.java
public class WindowsController {@FXMLprivate TextField kuan1;@FXMLprivate TextField kuan2;@FXMLprivate TextField banjing3;@FXMLprivate AnchorPane main;@FXMLprivate TextField chang2;@FXMLprivate TextField chang1;@FXMLprivate TextField bian1;@FXMLprivate TextField zhouchang3;@FXMLprivate TextField gao2;@FXMLprivate TextField zhouchang2;@FXMLprivate Button b1;@FXMLprivate TextField zhouchang1;@FXMLprivate Button b2;@FXMLprivate Button b3;@FXMLprivate TextField mianji2;@FXMLprivate TextField mianji3;@FXMLprivate TextField mianji1;@FXMLprivate Button calc1;@FXMLprivate Button calc2;@FXMLprivate Button calc3;@FXMLvoid action1(ActionEvent event) {double jcc = Double.parseDouble(chang1.getText());double jkk = Double.parseDouble(kuan1.getText());rectangle rectangleobject = new rectangle(jcc, jkk);zhouchang1.setText(String.format("%.2f", rectangleobject.zhouchang()));mianji1.setText(String.format("%.2f", rectangleobject.mianji()));}@FXMLvoid action3(ActionEvent event) {double ybb = Double.parseDouble(banjing3.getText());circular circularobject = new circular(ybb);zhouchang3.setText(String.format("%.2f", circularobject.zhouchang()));mianji3.setText(String.format("%.2f", circularobject.mianji()));}@FXMLvoid action2(ActionEvent event) {double shangdi = Double.parseDouble(chang2.getText());double xiadi = Double.parseDouble(kuan2.getText());double gao = Double.parseDouble(gao2.getText());double yao1 = Double.parseDouble(bian1.getText());double yao2 = Double.parseDouble(bian1.getText());tixing t1 = new tixing(shangdi, xiadi, gao, yao1, yao2);zhouchang2.setText(String.format("%.2f", t1.zhouchang()));mianji2.setText(String.format("%.2f", t1.mianji()));}private abstract class shape{private double length;private double angle;abstract double zhouchang();abstract double mianji();}//矩形类public class rectangle extends shape{// 属性double edge1, edge2;// 方法rectangle(double edge1, double edge2) {this.edge1 = edge1;this.edge2 = edge2;}double zhouchang() {return (2 * edge1 + 2 * edge2);}double mianji() {return (edge1 * edge2);}}//圆形类public class circular extends shape{// 属性double radius;// 方法circular(double radius) {this.radius = radius;}double zhouchang() {return (2 * radius * 3.14);}double mianji() {return (radius * radius * 3.14);}}//梯形类public class tixing extends shape{// 属性double upperline, underline, high, xiebian1, xiebian2;// 方法tixing(double upperline, double underline, double xiebian1, double xiebian2, double high) {this.upperline = upperline;this.underline = underline;this.xiebian1 = xiebian1;this.xiebian2 = xiebian2;this.high = high;}double zhouchang() {return (upperline + underline + xiebian1 + xiebian2);}double mianji() {return ((upperline + underline) * high * 0.5);}}}
结果
总结
由于对界面开发这方面没有太过深入的了解,做的非常垃圾,不过可以开阔自己的知识面,增加广度。
点击此处下载:下载地址