Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 3 of 3 FirstFirst 123
Results 51 to 59 of 59

Thread: how do I add Action Listener to my code to make the calculator work

  1. #51
    Junior Member
    Join Date
    Mar 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do I add Action Listener to my code to make the calculator work

    i dont what to you mean can you provide the whole completed code for the actionlistener and the method please

  2. #52
    Junior Member
    Join Date
    Mar 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Actionlistener help look at code need button to work for calculator

    need help can someone give the code which i need to allow my calculator to allow the button created from the nested for loop to function with an actionlistener.

    here the code;

     
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class EX_7_2 extends JFrame 
        {
            public EX_7_2()
            {
                setLayout(new BorderLayout(5, 10));
     
                // create p1
                JPanel p1 = new JPanel();
                p1.setLayout(new BorderLayout(5, 10));
     
              p1.add(new JTextField(), BorderLayout.NORTH);
                        JPanel p3 = new JPanel();
     
             p3.setLayout(new GridLayout(1, 3, 5, 5));
             JButton btBackspace = new JButton ("Backspace");
             btBackspace.setBackground(Color.WHITE);
             btBackspace.setForeground(Color.RED);
     
             JButton btCE = new JButton ("CE");
             btCE.setBackground(Color.WHITE);
             btCE.setForeground(Color.RED);
     
             JButton btC = new JButton ("C");
             btC.setBackground(Color.WHITE);
             btC.setForeground(Color.RED);
     
              p3.add(btBackspace);
              p3.add(btCE);
              p3.add(btC);
     
              p1.add(p3, BorderLayout.CENTER);
     
     
     
              // create p4
     
              JPanel p2 = new JPanel();
              p2.setLayout(new GridLayout(4, 3, 5, 5));  
     
              int num;
              for (int iRow = 3; iRow>0; iRow--)
              {
                  for (int iCol = 1; iCol<4; iCol++)
                  {
                      num = 3*(iRow-1) + iCol;
     
                      final JButton btBody = new JButton("" + num);
                      btBody.setBackground(Color.WHITE);
                      btBody.setForeground(Color.BLUE);
                      p2.add(btBody);
     
                       btBody.addActionListener(new ActionListener() 
                       {
                        @Override /**HandleItemEvent*/
                        public void actionPerformed(ActionEvent e) 
                            {
     
     
                              }
     
     
     
                      });
                  }           
              }
     
             JButton btZero = new JButton ("0");
             btZero.setBackground(Color.WHITE);
             btZero.setForeground(Color.BLUE);
     
             JButton btPlusMinus = new JButton ("+/-");
             btPlusMinus.setBackground(Color.WHITE);
             btPlusMinus.setForeground(Color.BLUE);
     
             JButton btDot = new JButton (".");
             btDot.setBackground(Color.WHITE);
             btDot.setForeground(Color.BLUE);
     
             p2.add(btZero);
              p2.add(btPlusMinus);
              p2.add(btDot);
     
     
             JPanel p5 = new JPanel();
             p5.setLayout(new GridLayout(4, 2, 5, 5));
     
             JButton btBack = new JButton ("/");
             btBack.setBackground(Color.WHITE);
             btBack.setForeground(Color.RED);
     
             JButton btsprt = new JButton ("sprt");
             btsprt.setBackground(Color.WHITE);
             btsprt.setForeground(Color.BLUE);
     
             JButton btStar = new JButton ("*");
             btStar.setBackground(Color.WHITE);
             btStar.setForeground(Color.RED);
     
             JButton btPercentage = new JButton ("%");
             btPercentage.setBackground(Color.WHITE);
             btPercentage.setForeground(Color.BLUE);
     
             JButton btMinus = new JButton ("-");
             btMinus.setBackground(Color.WHITE);
             btMinus.setForeground(Color.RED);
     
             JButton btOnex = new JButton ("1/x");
             btOnex.setBackground(Color.WHITE);
             btOnex.setForeground(Color.BLUE);
     
             JButton btPlus = new JButton ("+");
             btPlus.setBackground(Color.WHITE);
             btPlus.setForeground(Color.RED);
     
             JButton btEquals = new JButton ("=");
             btEquals.setBackground(Color.WHITE);
             btEquals.setForeground(Color.RED);
     
             p5.add(btBack);
               p5.add(btsprt);
                p5.add(btStar);
                 p5.add(btPercentage);
                  p5.add(btMinus);
                   p5.add(btOnex);
                    p5.add(btPlus);
                     p5.add(btEquals);
     
                     JPanel p4 = new JPanel();
                     p4.setLayout(new BorderLayout(5, 10));
                     p4.add(p2, BorderLayout.CENTER);                
          p4.add(p5, BorderLayout.EAST);          
     
          add(p1, BorderLayout.NORTH );
          add(p4, BorderLayout.CENTER);
     
     
     
        }             
     
     
    public static void main(String[] args) {
     
            EX_7_2 frame = new EX_7_2();
            frame.setTitle("Calculator");
            frame.setSize(350, 375);
                  frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
     
    }
    }

  3. #53
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: how do I add Action Listener to my code to make the calculator work

    You must feel a little bit frustrated alex. Be patient and Norm will help you get through this. For now, in order to keep things simple, why don't you create new objects for each button rather than having a loop do it. With that in place, all you need are action listeners for each button.
    Who holds the KEY to all knowledge?

  4. #54
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,760
    My Mood
    Mellow
    Thanks
    233
    Thanked 712 Times in 698 Posts

    Default Re: how do I add Action Listener to my code to make the calculator work

    @alex1994: Please don't open duplicate threads. Threads merged.

  5. #55
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: how do I add Action Listener to my code to make the calculator work

    What is the code in the action listener supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #56
    Junior Member
    Join Date
    Mar 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do I add Action Listener to my code to make the calculator work

    the actionlistener fr the button in the loop are suppose, once you link on them; that particular number should show up in the JTextField

    the code for the JTextField

      // create p1
                JPanel p1 = new JPanel();
                p1.setLayout(new BorderLayout(5, 10));
     
              p1.add(new JTextField(), BorderLayout.NORTH);


    --- Update ---

    the actionlistener for the button in the loop are suppose, once you have clicked on them; that particular number should show up in the JTextField;

    code for JTextFeild

    // create p1
                JPanel p1 = new JPanel();
                p1.setLayout(new BorderLayout(5, 10));
     
              p1.add(new JTextField(), BorderLayout.NORTH);

  7. #57
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: how do I add Action Listener to my code to make the calculator work

    The ActionEvent object passed to the actionlistener method contains a reference to the button that was pressed. Use the event class's getSource() method to get a reference to that button.
    When you get the reference to the button object, you can call one of its methods to get the String that is shown on the button.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #58
    Junior Member
    Join Date
    Mar 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do I add Action Listener to my code to make the calculator work

    how do use the getSource() could i have an example.

  9. #59
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,760
    My Mood
    Mellow
    Thanks
    233
    Thanked 712 Times in 698 Posts

    Default Re: how do I add Action Listener to my code to make the calculator work

    Topic continues in this thread.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. [SOLVED] Anonymous inner class with Action Listener and Abstract Action
    By shakes6791 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 13th, 2013, 02:52 PM
  2. Replies: 1
    Last Post: February 27th, 2013, 08:16 AM
  3. add action listener problem
    By mdhmdh100 in forum AWT / Java Swing
    Replies: 5
    Last Post: February 5th, 2012, 01:53 AM
  4. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM