一、实验目的
结合java中GUI的相关知识,设计一个矩形面积求解界面。要求界面如下图所示:
二、实验代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
2.读入数据
代码如下:
MyFrame.java
public class MyFrame {
public static void main(String[] args) {
// TODO 自动生成的方法存根
WindowFrame win = new WindowFrame();
}
}
Rectange.java
public class Rectange {
double len;
double wide;
public void setLen(double len) {
this.len = len;
}
public void setWide(double wide) {
this.wide = wide;
}
public double SetC() {
double m = 2*(wide+len);
return m;
}
public double SetArea() {
double n = wide*len;
return n;
}
}
WindowFrame.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WindowFrame extends JFrame implements ActionListener {
Rectange r;//实体
JButton b1;
JButton b2;
JButton b3;
JTextField f1;
JTextField f2;
JTextArea showAC;
WindowFrame(){
init();
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void init() {
this.setBounds(100, 100, 750, 700);
this.setTitle("求矩形的周长和面积");
this.setLayout(null);
JLabel lab1 = new JLabel("请输入矩形的长:");
lab1.setBounds(120, 120,100, 80);
this.add(lab1);
JLabel lab2 = new JLabel("请输入矩形的宽:");//视图
lab2.setBounds(120, 250,100, 80);
this.add(lab2);
f1 = new JTextField();
f1.setBounds(230,120, 200, 50);
this.add(f1);
f2 = new JTextField();
f2.setBounds(230,250, 200, 50);
this.add(f2);
b1 = new JButton("求周长");
b1.setBounds(100, 380, 80, 50);
this.add(b1);
b2 = new JButton("求面积");
b2.setBounds(300, 380, 80, 50);
this.add(b2);
b3 = new JButton("重置");
b3.setBounds(530, 380, 80, 50);
this.add(b3);
JLabel lab3 = new JLabel("结果为:");
lab3.setBounds(120, 520, 100, 80);
this.add(lab3);
showAC = new JTextArea();//控制器
showAC.setBounds(230, 520, 300,80);//控制器
this.add( showAC);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
double m = Double.parseDouble(f1.getText().trim());
double n = Double.parseDouble(f2.getText().trim());
r = new Rectange();
r.setLen(m);
r.setWide(n);
double x = r.SetC();
double y = r.SetArea();
if(e.getSource()==b1) {
showAC.setText("周长为"+x);
}
else if(e.getSource()==b2) {
showAC.setText("面积为"+y);
}
else if(e.getSource()==b3) {
f1.setText("");
f2.setText("");
}
}catch(Exception ex) {
System.out.println(ex);
}
}
}