Question: Java for beginner: This program implements a flash card to teach multiplication. Write a program that uses a WidgetViewer object (the WidgetView class is available

Java for beginner: This program implements a flash card to teach multiplication. Write a program that uses a WidgetViewer object (the WidgetView class is available elsewhere in this Lesson). your program should use a Random object to generate two random numbers between 0 and 9 (inclusive). To explain the operation of this program, we'll assume that our random number generator generated 6 and 3. display a JLabel with the text "What is 6 times 3?" create an empty JTextField to hold the user's answer create a JButton that has the text "click after answering" The user should put his or her guess in the JTextField and click the JButton. When the button is clicked, the program should get the text from the JTextField, convert it from String to int, and create a JLabel that says either That's right. Good Job, or Sorry, the correct answer is 18 depending on whether the user input the correct number (18 in this case).

These are the widgetviewer.java codes given

import java.awt.event.ActionListener;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

import javax.swing.AbstractButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.JButton;

/**

* A really simple class to display Swing widgets in absolute Layout GUI.

*

*

* @author parks

*/

public class WidgetViewer {

/**

* This is the width of the JFrame if no value is specified on the constructor

*/

public static final int DEFAULT_X_SIZE = 600;

/**

* This is the height of the JFrame if no value is specified on the constructor

*/

public static final int DEFAULT_Y_SIZE = 400;

private JFrame jframe;

private JPanel anchor;

private boolean userClicked = false;

private Lock lock;

private Condition waitingForUser;

private JComponent userInputComponent = null;

private ActionListener eventHandler;

/**

* Default constructor will display an empty JFrame that has a Absolute layout

* JPanel as its content pane and initialize the frame to a default size.

*/

public WidgetViewer() {

this(DEFAULT_X_SIZE, DEFAULT_Y_SIZE);

}

/**

* Constructor will display an empty JFrame that has a Flow layout JPanel as its

* content pane and initialize the frame to a given size.

*/

public WidgetViewer(int pixelSizeInX, int pixelSizeInY) {

lock = new ReentrantLock();

waitingForUser = lock.newCondition();

// lambda expression requires Java 8

eventHandler = e -> {

if (e.getSource() != userInputComponent) {

return;

}

lock.lock();

userClicked = true;

waitingForUser.signalAll();

lock.unlock();

JComponent jbx = (JComponent) e.getSource();

anchor.remove(jbx);

};

jframe = new JFrame();

anchor = new JPanel();

anchor.setLayout(null);

jframe.setContentPane(anchor);

jframe.setSize(pixelSizeInX, pixelSizeInY);

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jframe.setVisible(true);

}

/**

* Add a Swing widget to the GUI.

* GUI coordinates start at the top left corner of the frame, and

* are measured in pixels.

*

    *

  • x increases horizontally to (to the right)

    *

  • y increases vertically GOING DOWN.

    *

*

* @param jcomp

* Swing widget (subclasses of JComponent--like JLabel and

* JTextField) to be added to the JFrame

*

* @param x the x value of this widget's top left corner

* @param y the y value of this widget's top left corner

* @param w the width, in pixels, of this widget

* @param h the height, in pixels, of this widget

*/

public void add(JComponent jcomp, int x, int y, int w, int h) {

jcomp.setBounds(x, y, w, h);

anchor.add(jcomp);

jframe.setContentPane(anchor);

}

/**

* Add an Abstract Button (like a JButton) to the JFrame. Create an action

* listener to wait (suspend the caller) until it is clicked.

*

* @param absButton

* Button (like a JButton) to add to the JFrame

*/

public void addAndWait(AbstractButton absButton) {

userInputComponent = absButton;

absButton.addActionListener(eventHandler);

addWait(absButton, absButton.getText().length() + 2);

}

/**

* Convenience method to create a JButton with the given text and use it

* in an addAndWait.

*

* @param prompt Text for the JButton to display

*/

public void addButtonAndWait(String prompt) {

JButton jb = new JButton(prompt);

addAndWait(jb);

}

/**

* Add a JTextField to the JFrame, and wait for the user to put the cursor in

* the field and click Enter. The caller is suspended until enter is clicked.

*

* @param jTextField

* Field to add to the JFrame

*/

public void addAndWait(JTextField jTextField) {

userInputComponent = jTextField;

jTextField.addActionListener(eventHandler);

addWait(jTextField, jTextField.getText().length() + 2);

}

@SuppressWarnings("unused")

private void addWait(JComponent jcomp) {

addWait(jcomp, 5);

}

private void addWait(JComponent jcomp, int charWidth) {

int guessAtWidth = Math.min(charWidth * 10, jframe.getWidth());

add(jcomp, 0, 10, guessAtWidth, 20);

lock.lock();

try {

while (!userClicked) {

waitingForUser.await();

}

// we need this to make the just clicked widget disappear in some circumstances

jframe.setContentPane(anchor);

} catch (InterruptedException e1) {

System.err.println("WidgetViewer reports that something really bad just happened");

e1.printStackTrace();

System.exit(0);

}

userClicked = false;

waitingForUser.signalAll();

lock.unlock();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!