Batusai
Batusai

Reputation: 149

ActionListener,Performed Gui

how to put an actionListener or ActionEvent on my gui program because i cannot put function of my button please help me this is my code so far.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import javax.swing.*;
public class jiw extends JFrame
{
    private JTextField aw1;
    private JLabel aww;
    private JButton aw2;

    public jiw()
    {
        setLayout(new FlowLayout());


        aww = new JLabel("Enter Your Password");
        add(aww);

        aw1 = new JTextField(15);
        add(aw1);

        aw2 = new JButton("Enter");
        add(aw2);
        setVisible(true);

         actionPerformed(ActionEvent e)
        {
            Object source = e.getSource();

            if(source == aw2)
            {
                System.exit(0);
            }
        }





    }
    public static void main(String args [])
    {
    jiw v = new jiw();

            v.setSize(200,200);
            v.setResizable(false);
            v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

Upvotes: 0

Views: 47

Answers (1)

AJ.
AJ.

Reputation: 4534

implements ActionListener first then override actionPerformed(ActionEvent e)

public class JavaApplication1 extends JFrame implements ActionListener
    {
        private JTextField aw1;
        private JLabel aww;
        private JButton aw2;

        public JavaApplication1()
        {
            setLayout(new FlowLayout());

            aww = new JLabel("Enter Your Password");
            add(aww);

            aw1 = new JTextField(15);
            add(aw1);

            aw2 = new JButton("Enter");
            add(aw2);
            setVisible(true);

        }
        public static void main(String args [])
        {
        JavaApplication1 v = new JavaApplication1();

                v.setSize(200,200);
                v.setResizable(false);
                v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // Add your functionality here
        }
    }

Upvotes: 2

Related Questions