Java 使用JOptionPane隐藏数据
如何使用作业窗格的showInputDialog隐藏字符。例如:JOptionPane.showInputDialog(“输入您的姓名:”)Java 使用JOptionPane隐藏数据,java,swing,Java,Swing,如何使用作业窗格的showInputDialog隐藏字符。例如:JOptionPane.showInputDialog(“输入您的姓名:”) 我想在用户提供姓名时隐藏字符。我在申请中。如果我能隐藏这些角色,我的工作就完成了。我不想使用JPasswordField,因为它需要一个表单来保存该标签(JPasswordField)。您可以使用JPasswordField,默认情况下它将字符替换为“*” 您可能需要阅读以下内容: 如果您正在寻找JPasswordField的替代品,您可能需要阅读以下内
我想在用户提供姓名时隐藏字符。我在申请中。如果我能隐藏这些角色,我的工作就完成了。我不想使用JPasswordField,因为它需要一个表单来保存该标签(JPasswordField)。您可以使用JPasswordField,默认情况下它将字符替换为“*” 您可能需要阅读以下内容: 如果您正在寻找JPasswordField的替代品,您可能需要阅读以下内容:
这里有一个解决方案,它使用
JOptionPane
显示JPasswordField
,在用户键入时打印空白
实际上,您只想在代码中放入showPasswordPrompt()
方法,但代码中包含一个main()
方法,该方法允许您轻松测试对话框的外观
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
public class JOptionPaneTest
{
public static String showPasswordPrompt( Component parent, String title )
{
// create a new JPasswordField
JPasswordField passwordField = new JPasswordField( );
// display nothing as the user types
passwordField.setEchoChar( ' ' );
// set the width of the field to allow space for 20 characters
passwordField.setColumns( 20 );
int returnVal = JOptionPane.showConfirmDialog( parent, passwordField, title, JOptionPane.OK_CANCEL_OPTION );
if ( returnVal == JOptionPane.OK_OPTION )
{
// there's a reason getPassword() returns a char[], but we ignore this for now...
// see: http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords
return new String( passwordField.getPassword( ) );
}
else
{
return null;
}
}
public static void main( String[] args )
{
final JFrame frame = new JFrame( );
final JButton button = new JButton( "Push Me For Dialog Box" );
button.addActionListener( new ActionListener( )
{
@Override
public void actionPerformed( ActionEvent e )
{
String password = showPasswordPrompt( frame, "Enter Password:" );
button.setText( password );
}
} );
frame.add( button );
frame.setSize( 400, 400 );
frame.setVisible( true );
}
}
如果我使用JPasswordField,它会让我创建一个表单。我不想这样做,有没有办法使用JOPtion pane???@user10101
JOptionPane
可以显示组件
,所以你不需要创建JFrame
或JDialog
(我想这就是你所说的“表单”)为了使用密码字段。确切地说,JOptionPane可以显示组件,但我如何隐藏他在JOption pane中输入的数据???以便用户键入的字符不会出现在文本字段中?它只是保持空白?只是小心不要混淆用户。如果你开始打字,用户希望得到某种反馈。有一个不显示任何反馈的盒子可能会让人困惑。
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
public class JOptionPaneTest
{
public static String showPasswordPrompt( Component parent, String title )
{
// create a new JPasswordField
JPasswordField passwordField = new JPasswordField( );
// display nothing as the user types
passwordField.setEchoChar( ' ' );
// set the width of the field to allow space for 20 characters
passwordField.setColumns( 20 );
int returnVal = JOptionPane.showConfirmDialog( parent, passwordField, title, JOptionPane.OK_CANCEL_OPTION );
if ( returnVal == JOptionPane.OK_OPTION )
{
// there's a reason getPassword() returns a char[], but we ignore this for now...
// see: http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords
return new String( passwordField.getPassword( ) );
}
else
{
return null;
}
}
public static void main( String[] args )
{
final JFrame frame = new JFrame( );
final JButton button = new JButton( "Push Me For Dialog Box" );
button.addActionListener( new ActionListener( )
{
@Override
public void actionPerformed( ActionEvent e )
{
String password = showPasswordPrompt( frame, "Enter Password:" );
button.setText( password );
}
} );
frame.add( button );
frame.setSize( 400, 400 );
frame.setVisible( true );
}
}