Nr La classe di gestione degli eventi sulla finestra
1 import java.io.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 class GestoreFinestra implements WindowListener {
5 public void windowIconified(WindowEvent e) {}
6 public void windowDeiconified(WindowEvent e){}
7 public void windowActivated(WindowEvent e){}
8 public void windowDeactivated(WindowEvent e){}
9 public void windowOpened(WindowEvent e){}
10 public void windowClosed(WindowEvent e){}
11 public void windowClosing(WindowEvent e){
12 System.exit(0);
13 }
14 }
Commenti:
Nr. linea | descrizione |
1, 2 | I prototipi delle classi WindowListener e WindowEvent, cui si fa riferimento nella definizione della classe GestoreFinestra, sono contenuti nella libreria AWT |
3 | La classe GestoreFinestra è derivata dalla classe WindowListener e ne ridefinisce i metodi. |
4÷9 | Si devono elencare tutti i metodi della classe, anche se non vengono ridefiniti. |
10÷11 | Il metodo windowClosing viene ridefinito. L’evento windowClosing si verifica quando si chiude la finestra. L’operazione svolta consiste nel restituire il controllo al sistema operativo mediante il metodo exit dell’oggetto System. Il parametro 0, del metodo exit, è un codice che il sistema operativo si aspetta e che usa per individuare la terminazione con o senza errore del programma. |
Nr Istruzioni del programma 1 class Trasforma extends Frame implements ActionListener {
2 private String Num[] = new String[30]; 3 private Panel p1 = new Panel();
4 private Panel p2 = new Panel(); 5 private TextField ImportoNum = new TextField(15);
6 private TextField ImportoLett = new TextField(50); 7 private Button converti = new Button("Converti");
Nr | Istruzioni del programma |
8 | public Trasforma() { |
9 | super("Da cifre a lettere"); |
10 | addWindowListener(new GestoreFinestra()); |
11 | p1.add(new Label("Importo Numerico: ")); |
12 | p1.add(ImportoNum); |
13 | p2.add(new Label("Importo in Lettere: ")); |
14 | p2.add(ImportoLett); |
15 | setLayout(new GridLayout(3,1,5,10)); |
16 | add(p1); |
17 | add(converti); |
18 | add(p2); |
19 | converti.addActionListener(this); |
20 | NumLet(); |
21 | } |
0 | Zero |
1 | Uno |
2 | Due |
3 | Tre |
4 | Quattro |
5 | Cinque |
6 | Sei |
7 | Sette |
8 | Otto |
9 | Nove |
10 | Dieci |
11 | Undici |
12 | Dodici |
13 | Tredici |
14 | Quattordici |
15 | Quindici |
16 | Sedici |
17 | Diciassette |
18 | Diciotto |
19 | Diciannove |
posizione | stringa |
22 | venti |
23 | trenta |
24 | quaranta |
25 | cinquanta |
26 | sessanta |
27 | settanta |
28 | ottanta |
29 | novanta |
22 | private void NumLet(){ |
23 | Num[0]="zero"; |
24 | Num[1]="uno"; |
25 | Num[2]="due"; |
26 | Num[3]="tre"; |
27 | Num[4]="quattro"; |
28 | Num[5]="cinque"; |
29 | Num[6]="sei"; |
30 | Num[7]="sette"; |
31 | Num[8]="otto"; |
32 | Num[9]="nove"; |
33 | Num[10]="dieci"; |
34 | Num[11]="undici"; |
35 | Num[12]="dodici"; |
36 | Num[13]="tredici"; |
37 | Num[14]="quattordici"; |
38 | Num[15]="quindici"; |
39 | Num[16]="sedici"; |
40 | Num[17]="diciassette"; |
41 | Num[18]="diciotto"; |
42 | Num[19]="diciannove"; |
43 | Num[20]=""; |
44 | Num[21]=""; |
45 | Num[22]="venti"; |
46 | Num[23]="trenta"; |
47 | Num[24]="quaranta"; |
48 | Num[25]="cinquanta"; |
49 | Num[26]="sessanta"; |
50 | Num[27]="settanta"; |
51 | Num[28]="ottanta"; |
52 | Num[29]="novanta"; |
53 | } |
Nr | Istruzioni del programma |
54 | public String Converti(long u){ |
55 | return Num[(int)u]; |
56 | } |
Nr | Istruzioni del programma |
57 | public void actionPerformed(ActionEvent e) { |
58 | String bottone = e.getActionCommand(); |
59 | long Numero; |
60 | String Lettere; |
61 | if (bottone.equals("Converti")) { |
62 | try { |
63 | String numeroLetto = ImportoNum.getText(); |
64 | Numero = Long.valueOf(numeroLetto).longValue(); |
65 | Lettere = TrasformaDaCifre(Numero); |
66 | ImportoLett.setText(" "+Lettere); |
67 | } |
68 | catch(Exception exc){ |
69 | ImportoLett.setText("usa il punto per separare i decimali"); |
70 | } |
71 | } |
72 | } |
Nr | Istruzioni del programma |
73 | public String TrasformaDaCifre(long N){ |
74 | String str; |
75 | str = Converti3Cifre(N); |
76 | return str; |
77 | } |
Nr | Istruzioni del programma |
78 | public String Converti3Cifre(long TreCifre){ |
79 | long Centinaia, Decine, UltimaCifra; |
80 | String strCentinaia, strDecine, strUnita; |
81 | Centinaia = TreCifre/100; |
82 | strCentinaia = ""; |
83 | if (Centinaia > 0) { |
84 | strCentinaia = "cento"; |
85 | if (Centinaia>1) strCentinaia = Converti(Centinaia) + strCentinaia; |
86 | } |
87 | Decine = TreCifre % 100; |
88 | if (Decine < 20) { |
89 | strDecine = Converti(Decine); |
90 | return strCentinaia + strDecine; |
91 | } |
92 | Decine = Decine/10; |
93 | UltimaCifra = TreCifre % 10; |
94 | strDecine = Converti(Decine+20); |
95 | if ((UltimaCifra==1) || (UltimaCifra==8)) { |
96 | strDecine = strDecine.substring(0,strDecine.length()-1); |
97 | } |
98 | if (UltimaCifra>0) strUnita = Converti(UltimaCifra); |
99 | else strUnita = ""; |
101 | return strCentinaia + strDecine + strUnita; |
102 | } |
103 | } |
Nr | Istruzioni del programma |
104 | class DaCifreaLettere { |
105 | public static void main (String argv[]) { |
106 | Trasforma f = new Trasforma(); |
107 | f. pack(); |
108 | f.setVisible(true); |
109 | } |
110 | } |
73 | public String TrasformaDaCifre(long N){ |
74 | int Milioni, Migliaia, Unita; |
75 | String strMilioni, strMigliaia, strUnita, str; |
75 | Milioni = (int) (N/1000000); |
76 | Migliaia = (int) (N % 1000000); |
77 | Migliaia = (int) (Migliaia/1000); |
78 | Unita = (int) (N % 1000); |
79 | str=""; |
80 | strMilioni=""; |
81 | strMigliaia=""; |
82 | strUnita=""; |
83 | if (Milioni > 1) strMilioni = Converti3Cifre(Milioni); |
84 | if (Migliaia>1) strMigliaia = Converti3Cifre(Migliaia); |
85 | if (Unita>1) strUnita = Converti3Cifre(Unita); |
86 | if (Milioni>0) { |
87 | if (Milioni==1) str = str+"unmilione"; |
88 | else str = str+strMilioni+"milioni"; |
89 | } |
90 | if (Migliaia>0) { |
91 | if (Migliaia==1) str=str + "mille"; |
92 | else str = str+strMigliaia + "mila"; |
93 | } |
94 | if (Unita>0) { |
95 | if (Unita==1) str = str + "uno"; |
96 | else str = str + strUnita; |
97 | } |
98 | if (str=="") str = "zero"; |
99 | return str; |
100 | } |