Results 1 to 2 of 2
  1. #1
    neeeqa is offline Member
    Join Date
    Feb 2015
    Rep Power

    Default how to create an actionlistener for multiple buttons

    My task is to make a BorderLayout and when clicking a button it should say "Going Right!", "Going Up!" etc but I don't know how to create that, help please

    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    public class BorderLayoutJFrame extends JFrame
        public static final int WIDTH = 400;
        public static final int HEIGHT = 400;
        public BorderLayoutJFrame()
           super ("BorderLayoutDemo");
           setSize (WIDTH, HEIGHT);
           JButton[] buttons = {new JButton("UP"), new JButton("RIGHT"),
                         new JButton("DOWN"), new JButton("LEFT")};
           JLabel label = new JLabel("Centre");
           add(label, BorderLayout.CENTER);
           add(buttons[0], BorderLayout.NORTH);
           add(buttons[1], BorderLayout.EAST);
           add(buttons[2], BorderLayout.SOUTH);
           add(buttons[3], BorderLayout.WEST);

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: how to create an actionlistener for multiple buttons

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Help....add actionListener to buttons
    By jmeats77 in forum New To Java
    Replies: 9
    Last Post: 05-24-2012, 02:11 AM
  2. Create multiple buttons inside jscrollbar
    By tyrantblizz in forum AWT / Swing
    Replies: 1
    Last Post: 03-28-2012, 04:31 AM
  3. Best Way to Run a method on multiple buttons
    By Dcalladi in forum New To Java
    Replies: 1
    Last Post: 11-07-2011, 04:45 AM
  4. Multiple JButton/ActionListener problem
    By graympa in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 01:48 AM
  5. Replies: 6
    Last Post: 02-01-2010, 10:30 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