A random number of Rabbit images ranging from 1 to 10 are displayed for each operand and the user is expected to enter the values of the two operands and the result of adding the two operands, in the given text fields. When the user clicks on the button ‘Check!’, one of two things can happen: Case 1: all three input values are correct i) the text changes to ‘"Correct! Have another go?"’. ii) the number of Rabbit images displayed for each of the two operands changes. See Figure 2 for an example. iii) the three text fields are reset (i.e. they are made empty). 2/5 Case 2: at least one of the input values entered is incorrect i) the text changes to ‘Wrong! Try again!’. ii) the number of Rabbit images displayed does NOT change. iii) the text fields do NOT change.Implement SumItUp as a Java application. You application must satisfy ALL the specific requirements given below: a) The title of the top-level container must be ‘Welcome to SumItUp!’. b) The initial text should be ‘Enter two operands, result and click on 'Check!'’. See Figure 1. c) There should be no more than 4 Rabbit images per row. See Hint 1. d) The text fields should be wide enough to display at least TWO characters. e) The button ‘Check!’ must not resize when the GUI is resized. See Hint 2 and Figure 3. f) The ‘plus sign’ icon should appear vertically centered between the two sets of Rabbit images and must not resize when the GUI is resized. See Hint 2 and Figure 3. g) When first launched and whenever a correct answer is given, the number of displayed Rabbit images for each operand should change to any number between 1 and 10 (inclusive). See Hint 3 and Hint 4. Note: It is possible for the next number(s) to be the same as the current number(s). h) Nothing should happen if the user clicks the ‘Check!’ button while at least one of the text fields are empty, i.e. no errors should be thrown in this case. Note: You can assume that only a numeric value will be entered into the text fields.

时间: 2024-02-14 15:13:40 浏览: 20
Sure, I can help you with that. Here's the Java code for the SumItUp application: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SumItUp extends JFrame implements ActionListener { private JPanel topPanel, centerPanel, bottomPanel; private JLabel rabbitLabel1, rabbitLabel2, plusLabel, equalsLabel, resultLabel; private JTextField operand1Field, operand2Field, resultField; private JButton checkButton; private int rabbitCount1, rabbitCount2; public SumItUp() { setTitle("Welcome to SumItUp!"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 300); // Create top panel topPanel = new JPanel(); topPanel.add(new JLabel("Enter two operands, result and click on 'Check!'")); add(topPanel, BorderLayout.NORTH); // Create center panel centerPanel = new JPanel(new GridLayout(1, 5, 10, 10)); rabbitCount1 = (int) (Math.random() * 10) + 1; rabbitCount2 = (int) (Math.random() * 10) + 1; for (int i = 0; i < rabbitCount1; i++) { centerPanel.add(new JLabel(new ImageIcon("rabbit.png"))); } plusLabel = new JLabel(new ImageIcon("plus.png")); plusLabel.setVerticalAlignment(JLabel.CENTER); centerPanel.add(plusLabel); for (int i = 0; i < rabbitCount2; i++) { centerPanel.add(new JLabel(new ImageIcon("rabbit.png"))); } equalsLabel = new JLabel(new ImageIcon("equals.png")); equalsLabel.setVerticalAlignment(JLabel.CENTER); centerPanel.add(equalsLabel); resultField = new JTextField(2); centerPanel.add(resultField); add(centerPanel, BorderLayout.CENTER); // Create bottom panel bottomPanel = new JPanel(new GridLayout(1, 4, 10, 10)); rabbitLabel1 = new JLabel(rabbitCount1 + " rabbits"); bottomPanel.add(rabbitLabel1); operand1Field = new JTextField(2); bottomPanel.add(operand1Field); bottomPanel.add(new JLabel("+")); operand2Field = new JTextField(2); bottomPanel.add(operand2Field); rabbitLabel2 = new JLabel(rabbitCount2 + " rabbits"); bottomPanel.add(rabbitLabel2); resultLabel = new JLabel(""); bottomPanel.add(resultLabel); checkButton = new JButton("Check!"); checkButton.addActionListener(this); bottomPanel.add(checkButton); add(bottomPanel, BorderLayout.SOUTH); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == checkButton) { if (!operand1Field.getText().equals("") && !operand2Field.getText().equals("") && !resultField.getText().equals("")) { int operand1 = Integer.parseInt(operand1Field.getText()); int operand2 = Integer.parseInt(operand2Field.getText()); int result = Integer.parseInt(resultField.getText()); if (operand1 + operand2 == result) { rabbitCount1 = (int) (Math.random() * 10) + 1; rabbitCount2 = (int) (Math.random() * 10) + 1; centerPanel.removeAll(); for (int i = 0; i < rabbitCount1; i++) { centerPanel.add(new JLabel(new ImageIcon("rabbit.png"))); } plusLabel = new JLabel(new ImageIcon("plus.png")); plusLabel.setVerticalAlignment(JLabel.CENTER); centerPanel.add(plusLabel); for (int i = 0; i < rabbitCount2; i++) { centerPanel.add(new JLabel(new ImageIcon("rabbit.png"))); } equalsLabel = new JLabel(new ImageIcon("equals.png")); equalsLabel.setVerticalAlignment(JLabel.CENTER); centerPanel.add(equalsLabel); resultField.setText(""); operand1Field.setText(""); operand2Field.setText(""); rabbitLabel1.setText(rabbitCount1 + " rabbits"); rabbitLabel2.setText(rabbitCount2 + " rabbits"); resultLabel.setText("Correct! Have another go?"); } else { resultLabel.setText("Wrong! Try again!"); } } } } public static void main(String[] args) { SumItUp app = new SumItUp(); } } ``` Note: This code assumes that you have images named "rabbit.png", "plus.png", and "equals.png" in the same directory as your Java source file. If you want to use different images, you'll need to modify the code accordingly.

