ive created a program using the actionperformedmethod...even though the panel and all other
things r coming in the output nothing is happening when i perform an action....

here is the program......

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

public class goku extends JFrame implements ActionListener  
{
    int clicked=0;
    String s="No of times clicked";
    JButton b1=new JButton("saiyan");
    JButton b2=new JButton("quit");
    JLabel l=new JLabel(s +clicked);
    public goku(String str)
    {
    super(str);
    getContentPane().setLayout(new FlowLayout());
    JPanel p=new JPanel();
    p.setLayout(new FlowLayout());
    p.createImage(50,30);
    b1.setActionCommand("enable");
    b2.setActionCommand("disable"); 
    l.setLocation(50,30);
    p.add(b1);
    p.add(b2);
    getContentPane().add(p);
    getContentPane().add(l);
    getContentPane().setBackground(Color.YELLOW);
    getContentPane().setForeground(Color.blue);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b1.setToolTipText("saiyan");
    b2.setToolTipText("click here to quit");
    } 

  public void actionPerformed(ActionEvent e)
{
 String s1=e.getActionCommand();
 
    if("quit".equals(s1))
    {
    System.exit(0);
    
    }
    else
    if("saiyan".equals(s1))
    {clicked++;
     l.setText(s +clicked );
     
    }
        
 
 }   
        
public static void Main(String args[])
{
 goku g=new goku("ssj"); 
 g.setSize(400, 300);
 g.getIconImage();
 g.setVisible(true);
}        
 
}

Recommended Answers

All 2 Replies

....
    b1.setActionCommand("enable");
    b2.setActionCommand("disable"); 
....
 String s1=e.getActionCommand();
....
    if("quit".equals(s1))
....
    if("saiyan".equals(s1))
....

Enough said.

or:

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == b1) {
             // b1 is clicked, do some action here
    } else if (e.getSource() == b2) {
             // b2 is clicked, do some action here
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.