Practical No: 12
Name :- Minakshi Sunil Bhavar
Roll no :- 20
1.import javax.swing.*;
  public class PasswordFieldExample
 {
     public static void main(String[] args)
         {
         JFrame f=new JFrame("Password Field Example");
         JPasswordField value = new JPasswordField();
                   value.setEchoChar('#');
         JLabel l1=new JLabel("Password:");
         l1.setBounds(20,100, 80,30);
         value.setBounds(100,100,100,30);
             f.add(value); f.add(l1);
             f.setSize(300,300);
             f.setLayout(null);
                   f.setVisible(true);
     }
 }
OUTPUT:
2. import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
class Test extends JFrame implements ActionListener {
  JButton jb1;
  JTextField jt1, jt2;
  JLabel lbl;
  Test() {
    jt1 = new JTextField();
    jt1.setBounds(90, 50, 150, 30);
    add(jt1);
    jt2 = new JTextField();
    jt2.setBounds(90, 80, 150, 30);
    add(jt2);
    lbl = new JLabel("Result :");
    lbl.setBounds(90, 140, 150, 30);
    add(lbl);
     jb1 = new JButton("+");
     jb1.setBounds(90, 200, 100, 30);
     add(jb1);
     jb1.addActionListener(this);
     setLayout(null);
     setSize(600, 400);
     setVisible(true);
public void actionPerformed(ActionEvent e) {
     int a = Integer.parseInt(jt1.getText());
     int b = Integer.parseInt(jt2.getText());
     int c = 0;
     if (e.getSource().equals(jb1)) {
         c = a + b;
         lbl.setText(String.valueOf(c));
     }
}
public static void main(String args[]) {
     Test t = new Test();
}}
OUTPUT:
3. // Java Program to change the echo character of
// JPasswordField and set initial text for password field
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame implements ActionListener, FocusListener {
       // JTextField
       static JTextField t;
       // JPasswodField
       static JPasswordField pass;
       // JFrame
       static JFrame f;
       // JButton
       static JButton b;
// label to display text
static JLabel l;
// default constructor
text()
{
}
// main class
public static void main(String[] args)
{
         // create a new frame to store text field and button
         f = new JFrame("textfield");
         // create a label to display text
         l = new JLabel("nothing entered");
         // create a new button
         b = new JButton("submit");
         // create a object of the text class
         text te = new text();
         // addActionListener to button
         b.addActionListener(te);
// create a object of JTextField with 16 columns and initial text
t = new JTextField("enter name", 16);
// create a object of passwodField with 16 columns
pass = new JPasswordField(16);
// add FocusListener to passwordField
pass.addFocusListener(te);
// set the echo character of the password field
pass.setEchoChar((char)0);
// set initial text for password field
pass.setText("enter password");
// set the echo character of the password field
// create an object of font type
Font fo = new Font("Serif", Font.ITALIC, 20);
// set the font of the textfield
t.setFont(fo);
// create a panel to add buttons and textfield
JPanel p = new JPanel();
// add buttons and textfield to panel
p.add(t);
       p.add(pass);
       p.add(b);
       p.add(l);
       // add panel to frame
       f.add(p);
       // set the size of frame
       f.setSize(300, 300);
       f.show();
}
// flag to set the text to blank for the first time when the component gets focus
boolean flag = true;
// events of focus listener
// when focus is gained
public void focusGained(FocusEvent e)
{
       if (flag) {
                // set a definite echo char
                pass.setEchoChar('*');
                // only set the text to blank for 1st time
                // set the text to blank
                pass.setText("");
                flag = false;
           }
    }
    // when the focus is lost
    public void focusLost(FocusEvent e)
    {
    }
    // if the button is pressed
    public void actionPerformed(ActionEvent e)
    {
           String s = e.getActionCommand();
           if (s.equals("submit")) {
                   // set the text of the label to the text of the field
                   l.setText("name = " + t.getText() + "\t, Password = " + pass.getText());
                   // set the text of field to blank
                   t.setText(" ");
                   // set the text of password field to blank
                   pass.setText("");
           }
    }
}
OUTPUT: