【Java】 作业题 9.6 请输入两个整数及运算符 并输入运算结果

【Java】 作业题 9.6 GUI界面 复选框comboBox的使用

编写一个Applet,利用两个文本框对象input1和input2,接受用户从键盘输入的两个整型数。当用户点击“计算”按钮时,可进行算术运算,并输出运算结果;运算结果放在多行文本域JTextArea组件中。

题目要求修改:请输入两个整数及运算符(复选框comboBox),并输入计算结果,点击“检查”按钮(JButton),检查输入的计算结果是否正确。

在这里插入图片描述

实现代码如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ArithmeticOperator extends JFrame
{
	private Container container;
	private FlowLayout layout;
	
	JLabel prompt,equal;//输入提示语、等号
	JTextField input1,input2,input3;//三个输入框
	JTextField output;//计算判断结果显示
	JButton btn1,btn2;//检查、退出按钮
	JComboBox comboBox;//运算符复选框
	private int operator=0;//运算符(0-3分别代表+-*/)
	
	public ArithmeticOperator()
	{
		super("计算测试");
		container=getContentPane();
		layout=new FlowLayout();
		container.setLayout(layout);
		
		String[] operators={"+","-","*","/"};
		comboBox=new JComboBox(operators);
		comboBox.setMaximumRowCount(4);
		comboBox.addItemListener(
			new ItemListener()
			{
				public void itemStateChanged(ItemEvent event)
				{
					if(event.getStateChange()==ItemEvent.SELECTED)
						operator=comboBox.getSelectedIndex();//选中运算符
				}
			}
		);
		
		prompt=new JLabel("请输入两个整数及运算符,并输入运算结果:");
		equal=new JLabel("  =  ");
		input1=new JTextField(5);
		input2=new JTextField(5);
		input3=new JTextField(5);
		btn1=new JButton("检查");
		btn2=new JButton("退出");
		output=new JTextField(20);
		output.setEditable(false);
		
		btn1.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					int a,b,c,d=0;//注意d要初始化值,否则会报错
					a=Integer.parseInt(input1.getText());
					b=Integer.parseInt(input2.getText());
					c=Integer.parseInt(input3.getText());//分别从三个输入框得到数字
					if(operator==0)
						d=a+b;
					else if(operator==1)
						d=a-b;
					else if(operator==2)
						d=a*b;
					else if(operator==3)
						d=a/b;//按照当前选中的运算符,得到实际的计算结果
					String s1="对了!你真棒!";
					String s2="不对呀,你再仔细计算一下";
					if(c==d)
						output.setText(s1);
					else if(c!=d)
						output.setText(s2);//显示计算判断结果
				}
			}
		);//检查按钮
		
		btn2.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					System.exit(0);
				}
			}
		);//退出按钮
		
		container.add(prompt);
		container.add(input1);
		container.add(comboBox);
		container.add(input2);
		container.add(equal);
		container.add(input3);
		container.add(btn1);
		container.add(btn2);
		container.add(output);//添加组件
		
		setSize(320,160);//设置窗口大小
		setVisible(true);
	}
	public static void main(String[] args)
	{
		ArithmeticOperator application=new ArithmeticOperator();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

运行结果截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

球王武磊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值