1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final JPasswordField field = new JPasswordField(15);
final JLabel label = new JLabel("");
label.setHorizontalAlignment(SwingConstants.LEFT);
final List<JButton> buttons = new ArrayList<JButton>(10);
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ if ( e.getSource() instanceof JButton )
{ int index = buttons.indexOf(e.getSource());
Document doc = field.getDocument();
try
{ doc.insertString(doc.getLength(), Integer.toString(index), null); }
catch (BadLocationException ex)
{ ex.printStackTrace(); }
}
}
};
/** pour montrer que ca alimente bien la zone de password */
field.getDocument().addDocumentListener(new DocumentListener()
{
public void changedUpdate(DocumentEvent e)
{ }
public void insertUpdate(DocumentEvent e)
{ this.updateLabel(e.getDocument()); }
public void removeUpdate(DocumentEvent e)
{ this.updateLabel(e.getDocument()); }
/** méthode permettant de mettre à jour le label en fonction du contenu du document */
private void updateLabel(Document document)
{
try
{ label.setText(document.getText(0, document.getLength())); }
catch (BadLocationException ex)
{ ex.printStackTrace(); }
}
});
JButton button0 = new JButton("0");
buttons.add(button0);
JButton button1 = new JButton("1");
buttons.add(button1);
JButton button2 = new JButton("2");
buttons.add(button2);
JButton button3 = new JButton("3");
buttons.add(button3);
JButton button4 = new JButton("4");
buttons.add(button4);
JButton button5 = new JButton("5");
buttons.add(button5);
JButton button6 = new JButton("6");
buttons.add(button6);
JButton button7 = new JButton("7");
buttons.add(button7);
JButton button8 = new JButton("8");
buttons.add(button8);
JButton button9 = new JButton("9");
buttons.add(button9);
JPanel panelButton = new JPanel(new GridLayout(4, 3));
panelButton.add(button7);
panelButton.add(button8);
panelButton.add(button9);
panelButton.add(button4);
panelButton.add(button5);
panelButton.add(button6);
panelButton.add(button1);
panelButton.add(button2);
panelButton.add(button3);
panelButton.add(button0);
for(int i = 0; i < buttons.size(); i++)
{ buttons.get(i).addActionListener(listener); }
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = gbc.HORIZONTAL;
panel.add(field, gbc);
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.gridx = 1;
gbc2.gridy = 2;
gbc2.fill = gbc.HORIZONTAL;
panel.add(label, gbc2);
GridBagConstraints gbc3 = new GridBagConstraints();
gbc3.gridx = 1;
gbc3.gridy = 3;
gbc3.fill = gbc.BOTH;
gbc3.weighty = 1.0f;
panel.add(panelButton, gbc3);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
} |
Partager