[英]Add action to a JButton created by another JButton
我有一个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");
}
}
代码没有很好的组织,但这或多或少是我想要做的
一种方法是让内部类包含侦听器:
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的实例,并将其添加到单击按钮时
您可以为每个按钮创建自己的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);
}
}
newButton
实例需要填写其actionPerformed
方法。 我看到您在按钮上添加了一个ActionListener
,但这仅表示某人正在列出操作。 上面显示的代码未在newButton
上定义任何操作,因此,不会触发任何事件,并且ActionListener
永远不会收到任何通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.