Java Programming- GUI Strings This program implements a flash card to teach mult
ID: 3788431 • Letter: J
Question
Java Programming- GUI Strings This program implements a flash card to teach multiplication. Write a program that uses a WidgetView 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).Explanation / Answer
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TestView {
public static Random numGen =new Random();
public static int RandNum(){
int rand = Math.abs((1)+numGen.nextInt(9));
return rand;
}
public static void main(String[]Args){
//final int x = RandNum();
//final int y= RandNum();
final int x = 6;
final int y= 3;
final WidgetView wigView = new WidgetView();
JComponent compX = new JLabel("Value of x : ");
compX.setBounds(50,50, 150,20);
JComponent compXVal = new JLabel(Integer.toString(x));
compXVal.setBounds(200,50, 150,20);
wigView.add(compX);
wigView.add(compXVal);
JComponent compY = new JLabel("Value of y : ");
compY.setBounds(50,100, 150,20);
JComponent compYVal = new JLabel(Integer.toString(y));
compYVal.setBounds(200,100, 250,20);
wigView.add(compY);
wigView.add(compYVal);
JComponent compOption = new JLabel("What is 6 times 3?");
compOption.setBounds(50,150, 200,20);
wigView.add(compOption);
final JTextField compUserInput = new JTextField();
compUserInput.setBounds(250,150, 150, 20);
wigView.addAndWait(compUserInput);
final JTextField compResult = new JTextField("Result :");
compResult.setBounds(50,280, 200,20);
compResult.setEnabled(false);
compResult.setVisible(false);
wigView.add(compResult);
/*JComponent compResultF = new JLabel(Integer.toString(Operation));
compResultF.setBounds(250,280, 200,20);
wigView.add(compResult);
*/
JButton compButton = new JButton("Click after answering");
compButton.setBounds(80, 200, 300, 40);
compButton.setVisible(true);
compButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int Operation = Integer.parseInt(compUserInput.getText());
String Result;
if(x*y == Operation){
Result = "That's right. Great Job.";
}else {
Result = "Sorry, the correct answer is 18";
}
compResult.setVisible(true);
compResult.setText(Result);
}
});
wigView.addAndWait(compButton);
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
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;
/**
* A really simple class to display Swing widgets in a FlowLayout GUI.
* <p>
*
* @author parks
*/
public class WidgetView {
public static final int DEFAULT_X_SIZE = 600;
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 Flow layout
* JPanel as its content pane and initialize the frame to a default size.
*/
public WidgetView() {
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 WidgetView(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();
};*/
//java 7 solution
eventHandler = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() != userInputComponent) {
return;
}
lock.lock();
userClicked = true;
waitingForUser.signalAll();
lock.unlock();
}
};
jframe = new JFrame();
anchor = new JPanel();
jframe.setContentPane(anchor);
jframe.setSize(pixelSizeInX, pixelSizeInY);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
jframe.setLayout(null);
}
/**
* Add a Swing widget to the GUI.
*
* @param jcomp Swing widget (subclasses of JComponent--like JLabel and
* JTextField) to be added to the JFrame
*/
public void add(JComponent jcomp) {
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);
}
/**
* 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);
}
private void addWait(JComponent jcomp) {
add(jcomp);
lock.lock();
try {
while (!userClicked) {
waitingForUser.await();
}
}
catch (InterruptedException e1) {
System.err.println("WidgetView reports that something really bad just happened");
e1.printStackTrace();
System.exit(0);
}
userClicked = false;
waitingForUser.signalAll();
lock.unlock();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.