2012-03-22 72 views
1

我有一个Jbutton,当按下时创建另一个按钮并将新按钮添加到面板。如何添加一个actionListener到新的按钮?添加动作到由另一个JButton创建的JButton

例如:

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"); 
    } 
    } 

的代码是没有得到很好的组织,但是这或多或少是我想做的事情

+0

不,你的'aListener'类必须实现'ActionListnere',因为你正在做'addActionListener(...)'或者你写'newButton [click - 1] .addMouseListener(new aListener());'使它工作。 – 2012-03-22 15:50:54

+1

啊的确如此。你在调用addActionListener,但它应该是addMouseListener在需要aListener类的按钮上,或者你可以让类实现ActionListener而不是扩展MouseAdapter,但是你需要改变方法public void actionPerformed(ActionEvent e)的鼠标点击 – 2012-03-22 16:00:14

+0

为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。但简单来说:1)应用程序资源通常只能通过URL访问,而接受'String'的'ImageICon'构造函数将其解释为'File'路径。 2)看起来这个GUI的整个逻辑是可疑的。它打算实现什么? 3)布局更有可能兑现组件的首选尺寸,而不是尺寸。但要小心谨慎地设置首选大小,这不是轻言放弃。 4)添加一个'ActionListener'而不是'MouseListener'到按钮。 – 2012-03-22 16:04:22

回答

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"); 
     } 
    } 

这将创建一个yourListener实例,并在点击它时将其添加到按钮中

+0

但我在哪里放置内部类?它是否超出了actionPerformed()方法? – 2012-03-22 15:00:03

+0

是的,你把它放在actionPreformed之外,就像你通常对内部类进行操作一样。我通常把我的内部听众放在我的班级(毕竟方法) – 2012-03-22 15:03:28

+0

我可能做错了,它给我一个错误: 1错误发现: 文件:C:\ Users \Gonçalo\ Desktop \ MAIN PROJECTS \ File Project \ EUCountriesGUI.java [line:139] 错误:C:\ Users \Gonçalo\ Desktop \ MAIN PROJECTS \ File Project \ EUCountriesGUI.java:139:在javax中的addActionListener(java.awt.event.ActionListener)。 swing.abstractButton不能应用于(EUCountriesGUI.aListener) – 2012-03-22 15:11:42

1

newButton实例需要填写其actionPerformed方法。我看到你有一个ActionListener添加到按钮,但这只是意味着某人正在列出操作。您在上面显示的代码没有定义对该newButton的任何操作,因此没有任何事件被触发,并且ActionListener也不会收到任何通知。

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); 
    } 
} 
+1

很好的例子! Upvoted! – ron 2013-08-10 07:48:25