Results 1 to 5 of 5
Thread: Multiplication practice app
- 06-23-2012, 04:41 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 16
- Rep Power
- 0
Multiplication practice app
Hi All,
Can anyone p lease help me? I am writing this program which will help elementary students learn multiplication. The code works fine, but I am having problems with the layout. The JPanel and the string painted by g.drawString is overlapping. How can I fix it ?
Here is the code I have attempted:
//this program generates single digit multiplication problems for elementary students
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Multiply2 extends JFrame {
//variables used in the program
JPanel panel;
JLabel status,prompt;
JTextField question,input;
String questionString;
int correctAnswer,userInput;
//setup graphical user interface components
public Multiply2 ()
{
super ("Multiplication Practice for Kids");
ActionEventHandler handler = new ActionEventHandler();
//creat text fields and a label
question = new JTextField(20);
question.setEditable(false);
status = new JLabel ("Are you ready to have fun with Multiplication",SwingConstants.CENTER);
prompt = new JLabel("Enter your answer: ");
input = new JTextField(4);
input.addActionListener(handler);
//add components to applet
Container c = getContentPane();
panel = new JPanel();
c.setLayout (new FlowLayout());
getContentPane().setBackground(Color.yellow);
panel.setBackground (Color.BLUE);
c.add(status);
c.add(panel);
panel.add(question);
panel.add(prompt);
panel.add(input);
//generate a question
createQuestion();
}
private class ActionEventHandler implements ActionListener
{
//verify the response entered by the user
public void actionPerformed (ActionEvent e)
{
//get user input and convert in into integer
userInput = Integer.parseInt(input.getText());
//clear the textfield
input.setText("");
//display the correct response
repaint();
}
}
//show whether the answer was correct or not
public void paint(Graphics g)
{
//to make sure that the children are painted
super.paint(g);
if (userInput != correctAnswer)
g.drawString("No. Please try again.",20,70);
else
{
g.drawString("Very Good!",20,70);
createQuestion();
}
}
//creat a new question and its corresponding answer
public void createQuestion()
{
//get two random numbers between 0 and 9
int digit1 = (int) (Math.random() * 10);
int digit2 = (int) (Math.random() * 10);
//correct answer after multiplying the two digits
correctAnswer = digit1 * digit2;
questionString = "How much is " + digit1 + " times " + digit2 + "?";
//add to the applet
question.setText(questionString);
}
//set the size of the window
public static void main (String args[])
{
Multiply2 window = new Multiply2();
window.addWindowListener
(
new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
);
window.setSize (500,200);
window.show();
}
}//end of Multiply2 class
-
Re: Multiplication practice app
Usually you're better off putting text into JLabels rather than painting them in paint(...) or paintComponent(...) as then you can use the layout managers to help you place your JLabels and avoid any overlap.
Also, consider editing your post above and putting your code in [code] [/code] tags so that it will retain its formatting and be readable.
- 06-24-2012, 08:08 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 16
- Rep Power
- 0
Re: Multiplication practice app
Hi,
Thank you so much for your help. I tried it and but now the string which I wanted to "print" according to the "response" is not showing on the screen at all.
What am I doing wrong?
Java Code://this program generates single digit multiplication problems for elementary students import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Multiply2 extends JFrame { //variables used in the program JPanel panel; JLabel status,prompt,response; JTextField question,input; String questionString; int correctAnswer,userInput; //setup graphical user interface components public Multiply2 () { super ("Multiplication Practice for Kids"); ActionEventHandler handler = new ActionEventHandler(); //creat text fields and a label question = new JTextField(20); question.setEditable(false); status = new JLabel ("Are you ready to have fun with Multiplication",SwingConstants.CENTER); prompt = new JLabel("Enter your answer: "); response = new JLabel (); input = new JTextField(4); input.addActionListener(handler); //add components to applet Container c = getContentPane(); panel = new JPanel(); c.setLayout (new FlowLayout()); getContentPane().setBackground(Color.yellow); panel.setBackground (Color.BLUE); c.add(status); c.add(panel); panel.add(question); panel.add(prompt); panel.add(input); panel.add(response); //generate a question createQuestion(); } private class ActionEventHandler implements ActionListener { //verify the response entered by the user public void actionPerformed (ActionEvent e) { //get user input and convert in into integer userInput = Integer.parseInt(input.getText()); //clear the textfield input.setText(""); //display the correct response //repaint(); verifyAnswer(); } } //show whether the answer was correct or not public void verifyAnswer() { //to make sure that the children are painted //super.paint(g); if (userInput != correctAnswer) //g.drawString("No. Please try again.",20,70); response = new JLabel ("No.Please try again.",SwingConstants.CENTER); else { //g.drawString("Very Good!",20,70); response = new JLabel ("Very Good",SwingConstants.CENTER); createQuestion(); } } //creat a new question and its corresponding answer public void createQuestion() { //get two random numbers between 0 and 9 int digit1 = (int) (Math.random() * 10); int digit2 = (int) (Math.random() * 10); //correct answer after multiplying the two digits correctAnswer = digit1 * digit2; questionString = "How much is " + digit1 + " times " + digit2 + "?"; //add to the applet question.setText(questionString); } //set the size of the window public static void main (String args[]) { Multiply2 window = new Multiply2(); window.addWindowListener ( new WindowAdapter() { public void windowClosing (WindowEvent e) { System.exit (0); } } ); window.setSize (500,200); window.show(); } }//end of Multiply2 class
- 06-24-2012, 10:54 PM #4
Member
- Join Date
- Jun 2012
- Posts
- 22
- Rep Power
- 0
Re: Multiplication practice app
Hi Miki,
The problems is in line 89 and 95
You already create the object in line 32Java Code://response = new JLabel ("No.Please try again.",SwingConstants.CENTER); //response = new JLabel ("Very Good",SwingConstants.CENTER);
So instead of creating again the object, you just need to set the result to the JLabel, like this:Java Code:response = new JLabel();
Java Code:public void verifyAnswer() { //to make sure that the children are painted //super.paint(g); if (userInput != correctAnswer) response.setText("No.Please try again."); else { response.setText("Very Good"); createQuestion(); } }
It should work properly nowLast edited by jfabian; 06-24-2012 at 11:17 PM.
- 06-24-2012, 11:38 PM #5
Member
- Join Date
- Jun 2012
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
int array multiplication
By sternhagenr in forum New To JavaReplies: 10Last Post: 08-20-2011, 01:41 AM -
need help with multiplication
By dakid2 in forum New To JavaReplies: 10Last Post: 03-08-2011, 03:41 AM -
Multiplication Table
By BillyB in forum New To JavaReplies: 17Last Post: 12-24-2010, 06:58 AM -
Multiplication Table
By SwEeTAcTioN in forum New To JavaReplies: 4Last Post: 02-24-2010, 04:11 AM -
Help with Multiplication
By phil028 in forum New To JavaReplies: 1Last Post: 12-06-2007, 07:39 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks