AWT Button

Set Action Command For AWT Button Example

Example Output

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

Add Comment

  • import java.awt.*;
    import java.awt.event.*;

    public class sri extends Frame implements ActionListener
    {
    public Button b,b1;
    sri()
    {
    Button b=new Button(“India”);
    Button b1=new Button(“Pakistan”);
    add(b);
    add(b1);
    setVisible(true);
    setBounds(0,0,1024,768);
    b.addActionListener(this);
    b1.addActionListener(this);
    setLayout(null);
    b.setBounds(100,200,100,20);
    b1.setBounds(100,20,110,20);
    addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent ee)
    {
    System.exit(0);
    }
    });

    }
    public void actionPerformed(ActionEvent e)
    {
    if(b==e.getSource())
    {
    System.out.println(“Hello”);
    }
    else if(e.getSource()==b1)
    {
    System.out.println(“hi”);
    }
    }
    public static void main(String args[])
    {
    sri s=new sri();
    }
    }

    What is wrong with this code

Sponsors

Facebook Fans