Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Syanara is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default 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

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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);
    Your action listener method looks good, except you may want to change a label go contain the answer.
    Last edited by sunde887; 05-11-2011 at 10:14 PM.

  3. #3
    Bloitz is offline Member
    Join Date
    May 2011
    Location
    Belgium
    Posts
    13
    Rep Power
    0

    Default

    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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    @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.

  5. #5
    Syanara is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default 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);
      }
    });
    	
    	}
    now I have 6 errors


    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
    I am a major noob here too lol

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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);
      }
    }
    Two:
    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);
      }
    }
    Try working on simply laying the GUI out correctly, then add the action listener.

  7. #7
    Syanara is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    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);
    
    
    	}
    	
    	}
    the error I keep getting is:
    no main methods, applets, or MIDIlets found in file

    so now first off, what does that mean. Secondly, what action should I take?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    The most important information is the first bit "no main method". Surely you know how to fix that.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    }
    The main is the first thing the jvm enters and starts executing. You create objects and call methods in main.

    Also, in your code, remove the frame instance variable and anytime you see
    Java Code:
    frame.xxx
    where xxx is some method, remove the frame. , it's redundant. Your class is a JFrame so t shouldn't have it's own.

    Since everything in your class is done in the constructor your main method will only create an object,
    Java Code:
    Adding a = new Adding();
    This does 3 things:
    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.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    2) creates an object on the heap with the new keyword
    There, much better ;)

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by Junky View Post
    There, much better ;)

    Lol, thanks, damn cellphone typos.

  12. #12
    Syanara is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    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);
    
    	}
    	
    	}
    Heres my error



    Adding.java:9: class, interface, or enum expected
    public static void main (String[] args){
    ^
    1 error

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
      }
    }

  14. #14
    Syanara is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    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();
    
     }
    
     }

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Now you have to go back to your original code and see what you did. Hint: ActionListener

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  17. #17
    Syanara is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    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

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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"

  19. #19
    Syanara is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    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();
    
     }
    
     }
    my errors are:

    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

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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)

Page 1 of 2 12 LastLast

Similar Threads

  1. Simple Action/Listener Help GUI
    By aanders5 in forum New To Java
    Replies: 24
    Last Post: 10-18-2010, 06:43 PM
  2. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 05:39 PM
  3. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 08:51 AM
  4. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 10:14 AM
  5. Addition java program
    By tabrez_k81 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 10:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •