mdweb Dio maturo


Registrato: 18/12/07 16:59 Messaggi: 4412
|
Inviato: 26 Feb 2008 20:54 Oggetto: JAVA proteggere con password |
|
|
In questo tutorial creiamo un'applicazione che presenta all'utente un modulo all'interno del quale inserire la password
Codice: |
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SecurityKey extends JPanel
implements ActionListener {
private static String pass = "Convalida";
private JFrame controllingFrame;
private JPasswordField campoPass;
public SecurityKey(JFrame f) {
controllingFrame = f;
campoPass = new JPasswordField(8);
campoPass.setActionCommand(pass);
campoPass.addActionListener(this);
JLabel label = new JLabel("Metti la password: ");
label.setLabelFor(campoPass);
JComponent buttonPane = createButtonPanel();
JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
textPane.add(label);
textPane.add(campoPass);
add(textPane);
add(buttonPane);
}
protected JComponent createButtonPanel() {
JPanel p = new JPanel(new GridLayout(0,1));
JButton keyButton = new JButton("Invia");
keyButton.setActionCommand(pass);
keyButton.addActionListener(this);
p.add(keyButton);
return p;
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (pass.equals(cmd)) {
char[] input = campoPass.getPassword();
if (controlloPass(input)) {
JOptionPane.showMessageDialog(controllingFrame,
"Password esatta.");
} else {
JOptionPane.showMessageDialog(controllingFrame,
"Password sbagliata.",
"Errore",
JOptionPane.ERROR_MESSAGE);
}
for (int j = 0; j < input.length; j++) {
input[j] = 0;
}
campoPass.selectAll();
resetFocus();
}
}
private static boolean controlloPass(char[] input) {
boolean Ctrl = true;
char[] Password = { 'j', 'a', 'v', 'a' };
if (input.length != Password.length) {
Ctrl = false;
} else {
for (int j = 0; j < input.length; j++) {
if (input[j] != Password[j]) {
Ctrl = false;
}
}
}
for (int j = 0; j < Password.length; j++) {
Password[j] = 0;
}
return Ctrl;
}
protected void resetFocus() {
campoPass.requestFocusInWindow();
}
private static void interfaccia() {
JFrame frame = new JFrame("SecurityKey");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final SecurityKey newContentPane = new SecurityKey(frame);
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
newContentPane.resetFocus();
}
});
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
interfaccia();
}
});
}
} |
la password è java e potete cambiarla qua
Codice: |
char[] Password = { 'j', 'a', 'v', 'a' }; |
Buona fortuna!  |
|