输出多边形面积
一、使用JavaFX Scene Builder 2.0制作界面,因为比较懒,直接偷工减料😁😁😁
界面部分非常简陋
1.引入库
//main.java 部分
public class main extends Application {
@Override
public 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 {
@FXML
private TextField kuan1;
@FXML
private TextField kuan2;
@FXML
private TextField banjing3;
@FXML
private AnchorPane main;
@FXML
private TextField chang2;
@FXML
private TextField chang1;
@FXML
private TextField bian1;
@FXML
private TextField zhouchang3;
@FXML
private TextField gao2;
@FXML
private TextField zhouchang2;
@FXML
private Button b1;
@FXML
private TextField zhouchang1;
@FXML
private Button b2;
@FXML
private Button b3;
@FXML
private TextField mianji2;
@FXML
private TextField mianji3;
@FXML
private TextField mianji1;
@FXML
private Button calc1;
@FXML
private Button calc2;
@FXML
private Button calc3;
@FXML
void 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()));
}
@FXML
void 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()));
}
@FXML
void 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);
}
}
}
结果
总结
由于对界面开发这方面没有太过深入的了解,做的非常垃圾,不过可以开阔自己的知识面,增加广度。
点击此处下载:下载地址