Create the following GUI screen using appropriate layout managers. Accept the name, class , hobbies of the user and display the selected options in a text box.
ex6seta2.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GUI extends JFrame implements ItemListener
{
JLabel l1,l2,l3;
JButton b;
JRadioButton r1,r2,r3;
JCheckBox c1,c2,c3;
JTextField t1,t2;
ButtonGroup b1;
JPanel p1,p2;
GUI()
{
b1=new ButtonGroup();
p1=new JPanel();
p2=new JPanel();
b=new JButton("Clear");
b.addItemListener(this);
r1=new JRadioButton("FY");
r2=new JRadioButton("SY");
r3=new JRadioButton("TY");
b1.add(r1);
b1.add(r2);
b1.add(r3);
r1.addItemListener(this);
r2.addItemListener(this);
r3.addItemListener(this);
c1=new JCheckBox("Music");
c2=new JCheckBox("Dance");
c3=new JCheckBox("Sports");
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
l1=new JLabel("Your Name");
l2=new JLabel("Your Class");
l3=new JLabel("Your Hobbies");
t1=new JTextField(20);
t2=new JTextField(30);
p1.setLayout(new GridLayout(5,2));
p1.add(l1);p1.add(t1);
p1.add(l2);p1.add(l3);
p1.add(r1);p1.add(c1);
p1.add(r2); p1.add(c2);
p1.add(r3);p1.add(c3);
p2.setLayout(new FlowLayout());
p2.add(b);
p2.add(t2);
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.EAST);
setSize(400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e)
{
String msg="Your Name : "+t1.getText()+ " ";
if(r1.isSelected())
msg+="Class "+r1.getText()+" ";
else if(r2.isSelected())
msg+="Class" +r2.getText()+" ";
else
{
msg+="Class" +r3.getText()+" ";
}
msg+="Your Hobbies : "+" ";
if(c1.isSelected())
msg+=c1.getText()+" ";
if(c2.isSelected())
msg+=c2.getText()+" ";
if(c3.isSelected())
msg+=c3.getText()+" ";
t2.setText(msg);
}
public static void main(String arg[])
{
new GUI();
}
}
0 Comments
If anyone has Doubts or suggestions please let me know