Online-Academy

Look, Read, Understand, Apply

Menu

Example_2_2022

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

class reg extends JFrame implements ActionListener{
    JLabel lname,laddress,lgen,lhob;
    JTextField tname,taddress;
    JRadioButton r1,r2;
    JCheckBox c1,c2,c3;
    JButton btn,btn1;
    public reg(){
        setLayout(null);
        lname= new JLabel("Name");
        lname.setBounds(20,20,100,25);
        tname = new JTextField(20);
        tname.setBounds(130,20,100,25);
        laddress= new JLabel("Address");
        laddress.setBounds(20,50,100,25);
        taddress = new JTextField(20);
        taddress.setBounds(130,50,100,25);
        add(lname);add(tname);
        add(laddress);add(taddress);
        lgen = new JLabel("Gender");
        lgen.setBounds(20,80,100,25);
        r1 = new JRadioButton("Male");
        r1.setBounds(130,80,80,25);
        r2 = new JRadioButton("FeMale");
        r2.setBounds(210,80,80,25);
        lhob = new JLabel("Hobby");
        lhob.setBounds(20,110,100,25);
        c1 = new JCheckBox("Reading");
        c1.setBounds(130,110,100,25);
        c2 = new JCheckBox("Fighting");
        c2.setBounds(230,110,100,25);
        add(lgen);add(r1);add(r2);
        add(lhob);add(c1);add(c2);
        btn = new JButton("Click");
        btn.setBounds(20,140,100,25);
        btn1 = new JButton("Adman");
        btn1.setBounds(130,140,100,25);
        add(btn);
        btn.addActionListener(this);
        add(btn1);
        btn1.addActionListener(this);
        setSize(400,300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent ae){
        String s = ae.getActionCommand();
        if (s.equals("Click")) {
            //s = tname.getText()+" "+taddress.getText();
            JDialog d = new JDialog(this, "shikandar");
            JLabel l = new JLabel("Name:"+tname.getText());
            JLabel ll = new JLabel("Address: "+taddress.getText());
            d.add(l,BorderLayout.EAST);
            d.add(ll,BorderLayout.WEST);
            d.setSize(300, 200);
            d.setVisible(true);
        }
        if (s.equals("Adman")) {
            //s = tname.getText()+" "+taddress.getText();
            JDialog d = new JDialog(this, "dialog Box");
            JLabel l = new JLabel("ADman:"+tname.getText());
            JLabel ll = new JLabel("Address: "+taddress.getText());
            d.add(l,BorderLayout.EAST);
            d.add(ll,BorderLayout.WEST);
            d.setSize(300, 200);
            d.setVisible(true);
        }
    }
    public static void main(String[] o){
        new reg();
    }
}