将动作添加到由另一个JButton创建的JButton

时间:2012-03-22 14:29:29

标签: java swing jbutton actionlistener

我有一个Jbutton按下时会创建另一个按钮,并且新按钮会添加到面板中。如何将actionListener添加到新按钮?

例如:

JButton button = new JButton("lala");
button.addActionListener(this);

    public void actionPerformed(ActionEvent event)
      {
        if (event.getSource() == button)
        {
          JButton newButton = new JButton("ahah");
          newButton.addActionListener(this);
        }
       }

我想在newButton中添加动作,我该怎么做?

已编辑代码:

 public void actionPerformed(ActionEvent event)
  {
  if (event.getSource() == button)
    {
      String name = tfOne.getText();
      Icon flag = new ImageIcon("flag/"+name+".png");
      JButton[] newButton = new JButton[click]; 
      newButton[click-1] = new JButton(name, flag);
      p2.add(newButton[click-1]);
      newButton[click-1].addActionListener(new aListener());
      p2.setLayout(new GridLayout(5+click,1)); //p2 is a panel that has been created
      setSize(500,450+(click*20));

      click++; //number of times the button is pressed
    }
  }

  public class aListener extends MouseAdapter
  { 
    public void mouseClicked(MouseEvent e)
    {
      tfOne.setText("lala");
    }
  }

代码组织不好,但这或多或少都是我想做的事情

3 个答案:

答案 0 :(得分:3)

一种方法是让内部类包含监听器:

public void actionPerformed(ActionEvent event)
  {
    if (event.getSource() == button)
    {
      JButton newButton = new JButton("ahah");
      newButton.addMouseListener(new yourListener());
    }
   }  

//add this class as a inner class
   public class aListener extends MouseAdapter
   { 
      public void mouseClicked(MouseEvent e)
      {
         JButton buttonReference=(JButton)e.getSource(); // you want this since hardcoding the name of the button is bad if you want listeners for more then one button
         buttonReference.setText("lala");
      }
    }

这将创建一个Listener实例,并在单击时将其添加到按钮

答案 1 :(得分:2)

对于每个按钮,您可以创建自己的actionPerformed(...)方法,如下例所示: 你的意思是这样做:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonAction
{
    private JPanel contentPane;
    private JButton updateButton;
    private int count = 0;
    private ActionListener updateListener = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            final JButton button = new JButton("" +  count); 
            button.setActionCommand("" + count);
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    System.out.println("My COMMAND is : " + event.getActionCommand());
                }
            });
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    contentPane.add(button);
                    contentPane.revalidate();
                    contentPane.repaint();
                }
            });
            count++;
        }
    };

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("BUTTON ACTIONS");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);

        contentPane = new JPanel();
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        updateButton = new JButton("UPDATE GUI");
        updateButton.addActionListener(updateListener);

        frame.add(contentPane, BorderLayout.CENTER);
        frame.add(updateButton, BorderLayout.PAGE_END);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        Runnable runnable = new Runnable()
        {
            public void run()
            {
                new ButtonAction().createAndDisplayGUI();
            }
        };
        SwingUtilities.invokeLater(runnable);
    }
}

答案 2 :(得分:1)

newButton实例需要填写actionPerformed方法。我看到你在按钮上添加了ActionListener,但这只是意味着有人列出了操作。您在上面显示的代码没有定义newButton上的任何操作,因此没有事件被触发,ActionListener永远不会得到任何通知。