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 10:13:40 浏览: 73
RabbitMQ Java 例子
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.
阅读全文