Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class JavaInnerClassActionListner extends JFrame { private JButton button1 = new JButton("Click Me!"); public static void main(String[] args) { new JavaInnerClassActionListner(); } public JavaInnerClassActionListner() { this.setSize(200, 100); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("I'm Listening"); ClickListener cl = new ClickListener(); JPanel panel1 = new JPanel(); button1.addActionListener(cl); panel1.add(button1); this.add(panel1); this.setVisible(true); } private class ClickListener implements ActionListener { private int clickCount = 0; public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { clickCount++; if (clickCount == 1) button1.setText("I've been clicked "); else button1.setText("I've been clicked!" + clickCount + " times!"); } } } } |
Output: