Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Aiuto in JAVA

    Salve raga

    Ho passato una giornata intera a fare Java VVoVe: ed a compilare questo programma: ora non riesco a capire cosa non va...
    Mi date una mano? In pratica quando clicco sul pulsante "prenota" si inceppa e non mi compare la nuova finestra

    Scusate la "grezzagine" nell'esposizione del problema, mo ho sonno ed un terribile mal di testa...

    Grazie per l'aiuto

    codice:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    
    public class AddressBook extends JFrame
    {     
    private JPanel labelPanel;
    private JPanel label2Panel;
    private JPanel label3Panel;
    private JPanel inputOutputPanel;
    private JPanel inputOutput3Panel;
    private JPanel displayPanel;
    private JPanel display2Panel;
    private JPanel display3Panel;
    private JPanel buttonPanel;
    private JPanel button2Panel;
    private JPanel button3Panel;
    private JPanel backPanel;
    private JPanel back2Panel;
    private JPanel back3Panel;
    private JTextField nomeInput;
    private JTextField cognomeInput;
    private JTextField addressInput;
    private JTextField patenteInput;
    private JTextField luogoInput;
    private JTextField nazioneInput;
    private JTextField annoInput;
    private JTextField modello;
    private JTextField casa;
    private JTextField immat;
    private JTextField periodo;
    private JButton fattoButton;
    private JButton prenotaButton;
    private JButton okButton;
    private JButton prelevaButton;
    private JButton estendiButton;
    private JButton aggButton;
    private JButton consegnaButton;
    private String s1, s2, s3, s4, s5, s6, s7, output="";
    private String a1, a2, a3, a4, output2="";
    private int s5int, a4int;
    
    public AddressBook()
    {     
         super ("Effettuare le scelte");
         displayPanel = new JPanel();
         displayPanel.setLayout(new BorderLayout());
         prenotaButton = new JButton("Prenota");
         buttonPanel = new JPanel();
         buttonPanel.add(prenotaButton);
         consegnaButton = new JButton("Consegna");
         buttonPanel.add(consegnaButton);
         prelevaButton = new JButton("Preleva");
         buttonPanel.add(prelevaButton);
         estendiButton = new JButton("Estendi");
         buttonPanel.add(estendiButton);
         aggButton = new JButton("Aggiorna");
         buttonPanel.add(aggButton);
         backPanel = new JPanel();
         backPanel.setLayout(new BorderLayout());
         backPanel.add(displayPanel,BorderLayout.CENTER);
         backPanel.add(buttonPanel,BorderLayout.CENTER);
         getContentPane().add(backPanel);
         
         prenotaButton.addActionListener(new ActionListener()
         {
              public void actionPerformed(ActionEvent e)
                   {
                        prenota();
                   }
         }
         );
              
         prelevaButton.addActionListener(new ActionListener ()
         {
         	 public void actionPerformed(ActionEvent e)
         	       {
         	       	    System.exit(0);
         	       }
         }
         );
         
         aggButton.addActionListener(new ActionListener ()
         {
         	 public void actionPerformed(ActionEvent e)
         	       {
         	       	    System.exit(0);
         	       }
         }
         );
         
         estendiButton.addActionListener(new ActionListener ()
         {
         	 public void actionPerformed(ActionEvent e)
         	       {
         	       	    System.exit(0);
         	       }
         }
         );
         
         consegnaButton.addActionListener(new ActionListener()
         {
              public void actionPerformed(ActionEvent e)
                   {
                        System.exit(0);
                   }
         }
         );
         
         addWindowListener(new WindowAdapter()
         {
              public void windowClosing(WindowEvent e)
              {
                   System.exit(0);
              }
         }
         );
         pack();
         setResizable(false);
         setVisible(true);
         setSize(500,250);
         show();
    }
    
    public void getText()
    {
         s1 = nomeInput.getText();
         output=output + "Nome: " + s1 + "\n";
         s2 = cognomeInput.getText();
         output=output + "Cognome: " + s2 + "\n";
         s3 = addressInput.getText();
         output=output + "Indirizzo: " + s3 + "\n";
         s4 = patenteInput.getText();
         output=output + "N° patente : " + s4 + "\n";
         s5 = annoInput.getText();
         output=output + "Anno di nascita: " + s5 + "\n";
         s5int=Integer.parseInt(s5);
         if ((2004-s5int)<18)
              JOptionPane.showMessageDialog(
                     null, "Scorza, 6 ancora un poppante! Ciucciati il calzino e torna al tuo biberon!", "Messaggio", JOptionPane.INFORMATION_MESSAGE);
              else auto();
         s6 = nazioneInput.getText();
         output=output + "Nazionalità: " + s6 + "\n";
         s7 = luogoInput.getText();
         output=output + "Luogo di nascita: " + s7 + "\n";
    }     
    
    private void getText2()
    {
         a1 = casa.getText();
         output2=output2 + "Casa costruttrice: " + a1 + "\n";
         a2 = modello.getText();
         output2=output2 + "Modello: " + a2 + "\n";
         a3 = immat.getText();
         output2=output2 + "Anno di immatricolazione: " + a3 + "\n";
         a4 = periodo.getText();
         a4int=Integer.parseInt(a4);
         if (a4int > 30)
             do {
             a4=JOptionPane.showInputDialog("Non è possile fittare auto per un periodo maggiore di 30 giorni: reimmettere il numero di giorni di affitto");
             a4int=Integer.parseInt(a4);
                        }
             while (a4int > 30);
         output2=output2 + "Periodo di affitto: " + a4 + "\n";
    }
    
    public void prenota()
    {
         display2Panel = new JPanel();
         display2Panel.setLayout(new BorderLayout());
         label2Panel = new JPanel();
         label2Panel.setLayout(new GridLayout (7,1));
         label2Panel.add(new JLabel ("Nome: "));
         label2Panel.add(new JLabel ("Cognome: "));
         label2Panel.add(new JLabel ("Indirizzo: "));
         label2Panel.add(new JLabel ("N. patente: "));
         label2Panel.add(new JLabel ("Anno di nascita: "));
         label2Panel.add(new JLabel ("Nazionalità: "));
         label2Panel.add(new JLabel ("Luogo di nascita: "));
         inputOutputPanel = new JPanel();
         inputOutputPanel.setLayout(new GridLayout (7,1));
         nomeInput = new JTextField (20);
         inputOutputPanel.add(nomeInput);
         cognomeInput = new JTextField (20);
         inputOutputPanel.add(cognomeInput);
         addressInput = new JTextField (20);
         inputOutputPanel.add(addressInput);
         patenteInput = new JTextField (20);
         inputOutputPanel.add(patenteInput);
         annoInput = new JTextField (4);
         inputOutputPanel.add(annoInput);
         nazioneInput = new JTextField (20);
         inputOutputPanel.add(nazioneInput);
         luogoInput = new JTextField (20);
         inputOutputPanel.add(luogoInput);
         display2Panel = new JPanel();
         display2Panel.setLayout(new BorderLayout());
         display2Panel.add(label2Panel,BorderLayout.WEST);
         display2Panel.add(inputOutputPanel,BorderLayout.CENTER);
         okButton = new JButton("Ok");
         button2Panel = new JPanel();
         button2Panel.add(okButton);
         back2Panel = new JPanel();
         back2Panel.setLayout(new BorderLayout());
         getContentPane().removeAll();
         back2Panel.add(display2Panel,BorderLayout.CENTER);
         back2Panel.add(button2Panel,BorderLayout.SOUTH);
         getContentPane().add(back2Panel);
         System.exit(0);
         
         okButton.addActionListener(new ActionListener()
         {
              public void actionPerformed(ActionEvent e)
                   {
                        getText();
                        JOptionPane.showMessageDialog(
          		                null, output, "Dati immessi", JOptionPane.INFORMATION_MESSAGE);  
                        System.exit(0);
                   }
         }
         );
    
    }
    
    public void auto()
    {
         display3Panel = new JPanel();
         display3Panel.setLayout(new BorderLayout());
         label3Panel = new JPanel();
         label3Panel.setLayout(new GridLayout (4,1));
         label3Panel.add(new JLabel ("Casa costruttrice: "));
         label3Panel.add(new JLabel ("Modello: "));
         label3Panel.add(new JLabel ("Anno di immatricolazione: "));
         label3Panel.add(new JLabel ("Periodo di affitto: "));
         inputOutput3Panel = new JPanel();
         inputOutput3Panel.setLayout(new GridLayout (4,1));
         casa = new JTextField (20);
         inputOutput3Panel.add(casa);
         modello = new JTextField (20);
         inputOutput3Panel.add(modello);
         immat = new JTextField (20);
         inputOutput3Panel.add(immat);
         periodo = new JTextField (20);
         inputOutput3Panel.add(periodo);
         display3Panel = new JPanel();
         display3Panel.setLayout(new BorderLayout());
         display3Panel.add(label3Panel,BorderLayout.WEST);
         display3Panel.add(inputOutput3Panel,BorderLayout.CENTER);
         fattoButton = new JButton("Ok");
         button3Panel = new JPanel();
         button3Panel.add(fattoButton);
         back3Panel.setLayout(new BorderLayout());
         getContentPane().removeAll();
         back3Panel.add(display3Panel,BorderLayout.CENTER);
         back3Panel.add(button3Panel,BorderLayout.SOUTH);
         getContentPane().add(back3Panel);
         
         fattoButton.addActionListener(new ActionListener()
         {
              public void actionPerformed(ActionEvent e)
                   {
                        getText2();
                        JOptionPane.showMessageDialog(
          		                null, output2, "Dati auto da affittare", JOptionPane.INFORMATION_MESSAGE);  
                        System.exit(0);
                   }
         }
         );
    }
    
    public static void main(String[] args)
    {
          AddressBook addressbook = new AddressBook();
          
    }}

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    1,965
    Prova a fare così, poiche' non è un bel modo di usare gli eventi

    codice:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    
    public class AddressBook extends JFrame
    {     
    private JPanel labelPanel;
    private JPanel label2Panel;
    private JPanel label3Panel;
    private JPanel inputOutputPanel;
    private JPanel inputOutput3Panel;
    private JPanel displayPanel;
    private JPanel display2Panel;
    private JPanel display3Panel;
    private JPanel buttonPanel;
    private JPanel button2Panel;
    private JPanel button3Panel;
    private JPanel backPanel;
    private JPanel back2Panel;
    private JPanel back3Panel;
    private JTextField nomeInput;
    private JTextField cognomeInput;
    private JTextField addressInput;
    private JTextField patenteInput;
    private JTextField luogoInput;
    private JTextField nazioneInput;
    private JTextField annoInput;
    private JTextField modello;
    private JTextField casa;
    private JTextField immat;
    private JTextField periodo;
    private JButton fattoButton;
    private JButton prenotaButton;
    private JButton okButton;
    private JButton prelevaButton;
    private JButton estendiButton;
    private JButton aggButton;
    private JButton consegnaButton;
    private String s1, s2, s3, s4, s5, s6, s7, output="";
    private String a1, a2, a3, a4, output2="";
    private int s5int, a4int;
    
    public AddressBook()
    {     
         super ("Effettuare le scelte");
         displayPanel = new JPanel();
         displayPanel.setLayout(new BorderLayout());
         prenotaButton = new JButton("Prenota");
         buttonPanel = new JPanel();
         buttonPanel.add(prenotaButton);
         consegnaButton = new JButton("Consegna");
         buttonPanel.add(consegnaButton);
         prelevaButton = new JButton("Preleva");
         buttonPanel.add(prelevaButton);
         estendiButton = new JButton("Estendi");
         buttonPanel.add(estendiButton);
         aggButton = new JButton("Aggiorna");
         buttonPanel.add(aggButton);
         backPanel = new JPanel();
         backPanel.setLayout(new BorderLayout());
         backPanel.add(displayPanel,BorderLayout.CENTER);
         backPanel.add(buttonPanel,BorderLayout.CENTER);
         getContentPane().add(backPanel);
         
         prenotaButton.addActionListener(this);
         prelevaButton.addActionListener(this);
         aggButton.addActionListener(this);
         estendiButton.addActionLister(this);
         consegnaButton.addActionListener(this);
         
         addWindowListener(new WindowAdapter()
         {
              public void windowClosing(WindowEvent e)
              {
                   System.exit(0);
              }
         }
         );
         pack();
         setResizable(false);
         setVisible(true);
         setSize(500,250);
         show();
    }
    
    public void actionPerformed(ActionEvent ae){
    Object source = ae.getSource();
    switch(source){
     case prelevaButton:
        preleva(); break;
     case prenotaButton:
        System.exit(0); break;
     case aggButton:
        System.exit(0); break;
     case estendiButton:
        System.exit(0); break;
     case consegnaButton:
        System.exit(0); break;
    }
    }
    
    public void getText()
    {
         s1 = nomeInput.getText();
         output=output + "Nome: " + s1 + "\n";
         s2 = cognomeInput.getText();
         output=output + "Cognome: " + s2 + "\n";
         s3 = addressInput.getText();
         output=output + "Indirizzo: " + s3 + "\n";
         s4 = patenteInput.getText();
         output=output + "N° patente : " + s4 + "\n";
         s5 = annoInput.getText();
         output=output + "Anno di nascita: " + s5 + "\n";
         s5int=Integer.parseInt(s5);
         if ((2004-s5int)<18)
              JOptionPane.showMessageDialog(
                     null, "Scorza, 6 ancora un poppante! Ciucciati il calzino e torna al tuo biberon!", "Messaggio", JOptionPane.INFORMATION_MESSAGE);
              else auto();
         s6 = nazioneInput.getText();
         output=output + "Nazionalità: " + s6 + "\n";
         s7 = luogoInput.getText();
         output=output + "Luogo di nascita: " + s7 + "\n";
    }     
    
    private void getText2()
    {
         a1 = casa.getText();
         output2=output2 + "Casa costruttrice: " + a1 + "\n";
         a2 = modello.getText();
         output2=output2 + "Modello: " + a2 + "\n";
         a3 = immat.getText();
         output2=output2 + "Anno di immatricolazione: " + a3 + "\n";
         a4 = periodo.getText();
         a4int=Integer.parseInt(a4);
         if (a4int > 30)
             do {
             a4=JOptionPane.showInputDialog("Non è possile fittare auto per un periodo maggiore di 30 giorni: reimmettere il numero di giorni di affitto");
             a4int=Integer.parseInt(a4);
                        }
             while (a4int > 30);
         output2=output2 + "Periodo di affitto: " + a4 + "\n";
    }
    
    public void prenota()
    {
         display2Panel = new JPanel();
         display2Panel.setLayout(new BorderLayout());
         label2Panel = new JPanel();
         label2Panel.setLayout(new GridLayout (7,1));
         label2Panel.add(new JLabel ("Nome: "));
         label2Panel.add(new JLabel ("Cognome: "));
         label2Panel.add(new JLabel ("Indirizzo: "));
         label2Panel.add(new JLabel ("N. patente: "));
         label2Panel.add(new JLabel ("Anno di nascita: "));
         label2Panel.add(new JLabel ("Nazionalità: "));
         label2Panel.add(new JLabel ("Luogo di nascita: "));
         inputOutputPanel = new JPanel();
         inputOutputPanel.setLayout(new GridLayout (7,1));
         nomeInput = new JTextField (20);
         inputOutputPanel.add(nomeInput);
         cognomeInput = new JTextField (20);
         inputOutputPanel.add(cognomeInput);
         addressInput = new JTextField (20);
         inputOutputPanel.add(addressInput);
         patenteInput = new JTextField (20);
         inputOutputPanel.add(patenteInput);
         annoInput = new JTextField (4);
         inputOutputPanel.add(annoInput);
         nazioneInput = new JTextField (20);
         inputOutputPanel.add(nazioneInput);
         luogoInput = new JTextField (20);
         inputOutputPanel.add(luogoInput);
         display2Panel = new JPanel();
         display2Panel.setLayout(new BorderLayout());
         display2Panel.add(label2Panel,BorderLayout.WEST);
         display2Panel.add(inputOutputPanel,BorderLayout.CENTER);
         okButton = new JButton("Ok");
         button2Panel = new JPanel();
         button2Panel.add(okButton);
         back2Panel = new JPanel();
         back2Panel.setLayout(new BorderLayout());
         getContentPane().removeAll();
         back2Panel.add(display2Panel,BorderLayout.CENTER);
         back2Panel.add(button2Panel,BorderLayout.SOUTH);
         getContentPane().add(back2Panel);
         System.exit(0);
         
         okButton.addActionListener(new ActionListener()
         {
              public void actionPerformed(ActionEvent e)
                   {
                        getText();
                        JOptionPane.showMessageDialog(
          		                null, output, "Dati immessi", JOptionPane.INFORMATION_MESSAGE);  
                        System.exit(0);
                   }
         }
         );
    
    }
    
    public void auto()
    {
         display3Panel = new JPanel();
         display3Panel.setLayout(new BorderLayout());
         label3Panel = new JPanel();
         label3Panel.setLayout(new GridLayout (4,1));
         label3Panel.add(new JLabel ("Casa costruttrice: "));
         label3Panel.add(new JLabel ("Modello: "));
         label3Panel.add(new JLabel ("Anno di immatricolazione: "));
         label3Panel.add(new JLabel ("Periodo di affitto: "));
         inputOutput3Panel = new JPanel();
         inputOutput3Panel.setLayout(new GridLayout (4,1));
         casa = new JTextField (20);
         inputOutput3Panel.add(casa);
         modello = new JTextField (20);
         inputOutput3Panel.add(modello);
         immat = new JTextField (20);
         inputOutput3Panel.add(immat);
         periodo = new JTextField (20);
         inputOutput3Panel.add(periodo);
         display3Panel = new JPanel();
         display3Panel.setLayout(new BorderLayout());
         display3Panel.add(label3Panel,BorderLayout.WEST);
         display3Panel.add(inputOutput3Panel,BorderLayout.CENTER);
         fattoButton = new JButton("Ok");
         button3Panel = new JPanel();
         button3Panel.add(fattoButton);
         back3Panel.setLayout(new BorderLayout());
         getContentPane().removeAll();
         back3Panel.add(display3Panel,BorderLayout.CENTER);
         back3Panel.add(button3Panel,BorderLayout.SOUTH);
         getContentPane().add(back3Panel);
         
         fattoButton.addActionListener(new ActionListener()
         {
              public void actionPerformed(ActionEvent e)
                   {
                        getText2();
                        JOptionPane.showMessageDialog(
          		                null, output2, "Dati auto da affittare", JOptionPane.INFORMATION_MESSAGE);  
                        System.exit(0);
                   }
         }
         );
    }
    
    public static void main(String[] args)
    {
          AddressBook addressbook = new AddressBook();
          
    }}
    se così non va c'è un errore in preleva()
    magari il tuo metodo è valido ma è meglio definire una volta sola gli eventi (meno che non si leghino ad altri)

  3. #3
    Utente di HTML.it L'avatar di MMarzia
    Registrato dal
    Mar 2001
    Messaggi
    1,781
    ho modificato il titolo, troppo generico
    io sono festosamente cicciottello :: e. cartman

    t'amo senza sapere come, nè quando nè da dove,
    t'amo direttamente senza problemi nè orgoglio:
    così ti amo perchè non so amare altrimenti

  4. #4
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,320
    @ keratox: la cosa che hai scritto non può funzionare.

    L'istruzione switch opera su valori di tipo intero, non su oggetti, quindi bisogna agire tramite istruzioni if oppure arrangiarsi con degli indici per gli oggetti (cosa più laboriosa).

    Poi la classe non implementa ActionListener, ma basta aggiungerlo.

    @ Fire Fox II: non ho provato l'applicazione perchè non ho molto tempo ora... però ho notato un'istruzione System.exit(0). Viene eseguita questa istruzione o il programma si blocca? Se viene eseguita il programma termina senza visualizzare niente, appunto.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    1,965
    non l'ho provato il prog
    e da mesi non uso java

    il mio intento era di indicare come strutturare il programma utilizzando gli eventi

    cmq grazie x la nota

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.