codice:
public class Inserisci extends Applet implements ActionListener, Serializable {
private Lista l=null;
private JButton salva,sfoglia;
private JTextField textnome,textmadre,textanno,textgiorno,textmese;
JTextArea textinfo;
private JComboBox combo;
private JFrame f;
private Image img;
Inserisci(Lista l){
this.l=l;
elementi();
}
public void elementi(){
//Frame
f=new JFrame();
f.setVisible(true);
f.setTitle("Nuovo cane");
f.setSize(500, 400);
f.setDefaultCloseOperation(1);
f.setLayout(null);
//Inserimento del nome
JLabel nome=new JLabel("Nome:");
nome.setBounds(10, 10, 120, 20);
textnome=new JTextField();
textnome.setBounds(60, 10, 110, 20);
//Inserimento Madre
JLabel madre=new JLabel("Figlio di:");
madre.setBounds(10, 40, 120, 20);
textmadre =new JTextField();
textmadre.setBounds(60, 40, 110, 20);
//Data di Nascita
JLabel nascita=new JLabel("Data Nascita:");
nascita.setBounds(10, 70, 120, 20);
textanno=new JTextField("aaaa");
textanno.setBounds(70, 90, 40, 20);
textmese=new JTextField("mm");
textmese.setBounds(35, 90, 27, 20);
textgiorno=new JTextField("gg");
textgiorno.setBounds(10, 90, 20, 20);
//pulsante salva
salva =new JButton("Salva");
salva.setBounds(20, 300, 80, 40);
salva.addActionListener(this);
//ComboBox maschio o femmina
String[] stringhe = {"Maschio", "Femmina"};
combo=new JComboBox(stringhe);
combo.setBounds(200, 20, 100, 20);
//Area di testo x info aggiuntive
JLabel info=new JLabel("Informazioni Aggiuntive");
info.setBounds(10, 110, 150, 20);
textinfo =new JTextArea();
textinfo.setBounds(10, 130, 200, 150);
textinfo.setLineWrap(true);
textinfo.setWrapStyleWord(true);
//pulsante sfoglia
sfoglia =new JButton("inserisci Foto");
sfoglia.setBounds(350, 20, 120, 20);
sfoglia.addActionListener(this);
//aggiunta elementi al frame
f.add(nome);
f.add(textnome);
f.add(nascita);
f.add(textanno);
f.add(textmese);
f.add(textgiorno);
f.add(madre);
f.add(textmadre);
f.add(salva);
f.add(sfoglia);
f.add(combo);
f.add(info);
f.add(textinfo);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(salva)){
String nome=textnome.getText();
String madre=textmadre.getText();
String ann=textanno.getText();
String mes=textmese.getText();
String giorn=textgiorno.getText();
String inf=textinfo.getText();
int anno=0,mese=0,giorno=0;
String se=combo.getSelectedItem().toString();
try{
anno = Integer.parseInt(ann);
mese = Integer.parseInt(mes);
giorno = Integer.parseInt(giorn);
f.dispose();//chiude la finestra
Cane c=new Cane(nome,madre,anno,mese,giorno,inf,se);
l.aggiungi(c);
}
catch(Exception q){
JOptionPane.showMessageDialog(null, "Anno, Mese e giorno devono essere numeri.");
}
}
if(e.getSource().equals(sfoglia)){
JFileChooser chooser=new JFileChooser();
int risultato = chooser.showOpenDialog (null);
if(risultato==JFileChooser.APPROVE_OPTION){
String x=chooser.getSelectedFile().getName();
img = getImage(getDocumentBase(),x); il primo errore a runtime lo segna in questa righa
}
}
}
@Override
public void paint(Graphics g) {
g.drawImage(img, 220, 220, this);
f.repaint();
}
}