相关推荐

最新推荐

recommend-type

IEEE Std 802.15.4z-2020 IEEE Standard(原版非图片).pdf

IEEE Std 802.15.4z-2020 IEEE Standard for Low Rate Wireless Networks Amendment 1: Enhanced Ultra Wideband (UWB) Physical Layers(PHYs) and Associated Ranging Techniquesfinal(原版非图片) Abstract: ...
recommend-type

lms激光系列通信协议

SICK激光雷达通信协议,官网下载,配合博文介绍及配套上传...Technical_information_Telegram_Listing_Ranging_sensors_LMS1xx_LMS5xx_TiM5xx_MRS1000_MRS6000_NAV310_LD_OEM15xx_LD_LRS36xx_LMS4000_en_IM0045927.PDF
recommend-type

基于matlab实现人工免疫算法的解决TSP问题的方法

基于matlab实现人工免疫算法的解决TSP问题的方法,体现了免疫算法在进化计算过程中的抗原学习、记忆机制、浓度调节机制以及多样性抗体保持策略等优良特性.rar
recommend-type

麦肯锡图表绘制培训.pptx

麦肯锡图表绘制培训.pptx
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】增量式PID的simulink仿真实现

# 2.1 Simulink仿真环境简介 Simulink是MATLAB中用于建模、仿真和分析动态系统的图形化环境。它提供了一个直观的用户界面,允许用户使用块和连接线来创建系统模型。Simulink模型由以下元素组成: - **子系统:**将复杂系统分解成更小的、可管理的模块。 - **块:**代表系统中的组件,如传感器、执行器和控制器。 - **连接线:**表示信号在块之间的流动。 Simulink仿真环境提供了广泛的块库,涵盖了各种工程学科,包括控制系统、电子和机械工程。它还支持用户自定义块的创建,以满足特定仿真需求。 # 2. Simulink仿真环境的搭建和建模 ### 2.
recommend-type

训练集和测试集的准确率都99%,但是预测效果不好

即使训练集和测试集的准确率都很高,但是在实际的预测中表现不佳,可能会有以下几个原因: 1. 数据质量问题:模型训练和测试的数据可能存在问题,比如数据标签错误、数据集采样不均衡、数据集中存在异常值等问题,这些问题可能会导致模型在实际预测中表现不佳。 2. 特征工程问题:模型的特征工程可能存在问题,导致模型无法很好地捕捉数据的特征,从而影响预测效果。 3. 模型选择问题:模型的选择可能不合适,比如选择的模型太简单,无法很好地拟合数据,或者选择的模型太复杂,导致过拟合等问题。 4. 超参数调整问题:模型的超参数可能没有调整到最佳状态,需要进行调整。 针对以上可能的原因,可以采取相应的措施进
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依