基于MVC的GUI矩形面积求解



一、实验目的

结合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);
		}
		
	}
	
	
}


三、结果界面

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值