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

    Default How to use method in ActionListener

    I have a calculator.java file and calculatorGUI.java file.

    In the calculator file, I've got

    Java Code:
    import java.util.Scanner;
    import java.lang.Math;
    
    public class Calculator {
    
        Double total = 0.0;
        Scanner scan = new Scanner(System.in);
    
        public void add(double numberToAdd) {
            total += numberToAdd;
        }
    
        public void minus(double numberToMinus) {
            total -= numberToMinus;
        }
    
    	public void divide(double numberToDivide) {
    		total /= numberToDivide;
    	}
    etc....

    and in the calculator GUI I've got...

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class ButtonDemo extends Calculator{
    
        private JButton btnAdd,  btnMinus,  btnMultiply,  btnDivide,  btnPercent,  btnSqrRoot,  btnTotal,  btnExit;
        private JLabel lblTotal,  lblNumber;
        private JTextField txtTotal,  txtNumber;
        private JPanel panel;
        private JFrame frame;
    	
        public ButtonDemo() {
            frame = new JFrame();
            frame.setTitle("Calculator");
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    
            panel = new JPanel();
            panel.setLayout(null);
    
            lblTotal = new JLabel("Total");
            lblTotal.setBounds(10, 50, 100, 20);
            panel.add(lblTotal);
    
            txtTotal = new JTextField();
            txtTotal.setBounds(110, 50, 150, 20);
    		txtTotal.addActionListener(new TotalHandler());
            panel.add(txtTotal);
    
            lblNumber = new JLabel("Number");
            lblNumber.setBounds(10, 70, 100, 20);
            panel.add(lblNumber);
    
            txtNumber = new JTextField("");
            txtNumber.setBounds(110, 70, 150, 20);
            panel.add(txtNumber);
    
            btnAdd = new JButton("Add");
            btnAdd.setBounds(50, 110, 100, 20);
            btnAdd.addActionListener(new AddHandler());
            panel.add(btnAdd);
    
            btnMinus = new JButton("Minus");
            btnMinus.setBounds(150, 110, 100, 20);
            btnMinus.addActionListener(new MinusHandler());
            panel.add(btnMinus);
    
            btnMultiply = new JButton("Multiply");
            btnMultiply.setBounds(50, 130, 100, 20);
            btnMultiply.addActionListener(new MultiplyHandler());
            panel.add(btnMultiply);
    
            btnDivide = new JButton("Divide");
            btnDivide.setBounds(150, 130, 100, 20);
            btnDivide.addActionListener(new DivideHandler());
            panel.add(btnDivide);
    
            btnPercent = new JButton("Percent");
            btnPercent.setBounds(50, 150, 100, 20);
            btnPercent.addActionListener(new PercentHandler());
            panel.add(btnPercent);
    
            btnSqrRoot = new JButton("SqrRoot");
            btnSqrRoot.setBounds(150, 150, 100, 20);
            btnSqrRoot.addActionListener(new SqrRootHandler());
            panel.add(btnSqrRoot);
    
            btnTotal = new JButton("Total");
            btnTotal.setBounds(50, 170, 100, 20);
            btnTotal.addActionListener(new TotalHandler());
            panel.add(btnTotal);
    
            btnExit = new JButton("Exit");
            btnExit.setBounds(150, 170, 100, 20);
            btnExit.addActionListener(new ExitHandler());
            panel.add(btnExit);
    
            frame.getContentPane().add(panel);
            frame.setVisible(true);
        }
    
        class TotalHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    			txtTotal.setText(total +  "");
            }
        }
    
        class AddHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    			Calculator c = new Calculator();
    			            c.add();
            }
        }
    
        class MinusHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    
            }
        }
    
        class MultiplyHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    
            }
        }
    
        class DivideHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                
            }
        }
    
        class PercentHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                
            }
        }
    
        class SqrRootHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                
            }
        }
    
         class ExitHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        }
    
    
        public static void main(String[] args) {
           new ButtonDemo();
        }
    }
    What I need help with is how to call and use a method (add) and use it in the ActionEvent

    '' class AddHandler implements ActionListener {
    public void actionPerformed(ActionEvent event) {
    Calculator c = new Calculator();
    c.add();
    }
    }''

    I've tried the above but it doesn't work....

    Help please!!!

    I'm really new to Java, so simple terms please! :D

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Declare a Calculator variable in your class, not in the actionPerformed method, and use that same variable in all your GUI calculation methods.

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

    Default

    Quote Originally Posted by Fubarable View Post
    Declare a Calculator variable in your class, not in the actionPerformed method, and use that same variable in all your GUI calculation methods.
    If i do this then use c.add(); in the actionlistener for add, I'm getting error '' c.add() in Calculator cannot be applied to ()''

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ls7897 View Post
    If i do this then use c.add(); in the actionlistener for add, I'm getting error '' c.add() in Calculator cannot be applied to ()''
    Well the error message makes sense because you don't want to call the add method without passing in a double into it. If you do that, what are you adding to the Calculator? It doesn't make sense. So you must pass a double into the method, right? And you will need to extract the appropriate String from one of your JTextFields, convert it to a double via Double.parseDouble(myString) and then call the add method with a double. Please let us know if this is confusing, and if so, what in particular confuses you.

Similar Threads

  1. ActionListener issue.
    By kbro3 in forum New To Java
    Replies: 14
    Last Post: 02-24-2011, 07:08 AM
  2. Serialize ActionListener
    By Dennis in forum Advanced Java
    Replies: 4
    Last Post: 01-21-2011, 09:36 AM
  3. Determining ActionListener
    By siamino in forum New To Java
    Replies: 12
    Last Post: 05-26-2009, 12:04 AM
  4. ActionListener Error
    By blackstormattack in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 09:36 AM
  5. Java actionlistener help
    By justsomeguy in forum AWT / Swing
    Replies: 1
    Last Post: 05-27-2008, 06: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
  •