Pin It

How to add Event Handling to a Button using Anonymous class in Java

Program to demonstrate event handling in a Swing Button using anonymous class.
package com.hubberspot.awtSwing.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ButtonEventHandling 
{
  JFrame frame;
  JButton button;
  JTextField textField;

  ButtonEventHandling()
  {
    frame=new JFrame("Welcome to Hubberspot");
    frame.setLayout(new FlowLayout());
    button=new JButton("Like Us!");
    textField=new JTextField(20);

    button.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent ae) 
      {
 textField.setText("Thanks for liking Hubberspot!");
      }
    });

    frame.setSize(100,100);
    frame.add(textField);
    frame.add(button);

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
 
  public static void main(String[] args)
  {
    new ButtonEventHandling();
  }
}

Output of the program :

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot