Wednesday, March 05, 2014

Write a GUI program in Java that will have two buttons viz., RED and GREEN. If user clicks on RED button the background of the GUI will be painted in red or if user clicks on GREEN button the background of the GUI will be painted green



………………………………………………………………………………………….

Save this file as=> ex.java
………………………………………………………………………………………..
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class ex extends Applet implements ActionListener
{
            Button b1,b2;
           
            public void init()
            {
                        b1=new Button("RED");
                        add(b1);
                        b1.addActionListener(this);

                        b2=new Button("GREEN");
                        add(b2);
                        b2.addActionListener(this);
            }

            public void actionPerformed(ActionEvent e1)
            {
                        if(e1.getSource()==b1)
                        {
                                    setBackground(Color.red);
                        }                     
                       
                        if(e1.getSource()==b2)
                        {
                                    setBackground(Color.green);
                        }
            }

}
…………………………………………………………………………………………
compile this file: javac ex.java

………………………………………………………………………………………..
HTML file ex.html
<applet code ="ex.class"
width=200
 height=200>
</applet>


Output:
                    

No comments:

Post a Comment