can anyone help me put this to JFrame.

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 17920 Views - Last Post: 06 July 2011 - 05:09 AM Rate Topic: ***-- 2 Votes

#1 masterjohji   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-June 11

can anyone help me put this to JFrame.

Posted 04 July 2011 - 12:22 AM

Hello guys , our professor ask us to create a multiplication program where the multiplicand and multiplier should be random numbers., then it will ask the user to input the correct answer. If the inputted answer is correct it will prompt a random result like "you are correct" , "good job" and etc and If the answer is incorrect it would display another set of question.

this is the code that I've done.
import javax.swing.JOptionPane;

public class random2
{
public static void main(String[] johji)
{	
	for(int x=0;x<=0;x--)
	{
	int n = (int)(1+Math.random()*9);
	int n2 = (int)(1+Math.random()*9);
	JOptionPane.showMessageDialog(null,n +" times "+ n2);
	int ans = n * n2;
	int input=Integer.parseInt(JOptionPane.showInputDialog("Enter your answer"));
	if(input==ans)
			{
			int score = (int)(1+Math.random()*3);
			if(score==1)
			JOptionPane.showMessageDialog(null,"your answer is correct");
			if(score==2)
			JOptionPane.showMessageDialog(null,"very good");
			if(score==3)
			JOptionPane.showMessageDialog(null,"good job");
			}
			else
			JOptionPane.showMessageDialog(null," wrong ");
			
	}
}
}

