java gui(MVC期末考试上机题)

package gui;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/*
 * 姓名:黄柯翔评讲模拟考试
 */
public class MyFrame extends JFrame implements ActionListener {

	JLabel firstInputLab;
	JTextField firstInputTxt;

	JLabel secondInputLab;
	JTextField secondInputTxt;

	JButton btnZhouchang;
	JButton btnMianji;
	JButton btnRest;

	JLabel outputLab;
	JTextField outputTxt;

	Container container;

//	属性声明组件
	public MyFrame() {
		this.setTitle("我也是标题");
		this.setSize(450, 350);
		this.setDefaultCloseOperation(3);
		this.setLocationRelativeTo(null);
		container = this.getContentPane();
		container.setLayout(null);

		firstInputLab = new JLabel("输入矩形的长:");
		firstInputLab.setBounds(100, 50, 100, 30);
		container.add(firstInputLab);

		firstInputTxt = new JTextField();
		firstInputTxt.setBounds(200, 50, 150, 30);
		container.add(firstInputTxt);

		firstInputLab = new JLabel("输入矩形的宽:");
		firstInputLab.setBounds(100, 100, 100, 30);
		container.add(firstInputLab);

		secondInputTxt = new JTextField();
		secondInputTxt.setBounds(200, 100, 150, 30);
		container.add(secondInputTxt);

		btnZhouchang = new JButton("求周长");
		btnZhouchang.setBounds(50, 150, 80, 30);
		container.add(btnZhouchang);

		btnMianji = new JButton("求面积");
		btnMianji.setBounds(150, 150, 80, 30);
		container.add(btnMianji);

		btnRest = new JButton("重置");
		btnRest.setBounds(250, 150, 80, 30);
		container.add(btnRest);

		outputTxt = new JTextField();
		outputTxt.setBounds(150, 200, 200, 30);
		container.add(outputTxt);

		outputLab = new JLabel("结果输出:");
		outputLab.setBounds(80, 200, 100, 30);
		container.add(outputLab);

		btnZhouchang.addActionListener(this);
		btnMianji.addActionListener(this);
		btnRest.addActionListener(this);

		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnZhouchang) {
			// 周长]
			try {
				int firstNum = Integer.parseInt(firstInputTxt.getText().trim());// "55" --> 55
				int secondNum = Integer.parseInt(secondInputTxt.getText().trim());// "55" --> 55
				outputTxt.setText("" + ((firstNum + secondNum) * 2));
			} catch (NumberFormatException e1) {
				outputTxt.setText("输入有误或未输入");
			}

		} else if (e.getSource() == btnMianji) {

			// 周长
			try {
				int firstNum = Integer.parseInt(firstInputTxt.getText().trim());// "55" --> 55
				int secondNum = Integer.parseInt(secondInputTxt.getText().trim());// "55" --> 55
				outputTxt.setText("" + ((firstNum * secondNum)));
			} catch (NumberFormatException e1) {
				outputTxt.setText("输入有误或未输入");
			}

		} else if (e.getSource() == btnRest) {
			// 重置
			firstInputTxt.setText("");
			secondInputTxt.setText("");
		}

	}

	public static void main(String[] args) {
		new MyFrame();
	}
}

// 工具eclipse :
//alt + / 代码提示.不要手工一个字母一个字母
// 注释: ctrl + /

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值