想用java实现在命令行下输入密码,密码以“*”显示,想了很久,在网上也找了很久就是没结果,请帮忙啊!我开发用的是jdk1.4的,所以jdk6.0上的方法就不要介绍了! 谁帮我解决了分还可以多给。
解决方案 »
- 排序问题
- 计算机组成原理 浮点数补码问题
- java使用代理访问网址
- 向大家询问下,学HTTP协议和Servlet/JSP什么书比较好?
- 关于反射的一个问题。
- java的各种排序方法..
- 常见人争论几个常用Collection类的效率问题,这次我做了个实验,欢迎进来讨论
- 正则表达式问题 纠结
- 何谓内部类?
- How to "executeQuery("select * from 系 where 系名='计算机系'");" In an Application!
- 日期对日期加减
- 再次换个说法问下吧。在eclipse里扩大内存能导出8k+的数据到EXCEL表中,打包成。JAR文件后则不行,请问解决方法!
http://211.147.225.32/gate/big5/java.chinaitlab.com/Swing/526545_2.html
这里有答案:http://topic.csdn.net/t/20050726/21/4169966.html
与基于AWT或者JSwing的图形用户界面程序相比,在基于命令行的Java程序中实现密码屏蔽输入要比较麻烦一些,原因在于JDK没有提供任何基于文本的字符回显控制方法,因此需要编写相应的控制代码。基于通用性方面的考虑,在本文中将编写用于屏蔽用户输入的对象InputMasking,该对象的定义如下:
//InputMasking.java
import java.io.*;
public class InputMasking
{
String getPassword(String initial) throws IOException
{
MaskingThread listeningthread = new MaskingThread(initial);
Thread thread_instance = new Thread(listeningthread);
String password = "";
thread_instance.start();
while (true)
{
char input = (char)System.in.read();
listeningthread.stopMasking();
if (input == '\r')
{
input = (char)System.in.read();
if (input == '\n')
break;
else
continue;
}
else if(input == '\n')
break;
else
password += input;
}
return password;
}
}
该对象在后台启动线程的控制下,从系统输入设备中读取字符并对该字符进行分析。如果遇到行结束标志,则返回该线程获取的字符串对象password。读者一定会关心后台线程对象MaskingThread的作用,该线程对象周期地刷新终端窗口,其目的在于屏蔽用户输入的字符,使该字符不能够在窗口中显示出来。该线程对象的定义为:
//MaskingThread.java
import java.io.*;
class MaskingThread extends Thread
{
private boolean stop = false;
private int index;
private String initial;
public MaskingThread(String initial)
{
this.initial = initial;
}
public void run()
{
while(!stop)
{
try
{
this.sleep(1);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
if (!stop)
{
System.out.print("\r" + initial + " \r" + initial);
}
System.out.flush();
}
}
public void stopMasking()
{
this.stop = true;
}
}
在InputMasking对象和MaskingThread对象的配合下,使得基于命令行的Java应用程序能够实现用户输入密码字符的屏蔽,其核心方法是利用后台线程时时刷新终端窗口,屏蔽用户输入字符。下面的CmdLineUtility对象即利用前面定义的两个对象进行用户输入屏蔽,请读者实际运行上述程序,以了解命令行Java程序屏蔽输入的方式:
//CmdLineUtility.java
import java.io.*;
public class CmdLineUtility
{
public static void main(String argv[])
{
InputMasking masking = new InputMasking();
String password = null;
try
{
password = masking.getPassword("请输入登录密码: ");
}
catch(IOException ex)
{
ex.printStackTrace();
}
System.out.println("您输入的密码为: " + password);
}
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include <stdio.h >
#include <conio.h >
/* Header for class test_InputMasking */#ifndef _Included_test_InputMasking
#define _Included_test_InputMasking
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: test_InputMasking
* Method: getPassword
* Signature: (I)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_test_InputMasking_getPassword
(JNIEnv * env, jobject o, jint maxlength){
char pas[1024]={0},i; for(i=0;i<maxlength;i++)
{
pas[i] = getch();
if(pas[i]==13){break;}
putchar( '*');
}
return (env)->NewStringUTF(pas);
}#ifdef __cplusplus
}
#endif
#endif
java代码package test;/**
*
* @author test
* @version 1.0.0
* @2007-10-18 9:22:33
*/
public class InputMasking {
{
System.loadLibrary("test");
}
public native String getPassword(int maxlength);
}调用时直接
String password=new InputMasking().getPassword(10);
就可以了
#include <jni.h >
#include <stdio.h >
#include <conio.h >
/* Header for class test_InputMasking */ #ifndef _Included_test_InputMasking
#define _Included_test_InputMasking
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: test_InputMasking
* Method: getPassword
* Signature: (I)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_test_InputMasking_getPassword
(JNIEnv * env, jobject o, jint maxlength){
char pas[1024]={0},i; for(i=0;i <maxlength;i++)
{
pas[i] = getch();
if(pas[i]==13){break;}
putchar( '* '); }
return (env)- >NewStringUTF(pas);
} #ifdef __cplusplus
}
#endif
#endif
java代码 package test; /**
*
* @author test
* @version 1.0.0
* @2007-10-18 9:22:33
*/
public class InputMasking {
{
System.loadLibrary("test");
}
public native String getPassword(int maxlength);
} 调用时直接
String password=new InputMasking().getPassword(10);
就可以了
import javax.swing.JPanel;
import javax.swing.JPasswordField;/**
* @author haihai
*
*/
public class TestMain extends JFrame{
JPanel p=null;
JPasswordField pf=null;
public TestMain() {
super("密码屏蔽");
p=new JPanel();
pf=new JPasswordField(15); //密码输入框〈〈〈〈
pf.setEchoChar('●'); //掩码〈〈〈〈〈
p.add(pf);
this.getContentPane().add(p);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 300);
this.setVisible(true);
}
public static void main (String[] args) {
new TestMain();
}
}
是不楼主要的
工程需要,只能在JDK1.4下开发。
AntiRSI 的方法我已经试过了,确实不行,F1,F3,F7等键没有屏蔽掉,上次输入的密码会被记录。