Results 1 to 20 of 22
- 05-11-2011, 09:37 PM #1
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Java Addition Program Help with Action Listener
okay so I am really new to java, EXTREMELY new and I need some help with a school assignment of mine! My assignment is to make a GUI where I can have two text fields so I can add two numbers together using an action listener and so far I am stuck in the way that I don't know where to go? Here is the code I have now that I was able to get from following a bunch of youtube videos and online tutorials (I didn't learn much in class that I could understand). so yea heres my code please help:
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class adding extends JFrame{
JTextField num1;
JTextField num2;
JPanel panel;
JButton button;
JFrame frame;
private JLabel title1;
public adding(){
super ("Addition");
setLayout(new FlowLayout());
title1 = new JLabel("Please Add two numbers together");
add(title1);
frame = this;
panel = new JPanel();
num1 = new JTextField("enter first number here");
num1.setEditable(true);
num1.addActionListener(this);
num2 = new JTextField("enter second number here");
num2.setEditable(true);
num2.addActionListner(this);
button = new JButton("Add");
frame.add(panel);
panel.add(num1);
panel.add(button);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
button.addActionLisnter(new ActionListner(){
public void actionPerformed(ActionEvent e){
System.out.println("Addition of two numbers!");
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
System.out.println("Sum: " + sum);
}
});
}
public static void main(String[] arguments){
new adding();
}
}
Any help as to what i need to do next would be very helpful! thanks
- 05-11-2011, 10:08 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
What's wrong? Errors? If so, post the exact error message. If you post code please wrap it in code tags
[code]
YOUR CODE HERE
[/code]
Also, follow proper naming conventions, class names should have the first letter of each word capitalized. There are some other problems as well, you extended JFrame, so yor class is-a JFrame, it shouldn't have an instance variable of type JFrame. You can set the title with either setTitle, or passing a string into the super constructor(super("this is the title"); )
Then you can add stuff to the frame with calls to add, you won't have to qualify the method with an object like you are doing.
Java Code:add(panel); Instead of frame.add(panel);
Last edited by sunde887; 05-11-2011 at 10:14 PM.
- 05-11-2011, 10:18 PM #3
Member
- Join Date
- May 2011
- Location
- Belgium
- Posts
- 13
- Rep Power
- 0
1. Use the [CODE] tags.
2. By convention classes start with a capital letter.
3. ActionListEner ;) . And there's no need to add an ActionListener to your JTextFields.
4. Recheck if you added all your components
5. Check the JTextField APIl. Args[0] and args[1] aren't variables in your program so instead you should try to get the Text from your textfields.
PS: JTextFields are editable by default so no need for num1/num2.setEditable(true);
PPS: Don't expect homework answers on this forum ;)
PPPS: Looks like Sunde887 beat me to it.Last edited by Bloitz; 05-11-2011 at 10:26 PM.
- 05-11-2011, 10:28 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
@bloitz: I beat you to some things, other things I left out since I only really scanned the un formatted code. I believe the op should definitely be able to figure it out now.
- 05-11-2011, 10:37 PM #5
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
okay so
now so far I have changed alot of my code to what you guys recommended but I am still kinda stuck on what Exactly it is that I need to do with the title so here is my code now
Java Code:import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Adding { JTextField num1; JTextField num2; JPanel panel; JButton button; JFrame frame; public Adding(){ super ("Addition"); setLayout(new FlowLayout()); frame = this; panel = new JPanel(); num1 = new JTextField("enter first number here"); num2 = new JTextField("enter second number here"); button = new JButton("Add"); add(panel); panel.add(num1); panel.add(button); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); button.addActionLisnter(new ActionListner(){ public void ActionPerformed(ActionEvent e){ System.out.println("Addition of two numbers!"); int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int sum = a + b; System.out.println("Sum: " + sum); } }); }
Java Code:Adding.java:20: Object() in java.lang.Object cannot be applied to (java.lang.String) super ("Addition"); ^ Adding.java:21: cannot find symbol symbol : method setLayout(java.awt.FlowLayout) location: class Adding setLayout(new FlowLayout()); ^ Adding.java:24: cannot find symbol symbol : method add(javax.swing.JLabel) location: class Adding add(title1); ^ Adding.java:26: incompatible types found : Adding required: javax.swing.JFrame frame = this; ^ Adding.java:32: cannot find symbol symbol : method add(javax.swing.JPanel) location: class Adding add(panel); ^ Adding.java:38: cannot find symbol symbol : class ActionListner location: class Adding button.addActionLisnter(new ActionListner(){ ^ 6 errors
- 05-11-2011, 10:58 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
You no longer extend JFrame. There are two ways you can do GUIs(probably a simplification, there are most likely more)
One:
Java Code:public class X extends JFrame{ private JLabel label; public X(){ super("this extends JFrame and uses the super class constructor"); label = new JLabel("hi there"); add(label); setSize(400, 400); setVisible(true); } }
Java Code:public class X{ private JFrame frame; private JLabel label; public X(){ frame = new JFrame("this one uses and manages an instance. Notice the differences"); label = new JLabel("hi there"); frame.add(label); frame.setSize(400, 400); frame.setVisible(true); } }
- 05-11-2011, 11:53 PM #7
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Thank you soo much for your help! so I am just trying to figure out the GUI right now, I definitely think I was trying to do too much all at once right away here. I have compiled my JAVA program with no errors however I am getting an error when I try to run it.
Java Code:import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Adding extends JFrame{ JTextField num1; JTextField num2; JPanel panel; JButton button; JFrame frame; private JLabel label; public Adding(){ super("Add two numbers together"); label = new JLabel("Adding"); add(label); setSize(400, 400); setVisible(true); setLayout(new FlowLayout()); frame = this; panel = new JPanel(); num1 = new JTextField("enter first number here"); num2 = new JTextField("enter second number here"); button = new JButton("Add"); add(panel); panel.add(num1); panel.add(button); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
no main methods, applets, or MIDIlets found in file
so now first off, what does that mean. Secondly, what action should I take?
- 05-11-2011, 11:57 PM #8
The most important information is the first bit "no main method". Surely you know how to fix that.
- 05-12-2011, 12:03 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
It doesn't mean much. All classes that you want to do something need a main method.
Java Code:pulic static void main(String[] args){ //do stuff here }
Also, in your code, remove the frame instance variable and anytime you see
Java Code:frame.xxx
Since everything in your class is done in the constructor your main method will only create an object,
Java Code:Adding a = new Adding();
1) creates a reference if type Addition, it points to something
2) creates an object on the heap with the new keyboard
3) links the two so the reference is pointing at the newly created object on the heap.
- 05-12-2011, 12:08 AM #10
- 05-12-2011, 12:11 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
- 05-12-2011, 04:02 AM #12
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
okay guys I just started java yesterday so again still pretty far behind here and I am a major newbie to this but anyways I put the method in but I am not exactly sure where I need to place it cause anywhere I put it i keep getting an error
heres my code now:
Java Code:import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.*; import java.awt.event.*; import java.awt.*; public static void main (String[] args){ public class Adding extends JFrame{ JTextField num1; JTextField num2; JPanel panel; JButton button; JFrame frame; private JLabel label; public Adding(){ super("Add two numbers together"); label = new JLabel("Adding"); add(label); setSize(400, 400); setVisible(true); setLayout(new FlowLayout()); frame = this; panel = new JPanel(); num1 = new JTextField("enter first number here"); num2 = new JTextField("enter second number here"); button = new JButton("Add"); add(panel); panel.add(num1); panel.add(button); frame.pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } }
Adding.java:9: class, interface, or enum expected
public static void main (String[] args){
^
1 error
- 05-12-2011, 04:08 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
You still have a frame variable and some references and initialization of the frame object. Main should be a method inside the class.
Java Code:public class X{ //do stuff public static void main(String[] args){ //create objects and do stuff } }
- 05-12-2011, 05:30 AM #14
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Alright guys
I took a good hard look at my code and changed things around a lot and so now How should I go about getting the text fields to actually add my numbers together?
here's my code now:
Java Code:import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class adding extends JFrame { JButton addTogether = new JButton("add"); JTextField num1 = new JTextField(4); JTextField num2 = new JTextField(4); public adding() { super("adding"); setSize(308, 128); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FlowLayout flo = new FlowLayout(); setLayout(flo); add(addTogether); add(num1); add(num2); setVisible(true); } public static void main(String[] arguments) { adding ad = new adding(); } }
- 05-12-2011, 05:42 AM #15
Now you have to go back to your original code and see what you did. Hint: ActionListener
- 05-12-2011, 06:11 AM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Now that you got the gui working it's time to work on the listener code. First look up what kind of listeners buttons should use.
Then look if there is any methods in the text field class to get the text. Also, see if the label class has any methods to set text. You basically want to extract the text field info, add it, then set the label.
- 05-12-2011, 06:45 AM #17
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
alright so I was taking a few hints from my previous code and doing some more research and I have come up with this code:
Java Code:import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class adding extends JFrame { JButton addTogether = new JButton("add"); JTextField num1 = new JTextField(4); JTextField num2 = new JTextField(4); public adding() { super("adding"); setSize(308, 128); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FlowLayout flo = new FlowLayout(); setLayout(flo); add(addTogether); add(num1); add(num2); setVisible(true); addTogether.addActionLisnter(new ActionListner(){ public void ActionPerformed(ActionEvent e){ System.out.println("Addition of two numbers!"); num1 = Integer.parseInt(args[0]); num2 = Integer.parseInt(args[1]); int sum = a + b; System.out.println("Sum: " + sum); } }); } public static void main(String[] arguments) { adding ad = new adding(); } }
now all I wanna know is a. is this the right way to do this? and b. how do I solve this error for a compile:
adding.java:28: cannot find symbol
symbol : class ActionListner
location: class adding
addTogether.addActionLisnter(new ActionListner(){
^
1 error
- 05-12-2011, 06:52 AM #18
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
You need to spell very correctly. Listner, and lisnter are not correct spelling. It needs to be Listener.
Also, args isn't a variable anywhere. Instead you want to extract values from the text field. Check out the api for text fields by googling "java 6 JTextField"
- 05-12-2011, 07:54 AM #19
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
okay, I am so close, I am down to 2 more errors that I can figure out
Java Code:import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class adding extends JFrame { JButton addTogether = new JButton("add"); JTextField num1 = new JTextField(4); JTextField num2 = new JTextField(4); public adding() { super("adding"); setSize(308, 128); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FlowLayout flo = new FlowLayout(); setLayout(flo); add(addTogether); add(num1); add(num2); setVisible(true); addTogether.addActionListener(new ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent e){ System.out.println("Addition of two numbers!"); int a = Integer.parseInt(num1); int b = Integer.parseInt(num2); int sum = a + b; System.out.println("Sum: " + sum); } }); } public static void main(String[] arguments) { adding ad = new adding(); } }
adding.java:33: cannot find symbol
symbol : method parseInt(javax.swing.JTextField)
location: class java.lang.Integer
int a = Integer.parseInt(num1);
^
adding.java:34: cannot find symbol
symbol : method parseInt(javax.swing.JTextField)
location: class java.lang.Integer
int b = Integer.parseInt(num2);
^
2 errors
- 05-12-2011, 08:06 AM #20
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Integer.parseInt expects a String argument, not a text field.
Search the text field api for a method that gets the text in the text field and use it.
JTextField (Java Platform SE 6)
Similar Threads
-
Simple Action/Listener Help GUI
By aanders5 in forum New To JavaReplies: 24Last Post: 10-18-2010, 06:43 PM -
Action Listener
By greatmajestics in forum AWT / SwingReplies: 8Last Post: 03-25-2010, 05:39 PM -
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 08:51 AM -
action listener on jcombobox
By chkm8 in forum New To JavaReplies: 2Last Post: 02-05-2009, 10:14 AM -
Addition java program
By tabrez_k81 in forum New To JavaReplies: 5Last Post: 12-15-2008, 10:08 AM
Bookmarks