SHOW:
|
|
- or go back to the newest paste.
1 | import javax.swing.JFrame; | |
2 | import javax.swing.Action; | |
3 | import javax.swing.JButton; | |
4 | import javax.swing.JLabel; | |
5 | import javax.swing.JPanel; | |
6 | import java.awt.GridLayout; | |
7 | import javax.swing.JOptionPane; | |
8 | import java.awt.TextField; | |
9 | import java.awt.event.KeyAdapter; | |
10 | import java.awt.event.KeyEvent; | |
11 | import java.awt.event.MouseAdapter; | |
12 | import java.awt.event.MouseEvent; | |
13 | ||
14 | public class Calcolatrice extends JFrame{ | |
15 | ||
16 | private JButton[] numeri = new JButton[12]; | |
17 | private JButton[] operatori = new JButton[4]; | |
18 | private TextField casellaDiCalcolo = new TextField(); | |
19 | private String stampa = ""; | |
20 | ||
21 | ||
22 | public Calcolatrice(){ | |
23 | setVisible(true); | |
24 | setSize(500,500); | |
25 | setLayout(new GridLayout(3,1)); | |
26 | add(casellaDiCalcolo); | |
27 | add(new TabNumeri()); | |
28 | add(new TabOperatori()); | |
29 | addMouseListener(new AscoltatoreMouse()); | |
30 | setDefaultCloseOperation(EXIT_ON_CLOSE); | |
31 | }//Calcolatrice | |
32 | ||
33 | ||
34 | class AscoltatoreMouse extends MouseAdapter{ | |
35 | private double op1 = 0; | |
36 | private double op2 = 0; | |
37 | private boolean isOp2 = false; | |
38 | - | private string operatore = "+"; |
38 | + | private String operatore = "+"; |
39 | ||
40 | public void mousePressed(MouseEvent e){ | |
41 | JButton bottoneMagico; | |
42 | if(e.getComponent() instanceof JButton) { | |
43 | bottoneMagico = (JButton)e.getComponent(); | |
44 | - | if(bottoneMagico.getText().isDigit()) |
44 | + | if(Character.isDigit(bottoneMagico.getText().charAt(0))) |
45 | casellaDiCalcolo.setText(casellaDiCalcolo.getText()+bottoneMagico.getText()); | |
46 | else { | |
47 | operatore = bottoneMagico.getText(); | |
48 | ||
49 | if(isOp2) { | |
50 | op2 = Double.parseDouble(casellaDiCalcolo.getText()); | |
51 | } else { | |
52 | op1 = Double.parseDouble(casellaDiCalcolo.getText()); | |
53 | } | |
54 | ||
55 | casellaDiCalcolo.setText(""); | |
56 | isOp2 = !isOp2; | |
57 | ||
58 | if(bottoneMagico.getText() == "=") { | |
59 | - | if(operatore == "+") casellaDiCalcolo.setText(op1 + op2); |
59 | + | if(operatore == "+") casellaDiCalcolo.setText(""+(op1 + op2)); |
60 | - | if(operatore == "-") casellaDiCalcolo.setText(op1 - op2); |
60 | + | if(operatore == "-") casellaDiCalcolo.setText(""+(op1 - op2)); |
61 | - | if(operatore == "*") casellaDiCalcolo.setText(op1 * op2); |
61 | + | if(operatore == "*") casellaDiCalcolo.setText(""+(op1 * op2)); |
62 | - | if(operatore == "/") casellaDiCalcolo.setText(op1 / op2); |
62 | + | if(operatore == "/") casellaDiCalcolo.setText(""+(op1 / op2)); |
63 | } | |
64 | } | |
65 | }//if | |
66 | }//mousePressed | |
67 | }//AscoltatoreMouse | |
68 | ||
69 | ||
70 | class TabNumeri extends JPanel{ | |
71 | public TabNumeri(){ | |
72 | setLayout(new GridLayout(4,3)); | |
73 | for(int i=9;i>-1;i--){ | |
74 | numeri[i] = new JButton(""+i); | |
75 | add(numeri[i]); | |
76 | }//for | |
77 | numeri[10] = new JButton("="); | |
78 | add(numeri[10]); | |
79 | numeri[11] = new JButton("."); | |
80 | add(numeri[11]); | |
81 | }//TabNumeri | |
82 | }//TabNumeri | |
83 | ||
84 | class TabOperatori extends JPanel{ | |
85 | public TabOperatori(){ | |
86 | setLayout(new GridLayout(1,4)); | |
87 | operatori[0] = new JButton("/"); | |
88 | add(operatori[0]); | |
89 | operatori[1] = new JButton("*"); | |
90 | add(operatori[1]); | |
91 | operatori[2] = new JButton("-"); | |
92 | add(operatori[2]); | |
93 | operatori[3] = new JButton("+"); | |
94 | add(operatori[3]); | |
95 | }//TabOperatori | |
96 | }//TabOperatori | |
97 | ||
98 | }//Calcolatrice |