now my only problem is to put this to JFrame ;((

this my unfinished code ... I really don't know how to ask the user to input the answer and what are the codes should I use.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Container;



public class Assign extends JApplet implements ActionListener {

   JLabel titleLabel = new JLabel("Multiplication Program");
   JLabel subtitle = new JLabel("Multiplication Program");
   JTextField question = new JTextField(50);
   JTextField answer = new JTextField(10);
   JButton ok = new JButton("OK");
   JButton clear = new JButton("Clear");
   final int size = 3;
   int b[]=new int[size];
   
    public void init() {
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        c.add(titleLabel);
        c.add(subtitle);
        c.add(question);
		answer.addActionListener(this);
        c.add(ok);
        c.add(clear);
        question.setEditable(false);
    }
    public void ok()
    {
        for(int x=0;x<=0;x--)
        {
            int n = (int)(1+Math.random()*9);
			int n2 = (int)(1+Math.random()*9);
			question.setText(n1+"times"+n2);
			int ans = n * n2;
				
			
        }
                   
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: can anyone help me put this to JFrame.

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: can anyone help me put this to JFrame.

Posted 04 July 2011 - 02:23 AM

You specify that a JFrame must be used, but a common introductory approach to Java GUI programming to complete the simple task you've described is to use dialogs as shown here. You could present a dialog that showed the two random numbers and asked the user to provide their product. After entering a product and pressing the OK button, the required result would be shown in another dialog.

Would that be sufficient, or do you need to be more elaborate, actually using a JFrame?
Was This Post Helpful? 0
  • +
  • -

#3 masterjohji   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-June 11

Re: can anyone help me put this to JFrame.

Posted 04 July 2011 - 03:29 AM

View PostGregBrannon, on 04 July 2011 - 02:23 AM, said:

You specify that a JFrame must be used, but a common introductory approach to Java GUI programming to complete the simple task you've described is to use dialogs as shown here. You could present a dialog that showed the two random numbers and asked the user to provide their product. After entering a product and pressing the OK button, the required result would be shown in another dialog.

Would that be sufficient, or do you need to be more elaborate, actually using a JFrame?


thanks for the reply , actually if you look at the first code that i've posted it is similar to what you have said but we are required to put everything into JFrame.

This post has been edited by masterjohji: 04 July 2011 - 03:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: can anyone help me put this to JFrame.

Posted 04 July 2011 - 03:49 AM

So what's wrong with what you've written so far? It looks like you need to keep working on it.

I'm not sure why you have an ActionListener in the answer JTextField. I would use a button to let the user specify when an answer has been entered.
Was This Post Helpful? 0
  • +
  • -

#5 masterjohji   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-June 11

Re: can anyone help me put this to JFrame.

Posted 06 July 2011 - 01:21 AM

I've finished the code but when I run it , it hangs up. :(


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class assignment extends JFrame implements ActionListener
{
    JLabel label1 = new JLabel("Multiplication Program");
    JLabel question = new JLabel("");
    JTextField answer = new JTextField(20);
    JButton ok = new JButton("OK");
    JLabel win = new JLabel("");
    JLabel score = new JLabel("");
    final int Width = 400;
    final int Height = 600;
    
    public assignment()
    {
        super("Hello");
        setSize(Width, Height);
        setLayout(new FlowLayout());
        add(label1);
        add(question);
        add(answer);
        add(score);
        add(ok);
        add(win);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ok.addActionListener(this); 
        
    }
    public void actionPerformed(ActionEvent e)
    {
        for(int x=0;x<=0;x--)
        {
            int n = (int)(1+Math.random()*9);
            int n2 = (int)(1+Math.random()*9);
            question.setText(n+" times "+n2);
            int ans = n * n2;
            int input=Integer.parseInt(answer.getText());
            if(input==ans)
            {
                int scor = (int)(1+Math.random()*3);
                if(scor==1)
                    score.setText("Great!");
                if(scor==2)
                    score.setText("Good Job!");
                if(scor==3)
                    score.setText("Nxt Question!");
            }
    }
    }
}


public class demo
{
       public static void main(String[] args)
       {
           assignment frame = new assignment();
           frame.setVisible(true);
       }
}

This post has been edited by masterjohji: 06 July 2011 - 01:23 AM

Was This Post Helpful? 0
  • +
  • -

#6 n8schatten   User is offline

  • D.I.C Regular
  • member icon

Reputation: 147
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: can anyone help me put this to JFrame.

Posted 06 July 2011 - 01:55 AM

for(int x=0;x<=0;x--)

-> infinite loop!
Was This Post Helpful? 0
  • +
  • -

#7 masterjohji   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-June 11

Re: can anyone help me put this to JFrame.

Posted 06 July 2011 - 02:00 AM

View Postn8schatten, on 06 July 2011 - 01:55 AM, said:

for(int x=0;x<=0;x--)

-> infinite loop!


sorry, the loop is on purpose because that's the only way I know so the program will continue to ask the user another question.
Was This Post Helpful? 0
  • +
  • -

#8 masterjohji   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-June 11

Re: can anyone help me put this to JFrame.

Posted 06 July 2011 - 02:02 AM

View Postn8schatten, on 06 July 2011 - 01:55 AM, said:

for(int x=0;x<=0;x--)

-> infinite loop!


sorry, the loop is on purpose because that's the only way I know so the program will continue to ask the user another question.
Was This Post Helpful? 0
  • +
  • -

#9 n8schatten   User is offline

  • D.I.C Regular
  • member icon

Reputation: 147
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: can anyone help me put this to JFrame.

Posted 06 July 2011 - 02:11 AM

If you click the ok-button once, the actionPerformed is executed and your program will enter the infinite loop. In that loop you update your components. Since the loop is executed on the GUI-Thread (EDT), which is basically a queue, the GUI will never have the opportunity to perform the changes you desire.
There should be no problem with getting rid of that loop, since the GUI itself offers some kind of loop that waits for user inputs, e.g. a button click.
Was This Post Helpful? 1
  • +
  • -

#10 masterjohji   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-June 11

Re: can anyone help me put this to JFrame.

Posted 06 July 2011 - 02:30 AM

View Postn8schatten, on 06 July 2011 - 02:11 AM, said:

If you click the ok-button once, the actionPerformed is executed and your program will enter the infinite loop. In that loop you update your components. Since the loop is executed on the GUI-Thread (EDT), which is basically a queue, the GUI will never have the opportunity to perform the changes you desire.
There should be no problem with getting rid of that loop, since the GUI itself offers some kind of loop that waits for user inputs, e.g. a button click.


I've added another button called process , but it still hangs up . I'm getting frustated :hang:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class assignment extends JFrame implements ActionListener
{
    JLabel label1 = new JLabel("");
    JLabel question = new JLabel("");
    JTextField answer = new JTextField(10);
    JButton ok = new JButton("start");
    JButton process = new JButton("ok");
    JLabel win = new JLabel("");
    JLabel score = new JLabel("");
    final int Width = 400;
    final int Height = 600;
    
    public assignment()
    {
        super("Hello");
        setSize(Width, Height);
        setLayout(new FlowLayout());
        add(label1);
        add(question);
        add(answer);
        add(score);
        add(ok);
        add(process);
        add(win);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ok.addActionListener(this);
            process.addActionListener(this);
            answer.addActionListener(this);
        
    }
    public void actionPerformed(ActionEvent e)
    {
        for(int x=0;x<=0;x--)
        {
            int n = (int)(1+Math.random()*9);
            int n2 = (int)(1+Math.random()*9);
            question.setText(n+" times "+n2);
            int ans = n * n2;
            int input=Integer.parseInt(answer.getText());
            if(e.getSource()==process)
            {
            if(input==ans)
            {
                int scor =(int)(1+Math.random()*3);
                if(scor==1)
                    score.setText("Great!");
                if(scor==2)
                    score.setText("Good Job!");
                if(scor==3)
                    score.setText("Nxt Question!");
            }
            else
                score.setText("Wrong ! next question ...");
            }
    }
    }
}

Was This Post Helpful? 0
  • +
  • -

#11 n8schatten   User is offline

  • D.I.C Regular
  • member icon

Reputation: 147
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: can anyone help me put this to JFrame.

Posted 06 July 2011 - 02:35 AM

Why don't you just try to get rid of the loop? As I mentioned before, if you create a GUI, Java basically does the following:
while (programIsRunning()) {
displayAndUpdateGUI();
checkForUserInput();
}

Which is the "infinite" loop you need to continuously check for user input. When creating GUIs in Java, there is almost NEVER the need to manually add an infinite loop!
Was This Post Helpful? 0
  • +
  • -

#12 masterjohji   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-June 11

Re: can anyone help me put this to JFrame.

Posted 06 July 2011 - 02:44 AM

View Postn8schatten, on 06 July 2011 - 02:35 AM, said:

Why don't you just try to get rid of the loop? As I mentioned before, if you create a GUI, Java basically does the following:
while (programIsRunning()) {
displayAndUpdateGUI();
checkForUserInput();
}

Which is the "infinite" loop you need to continuously check for user input. When creating GUIs in Java, there is almost NEVER the need to manually add an infinite loop!


ok I've remove the for loop , it runs but it tells me an error and it when I hit the ok button which is process it will automatically display the else statement :(

and this is the error that keeps appearing on the run console

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at assignment.actionPerformed(assignment.java:43)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class assignment extends JFrame implements ActionListener
{
    JLabel label1 = new JLabel("");
    JLabel question = new JLabel("");
    JTextField answer = new JTextField(10);
    JButton ok = new JButton("start");
    JButton process = new JButton("ok");
    JLabel win = new JLabel("");
    JLabel score = new JLabel("");
    final int Width = 400;
    final int Height = 600;
    
    public assignment()
    {
        super("Hello");
        setSize(Width, Height);
        setLayout(new FlowLayout());
        add(label1);
        add(question);
        add(answer);
        add(score);
        add(ok);
        add(process);
        add(win);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ok.addActionListener(this);
            process.addActionListener(this);
        
    }
    public void actionPerformed(ActionEvent e)
    {
        
        
            int n = (int)(1+Math.random()*9);
            int n2 = (int)(1+Math.random()*9);
            question.setText(n+" times "+n2);
            int ans = n * n2;
            int input=Integer.parseInt(answer.getText());
            if(e.getSource()==process)
            {
            if(input==ans)
            {
                int scor =(int)(1+Math.random()*3);
                if(scor==1)
                    score.setText("Great!");
                if(scor==2)
                    score.setText("Good Job!");
                if(scor==3)
                    score.setText("Nxt Question!");
            }
            else
                score.setText("Wrong ! next question ...");
            
            }
    }
}

Was This Post Helpful? 0
  • +
  • -

#13 n8schatten   User is offline

  • D.I.C Regular
  • member icon

Reputation: 147
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: can anyone help me put this to JFrame.

Posted 06 July 2011 - 02:49 AM

Quote

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

It translates to: An empty String, which is what answer contained, is not a valid number.

Enter a valid number to the textfield and this error will disappear (or, better: You catch the Exception and display a proper error message).
Was This Post Helpful? 0
  • +
  • -

#14 masterjohji   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-June 11

Re: can anyone help me put this to JFrame.

Posted 06 July 2011 - 02:54 AM

View Postn8schatten, on 06 July 2011 - 02:49 AM, said:

Quote

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

It translates to: An empty String, which is what answer contained, is not a valid number.

Enter a valid number to the textfield and this error will disappear (or, better: You catch the Exception and display a proper error message).


ok... I didn't get what you've said , what do you mean by enter a valid number to the textfield?

but I have this line
int input=Integer.parseInt(answer.getText());


the answer will be an integer,

This post has been edited by masterjohji: 06 July 2011 - 02:58 AM

Was This Post Helpful? 0
  • +
  • -

#15 n8schatten   User is offline

  • D.I.C Regular
  • member icon

Reputation: 147
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: can anyone help me put this to JFrame.

Posted 06 July 2011 - 02:57 AM

According to the error message you provided, you clicked on the ok button while the JTextField answer did not contain any text. If you try to parse an empty String using Integer.parseInt(String someString) it will result in a NumberFormatExpcetion, which is what happened to you. Try entering a number, e.g. 5 into the textfield and then push the button.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2