JavaGUI、接口与内部类应用

应用实例1——求一个数的平方

【题目要求】

通过内部类设计GUI JFrame进行事件处理输入一个数求解改数的平方

【输出样例】
在这里插入图片描述
【代码分析】

package ml; //存放的包名
import java.awt.*; //引入包
import java.awt.event.*;
import javax.swing.*;
public class TestWindow extends JFrame
{
	private JLabel aLabel;
	private JTextField aField,displayField;
	private JButton computeButton,exitButton;
	public TestWindow()
	{
		super("内部类的使用:计算一个数的平方");
		Container container = getContentPane();
		container.setLayout(new FlowLayout());
		aLabel = new JLabel("输入一个整数:");
		aField = new JTextField(10);
		container.add(aLabel);
		container.add(aField);
		displayField = new JTextField(30);
		displayField.setEditable(false);
		container.add(displayField);
		computeButton = new JButton("计算平方");
		container.add(computeButton);
		exitButton = new JButton("退出");
		container.add(exitButton);
		ActionEventHandler handler = new ActionEventHandler();
		computeButton.addActionListener(handler);
		exitButton.addActionListener(handler);
		setSize(447,140);
		setVisible(true);
	}
	public static void main(String args[])
	{
		TestWindow window = new TestWindow();
	}
	private class ActionEventHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			if(event.getSource()==exitButton)
				System.exit(0);
			else if(event.getSource()==computeButton)
			{
				String a = aField.getText();
				int ai = Integer.parseInt(a);
				ai = ai*ai;
				String b = String.valueOf(ai);
				displayField.setText(a+" 的平方是:"+b);
			}
		}
	}
}

【结果输出】
在这里插入图片描述

应用实例2——求一元二次方程根

【题目要求】

Define an Equation class for computing the two real roots of the equation, and Design GUI JFrame by nested anonymous inner class for event handling. The GUI of the application is designed as follows, and the precision is two digits on the right side of decimal point.

定义一个Equation类来计算方程的两个实根,并通过嵌套的内部类设计GUI JFrame进行事件处理。 应用程序的GUI设计如下,精度保留小数点后两位。

【输出样例】

在这里插入图片描述
在这里插入图片描述
【代码分析】

package ml; //存放的包名
import java.awt.*; //引用包
import java.awt.event.*;
import javax.swing.*;
class Equition //定义求根的类
{
private
	double a,b,c; //方程的系数
	double x1,x2; //两个根
	double r;
    double v;
    int type; //0表示一个根,1表示两个实根,2表示两个复数的根
public
    double delta()
	{
		return b*b-4*a*c;
	}
	boolean computeRoots()
	{
		boolean right=(delta()>=0)?true:false;
		return right;
	}
	Equition(double a1,double b1,double c1)
	{
		a = a1;b = b1;c = c1; 
		double d = delta();
		if(Math.abs(d) < 1E-5)
		{
			type = 0;
			x1 = -b/(2*a);
			x2 = x1;
		}
		else if(d > 0)
		{
			type = 1;
			x1 = (-b+Math.sqrt(d))/(2*a);
			x2 = (-b-Math.sqrt(d))/(2*a);
		}
		else
		{
			type = 2;
			r =-b/(2*a);
			v = Math.sqrt(-d)/(2*a);
		}
	}
	double getX1()
	{
		return x1;
	}
	double getX2()
	{
		return x2;
	}
}
public class TestWindow extends JFrame
{
	private JLabel aLabel,bLabel,cLabel;
	private JTextField aField,bField,cField,displayField;
	private JButton computeButton,exitButton;
	public TestWindow() //创建GUI
	{
		super("Compute roots of equiton"); //调用构造函数设置标题栏字符串
		Container container = getContentPane(); //创建一个容器类
		container.setLayout(new FlowLayout());
		aLabel = new JLabel("a:");
		aField = new JTextField(10);
		container.add(aLabel);
		container.add(aField);
		Container container_1 = getContentPane();
		container.setLayout(new FlowLayout());
		bLabel = new JLabel("b:");
		bField = new JTextField(10);
		container_1.add(bLabel);
		container_1.add(bField);
		Container container_2 = getContentPane();
		container.setLayout(new FlowLayout());
		cLabel = new JLabel("c:");
		cField = new JTextField(10);
		container_2.add(cLabel);
		container_2.add(cField);
		displayField = new JTextField(30);
		displayField.setEditable(false);
		container.add(displayField);
		computeButton = new JButton("Compute roots");
		container.add(computeButton);
		exitButton = new JButton("exit");
		container.add(exitButton);
		//创建内部类ActionEventHandler的实例
		ActionEventHandler handler = new ActionEventHandler();
		computeButton.addActionListener(handler); //computeButton事件处理程序
		exitButton.addActionListener(handler); //exitButton事件处理程序
		setSize(600,240);
		setVisible(true);
	}
	public static void main(String[] args) 
	{
		TestWindow window = new TestWindow();
	}
	//内部类声明处理JButton事件
	private class ActionEventHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent event) //处理事件的方法
		{
			if(event.getSource()==exitButton)
				System.exit(0);
			else if(event.getSource()==computeButton)
			{
				String a=aField.getText();
				String b=bField.getText();
				String c=cField.getText();
				double ai=Double.parseDouble(a);
				double bi=Double.parseDouble(b);
				double ci=Double.parseDouble(c);
				Equition e = new Equition(ai,bi,ci);
				boolean right = e.computeRoots();
				if(!right) //如果该方程无实根
					displayField.setText("The equition has no real root.");
				else
				{
					String R_1=String.format("%.2f", e.getX1()).toString(); //保留小数点后两位
					String R_2=String.format("%.2f", e.getX2()).toString();
					displayField.setText("The two root: x1="+R_1+" , x2="+R_2); //正确输出答案
				}
			}
		}
	}
}

【结果输出】
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芷汀若静

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值