Login form with JPasswordField in java

Опубликовано: 02 Ноябрь 2024
на канале: Khadem tech
187
9

package jpasswordfield;



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


public class jpasswordfeild {


public static void main(String[] args) {
//how to create a jpasswordfeild
JFrame f=new JFrame();
//create label
JLabel lbl1=new JLabel("Username:");
lbl1.setBounds(30, 30,120,30);
//jtextfeild
JTextField txtUsernme=new JTextField();
txtUsernme.setBounds(160,30,130,30);

//another jlabel
JLabel lbl2=new JLabel("Password:");
lbl2.setBounds(30, 70, 120, 30);

JPasswordField txtPassword=new JPasswordField();
txtPassword.setBounds(160, 70, 130, 30);

//now a jbutton
JButton btnlogin =new JButton();
btnlogin.setText("login");
btnlogin.setBounds(160,110, 130,30);
btnlogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String data="username:"+txtUsernme.getText();
data +=",password:"+new String(txtPassword.getPassword());
JOptionPane.showMessageDialog(f, data);


}
});


//now all components are added into jframe
f.add(lbl1);f.add(lbl2);f.add(txtUsernme);f.add(txtPassword);f.add(btnlogin);

f.setSize(400, 300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.setVisible(true);

}

}