我是Java的新手,我想询问如何屏蔽/隐藏用户的密码输入。我希望程序每次用户输入任何内容时都会显示一个星号(*),以便隐藏密码。我的密码程序单独(没有屏蔽)工作。用户将输入密码,之后只有3次尝试。我想屏蔽用户用星号键入的内容。
我在互联网上研究了如何使用遮蔽但我不明白。我的节目未能掩盖多次。我真的很喜欢Java,如果你们有人会使用我的一些其他代码。请尽可能解释。感谢。
这是我的第一个代码: import java.io. *; import javax.swing.JPasswordField;
public class Capture_Password {
public static void main(String[] args) {
BufferedReader loopin=new BufferedReader (new InputStreamReader (System.in));
JPasswordField passwordFld = new JPasswordField();
final double pinno=12345;
double ca;
String attempt;
try
{
System.out.println("Please enter the 5-digit PIN code:");
attempt=loopin.readLine();
ca=Double.parseDouble(attempt);
if(ca==pinno)
{
System.out.println("Congratulations! You have entered the correct PIN code!");
}
else
{
for(int tryleft=3; tryleft>=0 && ca!=pinno; tryleft--)
{
System.out.println("Sorry, you have entered the wrong PIN code");
System.out.println("You have " + tryleft + " try/tries left");
System.out.println();
if(tryleft==0)
{
System.out.println("That was your last attempt, goodbye!");
}
System.out.println();
if(tryleft>0)
{
System.out.println("Please enter the 5-digit PIN code:");
attempt=loopin.readLine();
ca=Double.parseDouble(attempt);
}
if(ca==pinno)
{
System.out.println("Congratulations! You have entered the correct PIN code!");
}
}
}
}
catch(Exception e)
{
System.out.println("ERROR");
}
}}
这是另一个:
public class JPasswordField{
public String getEchoString(){
return "*";
}
}
我真的很感谢你的帮助,伙计们!
答案 0 :(得分:0)
import java.io.*;
import javax.swing.*;
import java.awt.*;
class Experiment
{
/*declaration */
JFrame frame;
JPanel panel;
JLabel label1, label2, label3;
JTextField tf1;
JPasswordField tf2;
JButton button;
String S1,S2,S3,S4;
/*declaration ended */
Experiment()
{
/*memory initialization */
frame = new JFrame("Eraser");
panel = new JPanel();
label1 = new JLabel("Sentence 1");
label2 = new JLabel("What to Erase");
label3 = new JLabel("");
tf1 = new JTextField(20);
tf2 = new JPasswordField(20);
button = new JButton("Erase");
/*memory initialization ended */
/*containment*/
frame.add(panel, BorderLayout.CENTER);
panel.add(label1);
panel.add(tf1);
panel.add(label2);
panel.add(tf2);
panel.add(label3);
panel.add(button);
/*containment ended*/
Toolkit tk = Toolkit.getDefaultToolkit();
int frame_width = ((int) tk.getScreenSize().getWidth());
int frame_height= ((int) tk.getScreenSize().getHeight());
/*button-configuration*/
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
/*close-button-configuration*/
/* open - frame configuration */
frame.setLocation(frame_width/2 - 125, frame_height/2 - 65);
frame.setSize(250,190);
frame.setResizable(false);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
/* close - frame configuration */
}
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
/*LOGIC*/
S1= tf1.getText();
S2= tf2.getText();
S3 = S1.replace(S2,"");
JOptionPane.showMessageDialog(null,S3);
/*LOGIC ENDED */
}
public static void main (String[]args)throws Exception
{
Experiment object = new Experiment();
}
}
###如果您坚持使用控制台,那么请阅读此http://docs.oracle.com/javase/7/docs/api/java/io/Console.html#readPassword%28%29您必须至少运行Java 6。 。这不适用于Eclipse控制台#####
答案 1 :(得分:0)
我不确定它是否可以在java程序的系统控制台中显示星号,但它绝对可以简单地禁用显示输出。
使用java中的Console对象,可以调用readPassword()方法,与大多数unix系统一样,当用户输入密码时,它只显示任何内容。
请考虑以下代码:
public class Capture_Password{
public static void main(String[] args){
final char[] pinno = "12345".toCharArray(); //For high-security applications, this should be replaced with a method that uses temporary mutable objects to store passwords.
char[] attempt;
Console c = System.console();
if(c != null){
attempt = c.readPassword();
if(Arrays.equals(attempt, pinno)){
System.out.println("Congratulations! You have entered the correct PIN code!");
}else{
for(int tryleft=3; tryleft>=0 && !Arrays.equals(attempt, pinno); tryleft--){
System.out.println("Sorry, you have entered the wrong PIN code");
System.out.println("You have " + tryleft + " try/tries left");
System.out.println();
if(tryleft==0){
System.out.println("That was your last attempt, goodbye!");
}
System.out.println();
if(tryleft>0){
System.out.println("Please enter the 5-digit PIN code:");
attempt = c.readPassword();
}
if(Arrays.equals(attempt, pinno)){
System.out.println("Congratulations! You have entered the correct PIN code!");
}
}
}
Arrays.fill(attempt, '0'); //Security measure, blanking the attempt
}else{
System.err.println("Error: No console");
}
}
}
请注意,由于IDE编译和运行程序的方式,此代码可能无法在诸如Eclipse之类的IDE中运行(打印出'错误:无控制台')。要准确测试基于控制台的程序,请在实际的系统控制台(命令提示符,终端等)中编译并运行它。