ActionListener

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    ActionListener

    Hello guys. How do you create an ActionListener. I am confused on how to do it.
    Here is my code:
    Code:
    /**
     * Written By: Edward Sanger
     * Coded in: Java
     * IDE Used: IntelliJ IDEA
     * Program Name: Calculator
     * Program Description: This program will create a GUI window with 4 buttons for 4 mathematical operators and 2 text
     * fields for the numbers to add, subtract, multiply, and divide.
     * Date: May 26, 2008
     * Time: 2:34:52 PM
     */
    
    //import declarations
    import java.awt.*;
    import javax.swing.*;
    
    public class Calculator implements ActionListener {
        public static void main(String[] args) {
            //create a new jframe
            JFrame frame = new JFrame();
            frame.setTitle("Calculator");
            //create a container
            Container contentPane = frame.getContentPane();
    
            //create the frame objects
            JTextField num1 = new JTextField("Number 1");
            JTextField num2 = new JTextField("Number 2");
    
            JButton add = new JButton("Add");
            JButton subtract = new JButton("Subtract");
            JButton multiply = new JButton("Multiply");
            JButton divide = new JButton("Subtract");
    
            JLabel result = new JLabel("Result");
    
            //add the objects
            contentPane.add(num1);
            contentPane.add(num2);
            
            contentPane.add(add);
            contentPane.add(subtract);
            contentPane.add(multiply);
            contentPane.add(divide);
    
            contentPane.add(result);
    
            //create the layout
            FlowLayout layout = new FlowLayout();
            contentPane.setLayout(layout);
    
            //create an action listener
            
    
            //frame settings
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Did you look in the new tutorial?

    This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components

    Comment

    • Kid Programmer
      New Member
      • Mar 2008
      • 176

      #3
      Originally posted by BigDaddyLH
      2 questions.

      1. What goes in the parentheses in this line of code
      Code:
      b.addActionListener(this);
      2. In my code I have four buttons. How do I create a seperate event for each one. It just says in the tutorial:
      Code:
      public void actionPerformed(ActionEvent e) {

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by Kid Programmer
        2 questions.

        1. What goes in the parentheses in this line of code
        Code:
        b.addActionListener(this);
        2. In my code I have four buttons. How do I create a seperate event for each one. It just says in the tutorial:
        Code:
        public void actionPerformed(ActionEvent e) {
        1. To find out how to use a method, look it up in the API:

        void addActionListen er(ActionListen er listener)

        So you pass addActionListen er a reference to an ActionListener.

        2. Separate events? The buttons create the events. I think you mean "separate event listeners". You create separate listeners by creating separate listeners. I think you are confused about something, because that question is like asking "how can I define four classes" or "how can I eat four apples". You go ahead and do it. Here's a quick demo:

        [CODE=Java]import java.awt.*;
        import java.awt.event. *;
        import javax.swing.*;

        class Listener1 implements ActionListener {
        public void actionPerformed (ActionEvent evt) {
        System.out.prin tln("Listener1" );
        }
        }

        class Listener2 implements ActionListener {
        public void actionPerformed (ActionEvent evt) {
        System.out.prin tln("Listener2" );
        }
        }

        class Listener3 implements ActionListener {
        public void actionPerformed (ActionEvent evt) {
        System.out.prin tln("Listener3" );
        }
        }

        class Listener4 implements ActionListener {
        public void actionPerformed (ActionEvent evt) {
        System.out.prin tln("Listener4" );
        }
        }

        public class FourListenersEx ample {
        public static void main(String[] args) {
        EventQueue.invo keLater(new Runnable(){
        public void run() {
        launch();
        }
        });
        }

        static void launch() {
        JPanel cp = new JPanel();
        configure(cp, "button 1", new Listener1());
        configure(cp, "button 2", new Listener2());
        configure(cp, "button 3", new Listener3());
        configure(cp, "button 4", new Listener4());

        JFrame f = new JFrame("FourLis tenersExample") ;
        f.setDefaultClo seOperation(JFr ame.EXIT_ON_CLO SE);
        f.setContentPan e(cp);
        f.pack();
        f.setLocationRe lativeTo(null);
        f.setVisible(tr ue);
        }

        static void configure(JPane l cp, String text, ActionListener al) {
        JButton btn = new JButton(text);
        btn.addActionLi stener(al);
        cp.add(btn);
        }
        }[/CODE]

        Comment

        • Kid Programmer
          New Member
          • Mar 2008
          • 176

          #5
          Originally posted by BigDaddyLH
          1. To find out how to use a method, look it up in the API:

          void addActionListen er(ActionListen er listener)

          So you pass addActionListen er a reference to an ActionListener.

          2. Separate events? The buttons create the events. I think you mean "separate event listeners". You create separate listeners by creating separate listeners. I think you are confused about something, because that question is like asking "how can I define four classes" or "how can I eat four apples". You go ahead and do it. Here's a quick demo:

          [CODE=Java]import java.awt.*;
          import java.awt.event. *;
          import javax.swing.*;

          class Listener1 implements ActionListener {
          public void actionPerformed (ActionEvent evt) {
          System.out.prin tln("Listener1" );
          }
          }

          class Listener2 implements ActionListener {
          public void actionPerformed (ActionEvent evt) {
          System.out.prin tln("Listener2" );
          }
          }

          class Listener3 implements ActionListener {
          public void actionPerformed (ActionEvent evt) {
          System.out.prin tln("Listener3" );
          }
          }

          class Listener4 implements ActionListener {
          public void actionPerformed (ActionEvent evt) {
          System.out.prin tln("Listener4" );
          }
          }

          public class FourListenersEx ample {
          public static void main(String[] args) {
          EventQueue.invo keLater(new Runnable(){
          public void run() {
          launch();
          }
          });
          }

          static void launch() {
          JPanel cp = new JPanel();
          configure(cp, "button 1", new Listener1());
          configure(cp, "button 2", new Listener2());
          configure(cp, "button 3", new Listener3());
          configure(cp, "button 4", new Listener4());

          JFrame f = new JFrame("FourLis tenersExample") ;
          f.setDefaultClo seOperation(JFr ame.EXIT_ON_CLO SE);
          f.setContentPan e(cp);
          f.pack();
          f.setLocationRe lativeTo(null);
          f.setVisible(tr ue);
          }

          static void configure(JPane l cp, String text, ActionListener al) {
          JButton btn = new JButton(text);
          btn.addActionLi stener(al);
          cp.add(btn);
          }
          }[/CODE]
          I am getting errors in my code for the methods. It says Identifier expected.

          Comment

          Working...