Join us on Facebook

Please wait..10 Seconds Cancel

11.30.2013

// // Leave a Comment

Create three buttons and perform action event on that.

Action Event

An Event representing some type of action. This event type is widely used to represent a variety of things, 
such as when a Button has been fired, when a KeyFrame has finished, and other such usages.
import javax.swing.*; 
import java.awt.event.*; 
package button_demo; 
public class Button_Demo extends JFrame 
 { 
    public Button_Demo()  
   { 
        initComponents();  
    } 
    private void initComponents() 
   { 
        JButton jButton1 = new JButton(); 
        JButton jButton2 = new JButton(); 
        JButton jButton3 = new JButton(); 
        JLabel jLabel1 = new JLabel(); 
 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
 
        jButton1.setText("Alpha"); 
        jButton1.addActionListener(new ActionListener()  
           { 
              public void actionPerformed(ActionEvent evt) 
                 { 
                    jButton1ActionPerformed(evt); 
                 } 
           }); 
 
        jButton2.setText("Beta"); 
        jButton2.addActionListener(new ActionListener()  
          { 
              public void actionPerformed(ActionEvent evt) 
                  { 
                    jButton2ActionPerformed(evt); 
                  } 
          }); 
        jButton3.setText("Gamma"); 
        jButton3.addActionListener(new ActionListener() 
         { 
              public void actionPerformed(ActionEvent evt) 
                  { 
                    jButton3ActionPerformed(evt); 
                  } 
          }); 
        GroupLayout layout = new GroupLayout(getContentPane()); 
        getContentPane().setLayout(layout); 
        pack(); 
    } 
    private void jButton1ActionPerformed(ActionEvent evt)  
         { 
            jLabel1.setText("You have pressed Alpha"); 
         } 
    private void jButton2ActionPerformed(ActionEvent evt)  
         { 
            jLabel1.setText("You have pressed Beta"); 
         }    
 
    private void jButton3ActionPerformed(ActionEvent evt)  
       { 
            jLabel1.setText("You have pressed Gamma"); 
       } 
    public static void main(String args[]) 
       { 
                new Button_Demo().setVisible(true); 
       } 
}  

0 comments:

Post a Comment