Skip to main content

) Create the following GUI screen using appropriate layout managers. Accept the name, class , hobbies of the user and apply the changes and display the selected options in a text box.

 

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

class Swing2 extends JFrame implements ActionListener
{
    JLabel l1,l2,l3;
        JButton b;
        JRadioButton r1,r2,r3;
        JCheckBox c1,c2,c3;
        JTextField t1,t2;
        ButtonGroup b1;
        JPanel p1,p2;
    static int cnt;
        private StringBuffer s1=new StringBuffer();
       
        Swing2()
        {
           
                b1=new ButtonGroup();
                p1=new JPanel();
                p2=new JPanel();
                b=new JButton("Clear");
                b.addActionListener(this);
               
                r1=new JRadioButton("FY");
                r2=new JRadioButton("SY");
                r3=new JRadioButton("TY");
               
                b1.add(r1);
                b1.add(r2);
                b1.add(r3);
                r1.addActionListener(this);
                r2.addActionListener(this);
                r3.addActionListener(this);
               
                c1=new JCheckBox("Music");
                c2=new JCheckBox("Dance");
                c3=new JCheckBox("Sports");
               
                c1.addActionListener(this);
                c2.addActionListener(this);
                c3.addActionListener(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 actionPerformed(ActionEvent e)
        {
           
                if(e.getSource()==r1)
                {
                    cnt++;
                    if(cnt==1)
                    {
                        String s =t1.getText();
                            s1.append("Name = ");
                            s1.append(s);
                    }
                    s1.append(" Class = FY");
                }
                else if(e.getSource()==r2)
                {
                    cnt++;
                    if(cnt==1)
                    {
                        String s =t1.getText();
                            s1.append("Name = ");
                            s1.append(s);
                    }
                    s1.append(" Class = SY");
                }
                else if(e.getSource()==r3)
                {
                    cnt++;
                    if(cnt==1)
                    {
                        String s =t1.getText();
                            s1.append("Name = ");
                            s1.append(s);
                    }
                    s1.append(" Class = TY");
                }

           
                else if(e.getSource()==c1)
                {
                    s1.append(" Hobbies = Music");
                }
                else if(e.getSource()==c2)
                {
                    s1.append(" Hobbies = Dance");
                }
                else if(e.getSource()==c3)
                {
                    s1.append(" Hobbies = Sports");
                }
           
                t2.setText(new String(s1));
                //              t2.setText(s2);
               
                if(e.getSource()==b)
                {
                    t2.setText(" ");
                        t1.setText(" ");
                }
           
        }
   
        public static void main(String arg[])
        {
            Swing2 s=new Swing2();
               
        }
}

Comments

Popular posts from this blog

Write a program to read book information (bookid, bookname, bookprice, bookqty) in file “book.dat”. Write a menu driven program to perform the following operations using Random access file: i. Search for a specific book by name. ii. Display all book and total cost

  import java . io .*; import java . util .*; class Book {       String name , id ;       int qty ;       double price , total ;       Book ( String i , String n , String p , String q )      {               name = n ;               id = i ;               qty = Integer . parseInt ( q );               price = Double . parseDouble ( p );               total = qty * price ;      }       public String toString ()      {               System . out . println ( "name\t id\t qty\t price\t total" );               String s = name + "\t" + id + "\t" + qty + "\t" + price + "\t" + total ;           ...

Define a class MyDate (day, month, year) with methods to accept and display a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” if the date is invalid. Examples of invalid dates : 03 15 2019, 31 6 2000, 29 2 2021

  import java . io .*; import java . util .*; class InvalidDateException extends Exception {       InvalidDateException ()       {               System . out . println ( "Invalid Date" );       } } class MyDate {       int day , mon , yr ;       void accept ( int d , int m , int y )       {             day = d ;             mon = m ;             yr = y ;       }       void display ()       {             System . out . println ( "Date is valid : " + day + "/" + mon + "/" + yr );       } } class Date {       public static void main ( String args []) throws Exception       {             Scanner sc = new Sca...

Write a Java program to design a screen using Awt that will take a user name and password. If the user name and password are not same, raise an Exception with appropriate message. User can have 3 login chances only. Use clear button to clear the TextFields.

  import java . awt .*; import java . awt . event .*; import javax . swing .*; class InvalidPasswordException extends Exception {       InvalidPasswordException ()       {             System . out . println ( "Username and password is not same" );       } } public class Password extends Frame implements ActionListener {       Label uname , upass ;       TextField nametext ;       TextField passtext , msg ;       Button login , Clear ;       Panel p ;       int attempt = 0 ;       char c = '*' ;             public void login ()       {             p = new Panel ();             uname = new Label ( "Username : " , Label . CENTER );             upass = n...