Four types of listeners (self class, external class, Inner class, anonymous class)

Source: Internet
Author: User
Tags event listener

  1. Import javax.swing.*;
  2. Import java.awt.*;
  3. Import java.awt.event.*;
  4. Public class Thisclassevent extends JFrame implements actionlistener{
  5. Public thisclassevent () {
  6. SetLayout (new FlowLayout ());
  7. JButton btn=New JButton ("OK");
  8. Add (BTN);
  9. Btn.addactionlistener (this);
  10. }
  11. public void actionperformed (ActionEvent e) {
  12. System.out.println ("The OK button is clicked");
  13. }
  14. public static void Main (String args[]) {
  15. Thisclassevent frame = new Thisclassevent ();
  16. Frame.settitle ("Self-class as Event listener");
  17. Frame.setlocationrelativeto (null); //Center the frame
  18. Frame.setdefaultcloseoperation (Jframe.exit_on_close);
  19. Frame.setsize (280, 100);
  20. Frame.setvisible (true);
  21. new Thisclassevent ();
  22. }
  23. }
  24. Import java.awt.*;
  25. Import java.awt.event.*;
  26. Import javax.swing.*;
  27. Public class Outerclassevent extends jframe{
  28. Public outerclassevent () {
  29. SetLayout (new FlowLayout ());
  30. JButton btn=New JButton ("OK");
  31. Add (BTN);
  32. Outerclass btlistener=New Outerclass ();
  33. Btn.addactionlistener (Btlistener);
  34. }
  35. public static void Main (String args[]) {
  36. Outerclassevent frame = new Outerclassevent ();
  37. Frame.settitle ("external class as Event listener");
  38. Frame.setlocationrelativeto (null); //Center the frame
  39. Frame.setdefaultcloseoperation (Jframe.exit_on_close);
  40. Frame.setsize (280, 100);
  41. Frame.setvisible (true);
  42. new Thisclassevent ();
  43. }
  44. }
  45. Class Outerclass implements actionlistener{
  46. public void actionperformed (ActionEvent e) {
  47. System.out.println ("The OK button is clicked");
  48. }
  49. }
  50. Import java.awt.*;
  51. Import java.awt.event.*;
  52. Import javax.swing.*;
  53. Class Innerclassevent extends jframe{
  54. Public innerclassevent () {
  55. SetLayout (new FlowLayout ());
  56. JButton btn=New JButton ("OK");
  57. Add (BTN);
  58. Outerclass btlistener=New Outerclass ();
  59. Btn.addactionlistener (Btlistener);
  60. }
  61. class Innerclass implements actionlistener{
  62. public void actionperformed (ActionEvent e) {
  63. System.out.println ("The OK button is clicked");
  64. }
  65. }
  66. public static void Main (String args[]) {
  67. Innerclassevent frame = new Innerclassevent ();
  68. Frame.settitle ("inner class as Event listener");
  69. Frame.setlocationrelativeto (null); //Center the frame
  70. Frame.setdefaultcloseoperation (Jframe.exit_on_close);
  71. Frame.setsize (280, 100);
  72. Frame.setvisible (true);
  73. new Thisclassevent ();
  74. }
  75. }
  76. Import java.awt.*;
  77. Import java.awt.event.*;
  78. Import javax.swing.*;
  79. Class Anonymousevent extends jframe{
  80. Public anonymousevent () {
  81. SetLayout (new FlowLayout ());
  82. JButton btn=New JButton ("OK");
  83. Add (BTN);
  84. Btn.addactionlistener (
  85. new ActionListener () { //anonymous inner class as parameter, new one lisenter actually creates a class that implements this listener
  86. public void actionperformed (ActionEvent e) {
  87. System.out.println ("The OK button is clicked");
  88. }
  89. }
  90. );
  91. }
  92. public static void Main (String args[]) {
  93. Anonymousevent frame = new Anonymousevent ();
  94. Frame.settitle ("Anonymous internal class as Event listener");
  95. Frame.setlocationrelativeto (null); //Center the frame
  96. Frame.setdefaultcloseoperation (Jframe.exit_on_close);
  97. Frame.setsize (280, 100);
  98. Frame.setvisible (true);
  99. new Thisclassevent ();
  100. }
  101. }

Four types of listeners (self class, external class, Inner class, anonymous class)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.