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 11:13:39 浏览: 184
RabbitMQ Java 例子
以下是一个基于Swing库实现的Java应用程序,实现了上述要求的SumItUp程序:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class SumItUp extends JFrame implements ActionListener {
private JLabel promptLabel;
private JLabel plusSignLabel;
private JLabel equalsSignLabel;
private JTextField operand1Field;
private JTextField operand2Field;
private JTextField resultField;
private JButton checkButton;
private JPanel rabbitPanel1;
private JPanel rabbitPanel2;
private int numRabbits1;
private int numRabbits2;
private Random rand;
public SumItUp() {
super("Welcome to SumItUp!");
// Set up the main content pane
Container contentPane = getContentPane();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(5, 5, 5, 5);
// Add the main prompt label
promptLabel = new JLabel("Enter two operands, result and click on 'Check!'");
contentPane.add(promptLabel, c);
// Add the first operand label and rabbit images
c.gridx = 0;
c.gridy = 1;
c.weightx = 1.0;
c.anchor = GridBagConstraints.CENTER;
rabbitPanel1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
numRabbits1 = generateRandomNumber();
addRabbitImages(rabbitPanel1, numRabbits1);
contentPane.add(rabbitPanel1, c);
c.gridx = 1;
c.weightx = 0.0;
operand1Field = new JTextField(2);
contentPane.add(operand1Field, c);
// Add the plus sign label
c.gridx = 2;
c.weightx = 0.0;
plusSignLabel = new JLabel(new ImageIcon("plus.png"));
contentPane.add(plusSignLabel, c);
// Add the second operand label and rabbit images
c.gridx = 3;
c.weightx = 1.0;
rabbitPanel2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
numRabbits2 = generateRandomNumber();
addRabbitImages(rabbitPanel2, numRabbits2);
contentPane.add(rabbitPanel2, c);
c.gridx = 4;
c.weightx = 0.0;
operand2Field = new JTextField(2);
contentPane.add(operand2Field, c);
// Add the equals sign label
c.gridx = 5;
c.weightx = 0.0;
equalsSignLabel = new JLabel("=");
contentPane.add(equalsSignLabel, c);
// Add the result label
c.gridx = 6;
c.weightx = 0.0;
resultField = new JTextField(2);
contentPane.add(resultField, c);
// Add the check button
c.gridx = 7;
c.weightx = 0.0;
checkButton = new JButton("Check!");
checkButton.addActionListener(this);
contentPane.add(checkButton, c);
// Set up the frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 200);
setVisible(true);
// Initialize the random number generator
rand = new Random();
}
private void addRabbitImages(JPanel panel, int numRabbits) {
for (int i = 0; i < numRabbits; i++) {
panel.add(new JLabel(new ImageIcon("rabbit.png")));
}
}
private int generateRandomNumber() {
return rand.nextInt(10) + 1;
}
private void resetFields() {
// Clear the text fields
operand1Field.setText("");
operand2Field.setText("");
resultField.setText("");
// Clear the rabbit images
rabbitPanel1.removeAll();
rabbitPanel2.removeAll();
// Generate new random numbers of rabbits
numRabbits1 = generateRandomNumber();
numRabbits2 = generateRandomNumber();
// Add new rabbit images
addRabbitImages(rabbitPanel1, numRabbits1);
addRabbitImages(rabbitPanel2, numRabbits2);
// Repaint the panels
rabbitPanel1.revalidate();
rabbitPanel1.repaint();
rabbitPanel2.revalidate();
rabbitPanel2.repaint();
}
public void actionPerformed(ActionEvent e) {
// Check if all fields are filled
if (operand1Field.getText().isEmpty() || operand2Field.getText().isEmpty() || resultField.getText().isEmpty()) {
return;
}
// Parse the input values
int operand1 = Integer.parseInt(operand1Field.getText());
int operand2 = Integer.parseInt(operand2Field.getText());
int result = Integer.parseInt(resultField.getText());
// Check if the input values are correct
if (operand1 + operand2 == result) {
// Display success message
promptLabel.setText("Correct! Have another go?");
// Reset the fields and generate new rabbit images
resetFields();
} else {
// Display error message
promptLabel.setText("Wrong! Try again!");
}
}
public static void main(String[] args) {
SumItUp sumitup = new SumItUp();
}
}
```
该程序使用了Swing库实现了一个GUI界面,其中包括两个操作数、一个结果和一组兔子图片。当用户在文本框中输入正确的操作数和结果后,单击“检查”按钮,程序会检查用户输入的值是否正确,并根据检查结果执行相应的操作。
阅读全文