Multiple Function Button

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

public class MultipleFunctionButton extends JFrame{
   
    private JTextArea txt=new JTextArea();
    private JButton btn1=new JButton();
    private JButton btn2=new JButton("Reset");
   
    public MultipleFunctionButton(){
        Container canvas=getContentPane();
        canvas.setLayout(null);
       
        canvas.add(btn1);
        canvas.add(btn2);
        canvas.add(txt);
       
        btn1.setBounds(10,10,120,25);
        btn2.setBounds(10,40,120,25);
        txt.setBounds(140,10,150,150);       
               
        function1();
       
        btn2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                txt.setText("      ");
                txt.setEditable(true);
               
                btn1.removeActionListener(this);
                function1();
            }
        });
       
        txt.setText("      ");
        canvas.setBackground(Color.BLACK);
       
        this.setBounds(100,100,320,200);       
        this.setVisible(true);
    }
   
    public void function1(){
        btn1.setText("Done");
        btn1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                txt.setEditable(false);
                btn1.removeActionListener(this);
                function2();
            }
        });
    }
   
    public void function2(){
        btn1.setText("Edit");
        btn1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                txt.setEditable(true);
                btn1.removeActionListener(this);
                function1();
            }
        });
    }
   
    public static void main(String []args){
        MultipleFunctionButton mfb=new MultipleFunctionButton();
        mfb.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Sample Running :


                                                                                                                                                      Created By : Z-man, 2012

No comments:

Post a Comment