Below code adds ActionListener to a JButton using an Inner class.See also JButton add Anonymous Class ActionListener /** * * @author...
Below code adds ActionListener to a JButton using an Inner class.See also JButton add Anonymous Class ActionListener
/**
*
* @author Eric
* site:www.download-all.net
*/
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JButtonListener extends JFrame {
public JButton button;
public int count=0;
JButtonListener()
{
super();
setSize(300,300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button=new JButton("Ok");
button.setBounds(10, 10, 200, 20);
button.addActionListener(new Listener());
add(button);
setVisible(true);
}
public static void main(String args[])
{
new JButtonListener();
}
public class Listener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
count++;
button.setText("Clicked "+ count);
}
}
}