FlashCard (JAVA , using WidgetView , im simplest beginner form) This program imp
ID: 3887669 • Letter: F
Question
FlashCard (JAVA , using WidgetView , im simplest beginner form)
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
Here is the program for flash card to teach Multiplication ,here we take two random number and checking whether the multiplication Guess is Right or Wrong
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JLabel;
public class JAVASwingFormExample {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JAVASwingFormExample window = new JAVASwingFormExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public JAVASwingFormExample() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
int x,y,z;
Random random_number = new Random();
x = random_number.nextInt(9);
y = random_number.nextInt(9);
z = x * y;
frame = new JFrame();
frame.setBounds(100, 100, 730, 489);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lbl1 = new JLabel("what is "+x+" times "+y+);
lbl1.setBounds(65, 31, 46, 14);
frame.getContentPane().add(lbl1);
textField_1 = new JTextField();
textField_1.setBounds(128, 65, 86, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JButton clickMe = new JButton("Click After Answering");
ClickMe.setBounds(312, 387, 89, 23);
frame.getContentPane().add(clickMe);
clickMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(textField1.getText().isEmpty() || ( Integer.parseInt(textField_1.getText())!=z)
{
JLabel lbl2 = new JLabel("Sorry the correct answer is "+z);
lbl2.setBounds(65, 31, 46, 14);
frame.getContentPane().add(lbl2);
}
else{
System.out.println("That's Right, Good Job");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.