Results 1 to 6 of 6
Thread: Educational Project
- 09-22-2011, 10:52 AM #1
Member
- Join Date
- Sep 2011
- Location
- Bristol, England
- Posts
- 5
- Rep Power
- 0
Educational Project
I'm creating this educational program for my college project. I'm quite near the start of coding, and I'm already stuck on why it isn't compiling and what I should be doing next to get the input from the user in Swing and if it detects if it is right or not.
Any help would be greatly appreciated.Java Code:import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class NewClass { /** * This is the code to go with my Extended Project. Creating a educational program for primary school children, consisting of * math's questions. * Version 1.0 */ public static void main(String[]args) { Random rand = new Random(); int num1 = rand.nextInt(25); int num2 = rand.nextInt(25); int num3 = num1 + num2; int guess; JFrame frame = new JFrame("Input Dialog Box Frame"); JButton button = new JButton("Show Input Dialog Box"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae, int num1, int num2, int num3){ String question = JOptionPane.showInputDialog("What is " + num1 + " + " + num2 +"?"); } } } }
- 09-22-2011, 10:57 AM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: Educational Project
If your code doesn't compile, it helps if you post any error messages you get. But I'll take a shot...
Where are you closing the set of parentheses that opens after addActionListener?Java Code:button.addActionListener(new ActionListener(){
- 09-22-2011, 11:01 AM #3
Member
- Join Date
- Sep 2011
- Location
- Bristol, England
- Posts
- 5
- Rep Power
- 0
Re: Educational Project
Sorry, here's the error log!
Java Code:Exception in thread "main" java.lang.Error: Unresolved compilation problems: The type new ActionListener(){} must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent) ActionEvent cannot be resolved to a type Syntax error, insert ")" to complete Expression Syntax error, insert ";" to complete Statement at NewClass.main(NewClass.java:23)
- 09-22-2011, 11:14 AM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: Educational Project
Your implementation of actionPerformed() takes the wrong arguments. As the compiler says, its only argument should be an ActionEvent, but you're trying to pass 3 ints along with it.The type new ActionListener(){} must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
Try declaring num1, num2 and num3 as class variables, instead of declaring them in the main method. That way, the actionPerformed() method will be able to see them without having them passed to it.
ActionEvent cannot be resolved to a typeJava Code:import java.awt.event.ActionEvent;
See my post above - you've opened a pair of parentheses for a method call without closing them or terminating the statement.Syntax error, insert ")" to complete Expression
Syntax error, insert ";" to complete Statement
- 09-22-2011, 11:46 AM #5
Member
- Join Date
- Sep 2011
- Location
- Bristol, England
- Posts
- 5
- Rep Power
- 0
Re: Educational Project
Here's my new code. I was wondering how I could use While to create a loop which keeps making it go back to the question until it is answered correctly. I tried to make one, but I was stuck in an infinite loop :(
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class NewClass {
/**
* This is the code to go with my Extended Project. Creating a educational program for primary school children, consisting of
* math's questions.
* Version 1.0
*/
public static void main(String[]args) {
JFrame frame = new JFrame("Maths Questions");
JButton button = new JButton("Start the questions");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
Random rand2 = new Random();
int num1 = rand2.nextInt(25);
int num2 = rand2.nextInt(25);
int num3 = num1+num2;
String answer = JOptionPane.showInputDialog("What is " + num1 + " + " + num2 + "?");
if (answer != null)
JOptionPane.showMessageDialog(null, "Sorry, that is wrong, try again:");
}
});
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel);
frame.setSize(300, 73);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
}
}
- 09-22-2011, 11:50 AM #6
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Similar Threads
-
Educational practice in NYC
By barss in forum Jobs DiscussionReplies: 1Last Post: 08-19-2011, 01:22 PM -
How to convert from Ant project to maven project?
By jiapei100 in forum New To JavaReplies: 0Last Post: 06-08-2011, 10:01 AM -
[CodeFest] Virtual Combat : An extension of educational game
By codefest11 in forum Reviews / AdvertisingReplies: 0Last Post: 02-10-2011, 07:52 PM -
What Java educational book to buy?
By goffy in forum New To JavaReplies: 3Last Post: 06-20-2010, 02:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks