Results 1 to 8 of 8
  1. #1
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Helo with calculator GUI and code

    I have two .java files, both that work. One is the calculator code and one is the GUI code.

    Firstly, is it correct to keep these as two separate files or should I put them together?

    Secondly, how do I call on an action from the GUI using the calculator code file...

    e.g.
    from the GUI .java file
    Java Code:
    btnAdd = new JButton("Add");
            btnAdd.setBounds(50, 110, 100, 20);
            btnAdd.addActionListener(new Add());
            panel.add(btnAdd);
    Java Code:
        class Add implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    .....
            }
        }
    I want to use the add code from the main code .java file....

    Java Code:
    public class SimpleCalc {
    
        Double total = 0.0;
        Scanner scan = new Scanner(System.in);
    
        public void add(double numberToAdd) {
            total += numberToAdd;
        }
    I hope this makes sense... any help appreciated! :)

  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

    That's fine to have them in seperate files. You want to have an instance of the calculator in the GUI class which the action handling manipulates. I just recently finished a calculator program and I am working on making it better at the moment.
    Last edited by sunde887; 03-13-2011 at 05:47 AM.

  3. #3
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    That's fine to have them in seperate files. You want to have an instance of the calculator in the GUI class which the action handling manipulates. I just recently finished a calculator program and I am working on making it better at the moment.
    Can you give me an example? I'm really struggling with this.

    Would I use

    Java Code:
    public class GUI extends Calculator{
    and then use the action handling to call....?

  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

    You could do this since technically you want the GUI to be a calculator(is-a relationship) however; it's better to favor composition over inheritance(has-a relationship).
    Java Code:
    public class GUI{
      Calculator calc;
      //everything else. 
    }
    I dono how you like to work with with GUIs, I tend to favor having a method which creates all the GUI stuff, however; some people prefer to extend from JFrame, if you do this thatthe calculator will need to be a has-a not is-a.

  5. #5
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    You could do this since technically you want the GUI to be a calculator(is-a relationship) however; it's better to favor composition over inheritance(has-a relationship).
    Java Code:
    public class GUI{
      Calculator calc;
      //everything else. 
    }
    I dono how you like to work with with GUIs, I tend to favor having a method which creates all the GUI stuff, however; some people prefer to extend from JFrame, if you do this thatthe calculator will need to be a has-a not is-a.
    Okay, thanks. We've been taught the 'inheritance' way with the extends and return/super method, so I better stick with that to please my lecturer.

    I tried the return super.Add but am getting error

    Java Code:
    ButtonDemo.java:78: cannot return a value from method whose result type is void
                return super.Add;
    The 'add' method is public void Add().....

  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

    Since it's a public method you probably don't even need to call super, it should inherit the method, you will need to probably call super.getTotal or you may be able to directly manipulate total, not sure entirely how that works with inheritance and private instance variables. You can also make the instance variables protected of you want to do direct manipulation. Try those things out and see how it works.

  7. #7
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Since it's a public method you probably don't even need to call super, it should inherit the method, you will need to probably call super.getTotal or you may be able to directly manipulate total, not sure entirely how that works with inheritance and private instance variables. You can also make the instance variables protected of you want to do direct manipulation. Try those things out and see how it works.
    I tried to just call it in the jButton but am now getting this...

    Java Code:
    ButtonDemo.java:39: add(double) in Calculator cannot be applied to ()
            btnAdd.addActionListener(add());

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

    Default

    When you do an action listener the argument is a class object of any type that implements the appropriate listener, in this case action listener.

    There are a few things you will have to do, first create a class(or inner class) and make it implement actionlistener. It can be just like any other class(has instance variables if you want it to, as well as a constructor) and it must have an actionPerformed method which handles the action when the button is pushed.

    You will also need to capture the current value of the display and then do some work inside actionPerformed.

Similar Threads

  1. Helo with Graphics
    By g6pd in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 04:45 AM
  2. Help in a calculator
    By Ayannie in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 08:21 PM
  3. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 06:00 AM
  4. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 12:57 PM
  5. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 07:42 AM

Posting Permissions

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