You are on page 1of 2

//A GUI with event Handling using Swing

/*
Author: Niranjan A
Designation: Assistant Professor
Organization: K L University
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class SwingDemo implements ActionListener,ItemListener
{
JFrame f;
JLabel l;JLabel sex;
JLabel d;
JTextField tf;
JButton b;
String s;
JRadioButton c1,c2;
ButtonGroup cbr;
Color clr;
SwingDemo()
{
f=new JFrame("My Frame");
f.setBounds(500,240,400,400);
l=new JLabel("Enter your name");
d=new JLabel("");
sex=new JLabel("Choose your sex");
b=new JButton("Submit");
tf=new JTextField(50);
cbr=new ButtonGroup();
c1=new JRadioButton("Male");
c2=new JRadioButton("Female");
cbr.add(c1);
cbr.add(c2);
clr=new Color(70,20,75);
//clr=new Color(0.7f,0.5f,0.6f);
c1.addItemListener(this);
c2.addItemListener(this);
b.addActionListener(this);
l.setBounds(50,50,100,20);
tf.setBounds(170,50,100,20);
b.setBounds(175,80,90,50);
d.setBounds(150,180,150,50);
sex.setBounds(150,225,205,50);
c1.setBounds(50,255,65,20);
c2.setBounds(95,255,85,20);
f.add(l);f.add(tf);
f.add(b);
f.add(d);
f.add(sex);f.add(c1);f.add(c2);

1
f.setBackground(clr);
f.setForeground(Color.white);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
s="";
s=s+tf.getText();
d.setText(s);
s="";
}
public void itemStateChanged(ItemEvent e)
{
if(c1.isSelected()==false||c2.isSelected()==true)
{
s="Miss ";
}
else if(c1.isSelected()==true||c2.isSelected()==false)
{
s="Mr ";
}
s=s+tf.getText();
d.setText(s);
s="";
}
public static void main(String n[])
{
new SwingDemo();
}
}

You might also like