Tuesday, May 28, 2019

Event handling -Radio button

Program to change background color of frame  according to radio button.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
<applet code="pst19.java" width=100 height=200>
</applet>

*/
public class pst19 extends JApplet implements ActionListener
{
       Container contentPane=getContentPane();
      
       public void init()
       {
             
              contentPane.setLayout(new FlowLayout());
      
              JTextField jtf=new JTextField(17);
              contentPane.add(jtf);
             
              JRadioButton rb=new JRadioButton("Red");
              rb.addActionListener(this);
              contentPane.add(rb);
              JRadioButton rb1=new JRadioButton("Yellow");
              rb1.addActionListener(this);
              contentPane.add(rb1);
              JRadioButton rb2=new JRadioButton("Blue");
              rb2.addActionListener(this);
              contentPane.add(rb2);
      
              ButtonGroup bg=new ButtonGroup();
              bg.add(rb);
              bg.add(rb1);
              bg.add(rb2);
       }
       public void actionPerformed(ActionEvent ae)
       {
              String str;
              str=ae.getActionCommand();
      
              if(str=="Red")
              {
             
              contentPane.setBackground(Color.red);
              showStatus(str);
              }
                    
              else if(str=="Yellow")
              {
             
              contentPane.setBackground(Color.yellow);
              showStatus(str);
              }
             
              else if(str=="Blue")
              contentPane.setBackground(Color.blue);
       showStatus(str);
             
             
       }
}

No comments:

Post a Comment

java program (Tree)