codice:
public class Registrati extends JFrame {
private JPanel contentPane;
private JTextField NU;
private JPasswordField PW;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Registrati frame = new Registrati();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Registrati() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 312, 265);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNomeUtente = new JLabel("Nome Utente");
lblNomeUtente.setBounds(35, 32, 87, 24);
contentPane.add(lblNomeUtente);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(35, 69, 75, 24);
contentPane.add(lblPassword);
JLabel LB = new JLabel("");
LB.setBounds(12, 106, 270, 64);
contentPane.add(LB);
NU = new JTextField();
NU.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
LB.setText("");
}
});
NU.setBounds(134, 33, 116, 22);
contentPane.add(NU);
NU.setColumns(10);
PW = new JPasswordField();
PW.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
LB.setText("");
}
});
PW.setBounds(134, 70, 116, 22);
contentPane.add(PW);
JButton btnOk = new JButton("OK");
btnOk.addActionListener(new ActionListener() {
@SuppressWarnings({ "resource", "deprecation" })
public void actionPerformed(ActionEvent arg0) {
try {
PrintWriter txt = new PrintWriter(new FileWriter("FileUtenti.txt", true));
Scanner tast = new Scanner(new FileReader("FileUtenti.txt"));
String nomeUtente, password,riga,ele;
String[] dati;
boolean chiave=false;
nomeUtente=NU.getText();
password=PW.getText();
if(nomeUtente.equals("")){
chiave=true;
JOptionPane.showMessageDialog(null,"Compilare tutti i campi","Error",JOptionPane.ERROR_MESSAGE);
}
if(password.equals("")){
chiave=true;
JOptionPane.showMessageDialog(null,"Compilare tutti i campi","Error",JOptionPane.ERROR_MESSAGE);
}
ele=nomeUtente+"&&"+password+"&&";
System.out
.println( "intero= "+ele+" nome "+nomeUtente);
while (tast.hasNext()) {
riga = tast.nextLine();
System.out.println("riga: " + riga);
if(!riga.equals(null)){
dati=riga.split("&&");
System.out.println("setta i dati");
for (int i = 0; i < dati.length&&chiave!=true; i++) {
System.out.println("dati[i]: "+dati[i]);
if(dati[i].equals(nomeUtente)){
chiave=true;//errore nome esistente
JOptionPane.showMessageDialog(null,"Utente già presente","Error",JOptionPane.ERROR_MESSAGE);
System.out.println("chiave true");
}else{
chiave=false;
System.out.println("chiave false");
}
}
}
}
if(chiave==false){
txt.println(ele);
LB.setText("AGGIUNTO!");
}
txt.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnOk.setBounds(185, 180, 97, 25);
contentPane.add(btnOk);
JButton btnAnnulla = new JButton("Annulla");
btnAnnulla.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
}
});
btnAnnulla.setBounds(70, 180, 97, 25);
contentPane.add(btnAnnulla);
}
}