2014-03-25 34 views
-1

我有一個完全在Eclipse中工作的小程序。我可以讓用戶輸入一個pin。有用。但是現在我想進入下一步並掩飾他輸入的內容。當他進入它時,我需要隱藏這個針。我想在他寫作時可以在JFormattedTextField中顯示星號(「*」),對嗎?我怎樣才能做到這一點 ?如何屏蔽帶有星號的JFormattedTextField的輸入?

這裏是我的代碼:

package codePin; 

import java.io.*; 
import java.text.NumberFormat; 
import java.util.*; 
import java.util.concurrent.atomic.AtomicInteger; 

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 

public class Main extends JFrame { 

    private static final long serialVersionUID = 1L; 

    private JPanel container = new JPanel(); 
    private JFormattedTextField jtf = new JFormattedTextField(NumberFormat.getIntegerInstance()); 
    private JLabel label = new JLabel("Enter Pin: "); 
    private JButton b = new JButton("OK"); 


    public Main() { 
     this.setTitle("NEEDS"); 
     this.setSize(300, 500); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLocationRelativeTo(null); 

     container.setBackground(Color.white); 
     container.setLayout(new BorderLayout()); 
     JPanel top = new JPanel(); 
     Font police = new Font("Arial", Font.BOLD, 14); 
     jtf.setFont(police); 
     jtf.setPreferredSize(new Dimension(100, 30)); 
     jtf.setForeground(Color.BLUE); 

     b.addActionListener(new BoutonListener()); 

     top.add(label); 
     top.add(jtf); 
     top.add(b); 


     this.setContentPane(top); 
     this.setVisible(true); 
    } 

    class BoutonListener implements ActionListener { 
     private final AtomicInteger nbTry = new AtomicInteger(0); 

     public void actionPerformed(ActionEvent e) { 
      if (nbTry.get() > 2) { 
       JOptionPane.showMessageDialog(null, "Pin blocked due to 3 wrong tries"); 
       return; 
      } 
      if (jtf.getText().replaceAll("\u00A0", "").length() != 4) { 
       //System.out.println("Pin must be 4 digits"); 
       JOptionPane.showMessageDialog(null, "Ping must be 4 digits"); 
       return; 
      } 
      System.out.println("Checking..."); 
      SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { 
       @Override 
       protected Void doInBackground() throws Exception { 
        boolean authenticated = false; 
        ArrayList<Integer> pins = new ArrayList<Integer>(); 
        readPinsData(new File("bdd.txt"), pins); 
        String[] thePins = new String[pins.size()]; 
        for (int i = 0; i < thePins.length; i++) { 
         thePins[i] = pins.get(i).toString(); 
        } 
        String passEntered = String.valueOf(jtf); 
        for (String thePin : thePins) { 
         if (passEntered.equals(thePin) && jtf.getText().length() == 4) { 
          System.out.println(":)"); 
          authenticated = true; 
          break; 
         } 
        } 
        if (!authenticated) { 
         System.out.println(":("); 
         nbTry.incrementAndGet(); 
        } 
        return null; 
       } 
      }; 
      worker.execute(); 
     } 


    } 

    // Read/Access pins bdd.txt file 
    static public boolean readPinsData(File dataFile, ArrayList<Integer> data) { 
     boolean err = false; 
     try { 
      Scanner scanner = new Scanner(dataFile); 
      String line; 
      while (scanner.hasNext()) { 
       line = scanner.nextLine(); 
       try { 
        data.add(Integer.parseInt(line)); 
       } catch (NumberFormatException e) { 
        e.printStackTrace(); 
        err = true; 
       } 
      } 
      scanner.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      err = true; 
     } 

     return err; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Main(); 
      } 
     }); 

    } 
} 

任何想法?謝謝。

Florent。

+4

如何使用['JPasswordField'(http://docs.oracle.com/javase/7/docs/api/javax/swing/JPasswordField.html) ? – Astrobleme

+0

是的,我在谷歌上找到它時發現了這個問題,但事實證明我無法弄清楚如何使用我的代碼進行這項工作。 – hacks4life

+0

@FlorentP你對JPasswordField有什麼問題? – Puce

回答

3

嘗試JPasswordField中像下面,

JPasswordField p1=new JPasswordField("pass",6); 
p1.setEchoChar('*'); 
top.add(p1); 
+0

我應該在哪裏應該把這個新的P1? – hacks4life

+1

刪除JFormattedTextField代碼,而是添加這一個... – Prakash

+0

完美的工作。謝謝 – hacks4life