java中提供密码输入的方法是什么?

java中提供密码输入的方法是什么?,第1张

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

就是有扰历个戚颂对话框,输入的时候显示的是星号。谢谢

解析:

import javax.swing.*

import java.awt.*

import java.awt.event.*

class C {

public static void main( String[] args ) {

JFrame f = new JFrame( )

f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE )

f.setSize( 300, 140 )

f.setLocation( 250, 250 )

Container c = f.getContentPane( )

c.setLayout( new FlowLayout( ) )

JPasswordField

c.add( new JLabel( "Enter your password into this JPasswordField: " ) )

JPasswordField jpf = new JPasswordField( 10 )

jpf.addActionListener(

new ActionListener( ) {

public void actionPerformed( ActionEvent e ) {

Object pwd =

new String( ( ( JPasswordField ) e.getSource( ) ).getPassword( ) )

JOptionPane.showMessageDialog( null, "Your password is " + pwd )

}

}

)

c.add( jpf )

TextField

c.add( new JLabel( "Or enter your password into this TextField: " ) )

TextField tf = new TextField( 12 )

tf.setEchoChar( '缓仔搜*' )

tf.addActionListener(

new ActionListener( ) {

public void actionPerformed( ActionEvent e ) {

Object pwd = ( ( ( TextField ) e.getSource( ) ).getText( ) )

JOptionPane.showMessageDialog( null, "Your password is " + pwd )

}

}

)

c.add( tf )

f.setVisible( true )

}

}

public static void main(String[] args) {

System.out.println("请输入局纤密码桐姿仿:")

Scanner scanner=new Scanner(System.in)

String password=scanner.nextLine()

System.out.println("你册梁输入的密码是:"+password)

scanner.close()

}

已完成,复制粘贴即可。

import java.util.Scanner

public class YuGiOh

{

private static void checkPass ( String password )

{

String regp = ".*[A-Z].*"

String rego 做正御= ".*[a-z].*"

String regq = ".*\\d.*"

String regs = ".*[\\`\\-\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\+\\=\\[\\{\\}\\]\\\\:\\,\\.\\<\\>纯岩\\/\\?].*"

String regex = "^[a-zA-Z\\d\\`\\-\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\+\\=\\[\\{\\}\\]\\\\:\\,\\.\\<\\>\\/\\?]{6,}$"

String result = ""

if (password.length () < 6)

{

result += "\t\t-contain at least 清郑six characters. Your password is only " + password.length () + " characters long.\n"

}

if (!password.matches (regp))

{

result += "\t\t-contain at least one uppercase letter.\n"

}

if (!password.matches (rego))

{

result += "\t\t-contain at least one lowercase letter.\n"

}

if (!password.matches (regq))

{

result += "\t\t-contain at least one number.\n"

}

if (!password.matches (regs))

{

result += "\t\t-contain at least one of the following special characters: `!@#$%^&*()_+=[{}]:,.<>/?\n"

}

else if (!password.matches (regex))

{

result += "\t\t-not contain an invalid character. The valid special characters: `!@#$%^&*()_+=[{}]:,.<>/?\n"

}

if (!"".equals (result))

{

System.out.println ("Your password must:\n" + result)

}

else

{

System.out.println ("Your valid password is: " + password)

}

System.out.println ()

}

public static void main ( String[] args )

{

Scanner scanner = new Scanner (System.in)

while (true)

{

System.out.print ("Enter a valid password: ")

String password = scanner.nextLine ()

checkPass (password)

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12560929.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存