Implement the ActionListener Interface As An Inner Class

Program

rtert

 

 

 

Program Source

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class Frame extends JFrame {
    
    private JButton jb;
    
    Frame()
    {
        setTitle("JButton with ActionListener");
        setLayout(new FlowLayout());
        setButton();
        setAction();
        setSize(700, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void setButton()
    {
        jb = new JButton("JButton");
        add(jb);
    }
    
    private void setAction()
    {
        jb.addActionListener(new Action());
    }
    
    private class Action implements ActionListener{
        
        public void actionPerformed(ActionEvent eve)
        {
            JOptionPane.showMessageDialog(rootPane, "JButton Pressed");
        }
    }
}

public class Javaapp {
   
    public static void main(String[] args) {
        
        Frame frame = new Frame();
    }
}

Leave a Comment