2012-03-22 54 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