/**
* Autore :Alenadri Enrico
* Classe : III G
* Data : 5/02/2009
* Nome File: rubrica.java
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class rubrica{
public static void main
(String[] args
)
{
int xLoc=400, //posizione x dei frame nello schermo
yLoc=400; //posizione y dei frame nello schermo
framePri frame = new framePri(xLoc,yLoc);
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
frame.show();
}
}
{
private boolean exist=true;
private int x=0,
num=0,
linee=0;
linea;
inCognome,
inNumero,
inIndirizzo,
inNameRub;
testoStp,
nome,
Congome,
Numero,
Indirizzo,
index,
spazio,
stmNome,
stmCognome,
stmNumero,
stmIndirizzo;
reset,
savExit,
invio,
invio_,
indIns,
indStp,
avanti,
back,
indietro,
exit;
public panelInserimento()
{
//Componenti Index
index
= new JLabel(" Cosa vuoi fare? ");
indIns
= new JButton("Crea rubrica");
indStp
= new JButton("Visualizza contatti");
//Componenti inserimento nome Rubrica-inserimento scherda/stampa scheda
testoIns
= new JLabel("Inserisci il nome con cui salvare la rubrica ");
testoStp
= new JLabel("Inserisci il nome della rubrica ");
//Componenti inserimento scheda
Congome
= new JLabel(" Cognome");
Numero
= new JLabel(" Numero Casa");
Indirizzo
= new JLabel(" Indirizzo");
savExit
= new JButton("Save & Back");
//Componenti stampa Scheda
stmNome
= new JLabel("Nome:");
stmCognome
= new JLabel("Cognome:");
stmNumero
= new JLabel("Numero:");
stmIndirizzo
= new JLabel("Indirizzo");
add(stmNome);
add(stmCognome);
add(stmNumero);
add(stmIndirizzo);
add(indietro);
add(back);
add(avanti);
indietro.setEnabled(false);
avanti.addActionListener(this);
back.addActionListener(this);
indietro.addActionListener(this);
nameRub = inNameRub.getText();
add(index);
add(indIns);
add(indStp);
add(exit);
add(spazio);
indIns.addActionListener(this);
indStp.addActionListener(this);
exit.addActionListener(this);
add(testoStp);
add(testoIns);
add(inNameRub);
add(invio);
add(invio_);
invio.addActionListener(this);
invio_.addActionListener(this);
add(nome);
add(inNome);
add(Congome);
add(inCognome);
add(Numero);
add(inNumero);
add(Indirizzo);
add(inIndirizzo);
add(ok);
add(reset);
add(savExit);
pannelloInput(false);
pannelloOutput(false);
pannelloRubIns(false);
ok.addActionListener(this);
reset.addActionListener(this);
savExit.addActionListener(this);
}
{
String nome
= inNome.
getText();
String cognome
= inCognome.
getText();
String numero
= inNumero.
getText();
String indirizzo
= inIndirizzo.
getText();
String nameRub
= inNameRub.
getText();
boolean test;
String command
= evt.
getActionCommand();
command = command.toLowerCase();
if(command.equals("<<"))
{
if(num>0)
{
num--;
avanti.setEnabled(true);
}
if(num==0)
indietro.setEnabled(false);
letturaFile(nameRub);
stampa();
}
if(command.equals(">>"))
{
if((num+1)*4<linee)
{
num++;
indietro.setEnabled(true);
}
if((num+1)*4==linee)
avanti.setEnabled(false);
letturaFile(nameRub);
stampa();
}
if(command.equals("back"))
{
num=0;
pannelloInput(false);
pannelloRubIns(false);
pannelloRubStp(false);
pannelloIndex(true);
pannelloOutput(false);
}
if(command.equals("add"))
{
try{
scrittura.println(nome);
scrittura.println(cognome);
scrittura.println(numero);
scrittura.println(indirizzo);
{
System.
out.
println("File insesistente... ");
}
reset();
}
if(command.equals("invio"))
{
test=true;
if(nameRub.equals(""))
test=false;
exFile
= new File(nameRub
);
if( ((exFile).exists()) || (test==false))
{
inNameRub.setText("");
if(test==false)
testoIns.setText("Nome File non corretto.Riprova ");
else
testoIns.setText("File già esistente. Riprova ");
}
else
{
pannelloRubIns(false);
pannelloInput(true);
try{
{
System.
out.
println("Errore in lettura File...");
}
}
}
if(command.equals("invio "))
{
test=true;
if(nameRub.equals(""))
test=false;
exFile
= new File(nameRub
);
if( ((exFile).exists()) && (test==true))
{
letturaFile(nameRub);
stampa();
pannelloRubStp(false);
pannelloOutput(true);
}
else
{
inNameRub.setText("");
if(test==false)
testoStp.setText("Nome File non corretto.Riprova! ");
else
testoStp.setText("File non esistente. Riprova! ");
}
}
if(command.equals("reset"))
{
reset();
}
if(command.equals("save & back"))
{
pannelloInput(false);
pannelloRubIns(false);
pannelloRubStp(false);
pannelloIndex(true);
pannelloOutput(false);
}
if(command.equals("esci"))
{
}
if(command.equals("crea rubrica"))
{
pannelloRubIns(true);
pannelloIndex(false);
}
if(command.equals("visualizza contatti"))
{
pannelloRubStp(true);
pannelloIndex(false);
}
}
void letturaFile
(String nomeFile
)
{
int cont=0;
linee=0;
try{
while( (linea = lettura.readLine()) != null )
{
linee++;
}
while( cont<num*4 )
{
cont++;
linea = lettura.readLine();
}
{
System.
out.
println("Errore in lettura file, Nessun file trovato!");
}
{
System.
out.
println("Errore in lettura file..");
}
}
void stampa()
{
try{
linea = lettura.readLine();
stmNome.setText(" Nome : "+linea+" ");
linea = lettura.readLine();
stmCognome.setText(" Cognome : "+linea+" ");
linea = lettura.readLine();
stmNumero.setText(" Numero : "+linea+" ");
linea = lettura.readLine();
stmIndirizzo.setText(" Indirizzo : "+linea+" ");
{
System.
out.
println("Errore in stampa Dati File.....");
{
System.
out.
println("Nessun file ricevuto...");
}
}
void reset()
{
inNome.setText("");
inCognome.setText("");
inNumero.setText("");
inIndirizzo.setText("");
}
void pannelloInput(boolean att)
{
nome.setVisible(att);
inNome.setVisible(att);
Congome.setVisible(att);
inCognome.setVisible(att);
Numero.setVisible(att);
inNumero.setVisible(att);
Indirizzo.setVisible(att);
inIndirizzo.setVisible(att);
ok.setVisible(att);
reset.setVisible(att);
savExit.setVisible(att);
}
void pannelloOutput(boolean att)
{
stmNome.setVisible(att);
stmCognome.setVisible(att);
stmNumero.setVisible(att);
stmIndirizzo.setVisible(att);
avanti.setVisible(att);
back.setVisible(att);
indietro.setVisible(att);
}
void pannelloRubIns(boolean att)
{
testoStp.setVisible(false);
testoIns.setVisible(att);
inNameRub.setVisible(att);
invio.setVisible(att);
invio_.setVisible(false);
}
void pannelloRubStp(boolean att)
{
testoIns.setVisible(false);
testoStp.setVisible(att);
inNameRub.setVisible(att);
invio.setVisible(false);
invio_.setVisible(att);
}
void pannelloIndex(boolean att)
{
index.setVisible(att);
indIns.setVisible(att);
indStp.setVisible(att);
exit.setVisible(att);
spazio.setVisible(att);
}
}
{
public framePri(int xLoc,int yLoc)
{
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
setTitle("Rubrica 1.0");
setResizable(false);
setLocation(xLoc,yLoc);
panelInserimento panelIns = new panelInserimento();
contentPane.add(panelIns);
}
public static final int DEFAULT_WIDTH =300 ;
public static final int DEFAULT_HEIGHT =200 ;
}