2012-04-03 208 views
2

我想在文本字段中顯示一個掩碼。我怎樣才能做到這一點?有沒有可重用的Java庫來做到這一點?我想創建一個僅允許輸入八位數的文本字段(每個數字應該爲0或1)如何在jtextfield中顯示掩碼?

例如,

enter image description here

+0

也許['JFormattedTextField'](http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html)? – trashgod 2012-04-03 14:34:01

+0

你也可以使用JPasswordField。看到這裏:http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JPasswordField.html – Zack 2012-04-03 14:35:32

+0

據我所知JFormattedTextFiled不顯示掩碼。不是嗎? – Sachindra 2012-04-03 14:40:37

回答

2

這將創建一個蒙面文本字段。當你按下輸入鍵時,它將輸出用戶輸入的內容。這使用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); 
    } 
} 
+0

非常感謝。但每個數字應該是0或1.我怎麼能實現這個目標? – Sachindra 2012-04-03 14:59:04

+0

面具應該是0s和1s? – Zack 2012-04-03 15:08:30

+0

是的它應該是0s和1s – Sachindra 2012-04-03 15:09:41