简体   繁体   中英

Dynamically adding action listener to buttons

i think this is wrong. i want my code to add actionlistener as soon as button is created.Is there a way to do that dynamically. look at the inner for loop i have a problem adding there

import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;

/* <applet code = "Gridl.java" width=300 height=200>
   </applet> */

public class Gridl extends Applet 
{
     TextField t1=new TextField("    ");

     public void init()
     {
         int n = 1;
         setLayout(new GridLayout(4,4));
         add(t1);
         setFont(new Font("Tahoma",Font.BOLD,24));

         for(int i=0;i<4;i++)
         {
             for(int j=0;j<4;j++)
             {
                 add(new Button(""+n));        
                 this.addActionListener(this);       // this didnt work :(
                 n++;
             }
         }  
    }

    public void actionPerformed(ActionEvent ae)
    {
        String str = ae.getActionCommand();
        t1.setText(str);
    }

}

try like this when creating button new button().add without ending that statement.

  new Button("").addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            System.out.println("You clicked the button");
        }
    });    

I think you have some conceptual misunderstandings reflected in your code here. It's important to consider which component you are adding the ActionListener onto. At the moment, your code adds the ActionListener to the Gridl object extending Applet, rather than the button itself. It won't throw an exception, because that's valid, but it won't give you the behaviour you want

To get you working, I suggest you replace

add(new Button(""+n));

with

Button b = new Button(""+n);
b.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e)
    {
        System.out.println("You clicked button "+e.getSource().toString());
    }
});

this.add(b);

Note that this sets up a new ActionListener for every Button object, with its behaviour determined by what you put in the actionPerformed() method. You can have the same behaviour for all buttons, or different for each.

I would suggest that you might want to read the oracle Java Swing GUI tutorials, in particular the one on actions . There are code samples there too.

EDIT:

I have realised you might want to have your Gridl be the listener for all buttons. In which case - you can achieve this by:

public class Gridl extends Applet implements ActionListener
{
     TextField t1=new TextField("    ");

     public void init()
     {
         int n = 1;
         setLayout(new GridLayout(4,4));
         add(t1);
         setFont(new Font("Tahoma",Font.BOLD,24));

         for(int i=0;i<4;i++)
         {
             for(int j=0;j<4;j++)
             {
                 Button b = new Button(""+n));        
                 b.addActionListener(this);
                 this.add(b);
                 n++;
             }
         }  
    }

    public void actionPerformed(ActionEvent ae)
    {
        String str = ae.getActionCommand();
        t1.setText(str);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM