本文介绍了如何显示在JTextField中的掩模?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示在文本字段的面具。我该怎么办呢?是否有任何可重复使用的Java库这样做呢?我想创建一个文本字段,只允许进入八位数字(每个数字应该是0或1)

I want to display a mask in a text field. How can I do that? Is there any reusable java library to do that? I want to create a text field that only allow to enter eight digits (each digit should be either 0 or 1)

例如

推荐答案

这将创建一个蒙面的文本字段。当你preSS进入它将输出用户键入的内容。这使用JPasswordField中。

This will create a masked text field. When you press enter it will output what the user typed in. This uses JPasswordField. http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JPasswordField.html

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

public class PasswordField1 extends JApplet implements ActionListener {
  /* Declaration */
  private JPasswordField Input;
  private JTextField Echo;
  private Container Panel;
  private LayoutManager Layout;

  public PasswordField1 () {
    /* Instantiation */
    Input = new JPasswordField ("", 20);
    Layout = new FlowLayout ();
    Panel = getContentPane ();

    /* Location */
    Panel.setLayout (Layout);
    Panel.add (Input);

    /* Configuration */
    Input.addActionListener (this);
  }

  public void actionPerformed (ActionEvent e) {
    char [] Chars;
    String Word;
    Chars = Input.getPassword();
    Word = new String(Chars);
    System.out.println("You Entered: " + Word);
  }
}

这篇关于如何显示在JTextField中的掩模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 04:19