Java - Añadir un action listener a un botón de calculadora y que escriba el valor del botón en un JText

 
Vista:

Añadir un action listener a un botón de calculadora y que escriba el valor del botón en un JText

Publicado por Aldo (1 intervención) el 01/11/2019 01:26:05
El código está en 2 clases:
Ventana:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import javax.swing.*;
import java.awt.*;
public class ventana extends JFrame {
public JPanel panel;
public ventana () { //constructor
    setSize(685,605); //se estabece el tamaño de la ventana
    setDefaultCloseOperation(EXIT_ON_CLOSE); //close on exit
    setTitle("Calculadora Científica"); //se establece el título
    setLocationRelativeTo(null); //se establece la ventana en el centro
    iniciarcomponentes (); //activo mis componentes
}
private void iniciarcomponentes () {
    colocarpanel(); //iniciamos el panel 
    colocarbotones(); //iniciamos los botones
    colocarcajadetexto();
}
private void colocarpanel() {
    panel = new JPanel (); //crear mi panel
    getContentPane().add(panel); //agregar el panel
    panel.setLayout(null);
}
private void colocarbotones() {
    JButton b1 = new JButton(); //crear botón 1
    b1.setText("Sin"); //establecer texto al botón
    b1.setBounds(50, 140, 90, 60); //definir posición del botón
    // creamos botón 2
    JButton b2 = new JButton();
    b2.setText("Cos");
    b2.setBounds(150, 140, 90, 60);
    //creamos botón 3
    JButton b3 = new JButton();
    b3.setText("Tan");
    b3.setBounds(250, 140, 90, 60);
    //creamos botón 4
    JButton b4 = new JButton();
    b4.setText("Sin -1");
    b4.setBounds(50, 210, 90, 60);
    //creamos botón 5
    JButton b5 = new JButton();
    b5.setText("Cos -1");
    b5.setBounds(150, 210, 90, 60);
    //creamos botón 6
    JButton b6 = new JButton();
    b6.setText("Tan -1");
    b6.setBounds(250, 210, 90, 60);
    //creamos botón 7
    JButton b7 = new JButton();
    b7.setText("Pi");
    b7.setBounds(50, 280, 90, 130);
    //creamos botón 8
    JButton b8 = new JButton();
    b8.setText("10^x");
    b8.setBounds(150, 280, 90, 130);
    //creamos botón 9
    JButton b9 = new JButton();
    b9.setText("10^-x");
    b9.setBounds(250, 280, 90, 130);
    //creamos botón 10
    JButton b10 = new JButton();
    b10.setText("Log");
    b10.setBounds(50, 420, 90, 60);
    //creamos botón 11
    JButton b11 = new JButton();
    b11.setText("x^2");
    b11.setBounds(150, 420, 90, 60);
    //creamos botón 12
    JButton b12 = new JButton();
    b12.setText("x^x");
    b12.setBounds(250, 420, 90, 60);
 
    //creamos botón raíz cudrada
    JButton sqrt = new JButton();
    sqrt.setText("sqrt");
    sqrt.setBounds(50, 490, 140, 60);
    //creamos botón raíz
    JButton sqrt2 = new JButton();
    sqrt2.setText("sqrt2");
    sqrt2.setBounds(200, 490, 140, 60);
 
    //creamos botón 13
    JButton b13 = new JButton();
    b13.setText("c");
    b13.setBounds(350, 140, 60, 60);
    //creamos botón 14
    JButton b14 = new JButton();
    b14.setText("ce");
    b14.setBounds(420, 140, 60, 60);
    //creamos botón 15
    JButton b15 = new JButton();
    b15.setText("/");
    b15.setBounds(490, 140, 60, 60);
    //creamos botón 16
    JButton b16 = new JButton();
    b16.setText("%");
    b16.setBounds(560, 140, 60, 60);
    //creamos botón 17
    JButton b17 = new JButton();
    b17.setText("1");
    b17.setBounds(350, 210, 60, 60);
    //creamos botón 18
    JButton b18 = new JButton();
    b18.setText("2");
    b18.setBounds(420, 210, 60, 60);
    //creamos botón 19
    JButton b19 = new JButton();
    b19.setText("3");
    b19.setBounds(490, 210, 60, 60);
    //creamos botón 20
    JButton b20 = new JButton();
    b20.setText("4");
    b20.setBounds(350, 280, 60, 60);
    //creamos botón 21
    JButton b21 = new JButton();
    b21.setText("5");
    b21.setBounds(420, 280, 60, 60);
    //creamos botón 22
    JButton b22 = new JButton();
    b22.setText("6");
    b22.setBounds(490, 280, 60, 60);
    //creamos botón 23
    JButton b23 = new JButton();
    b23.setText("7");
    b23.setBounds(350, 350, 60, 60);
    //creamos botón 24
    JButton b24 = new JButton();
    b24.setText("8");
    b24.setBounds(420, 350, 60, 60);
    //creamos botón 25
    JButton b25 = new JButton();
    b25.setText("9");
    b25.setBounds(490, 350, 60, 60);
    //creamos botón 26
    JButton b26 = new JButton();
    b26.setText("0");
    b26.setBounds(350, 420, 130, 60);
    //creamos botón 27
    JButton b27 = new JButton();
    b27.setText("=");
    b27.setBounds(490, 420, 60, 60);
 
    //creamos botón adelante
    JButton adelante = new JButton();
    adelante.setText("adelante");
    adelante.setBounds(350, 490, 130, 60);
    //creamos botón atrás
    JButton atras = new JButton();
    atras.setText("adelante");
    atras.setBounds(490, 490, 130, 60);
 
    //creamos botón 28
    JButton b28 = new JButton();
    b28.setText("+");
    b28.setBounds(560, 210, 60, 60);
    //creamos botón 28
    JButton b29 = new JButton();
    b29.setText("-");
    b29.setBounds(560, 280, 60, 60);
    //creamos botón 28
    JButton b30 = new JButton();
    b30.setText("*");
    b30.setBounds(560, 350, 60, 60);
    //creamos botón 28
    JButton b31 = new JButton();
    b31.setText(".");
    b31.setBounds(560, 420, 60, 60);
    //b1.setMnemonic('h'); ==
    // alt + h = presionar el botón de hola 
    b1.setForeground(Color.BLUE); //establecemos el color de la letra del botón
    b1.setFont(new Font("arial", Font.BOLD, 12)); //establecemos fuente de la letra del botón 
    //en el estilo (segundo parámetro) 0 es estilo plano, 1 negrita, 2 cursiva y 3 es negrita y cursiva
    panel.add(b1); //agregamos botón al panel
    panel.add(b2); //agregamos botón 2
    panel.add(b3);
    panel.add(b4);
    panel.add(b5);
    panel.add(b6);
    panel.add(b7);
    panel.add(b8);
    panel.add(b9);
    panel.add(b10);
    panel.add(b11);
    panel.add(b12);
    panel.add(b13);
    panel.add(b14);
    panel.add(b15);
    panel.add(b16);
    panel.add(b17);
    panel.add(b18);
    panel.add(b19);
    panel.add(b20);
    panel.add(b21);
    panel.add(b22);
    panel.add(b23);
    panel.add(b24);
    panel.add(b25);
    panel.add(b26);
    panel.add(b27);
    panel.add(b28);
    panel.add(b29);
    panel.add(b30);
    panel.add(b31);
    panel.add(sqrt);
    panel.add(sqrt2);
    panel.add(adelante);
    panel.add(atras);
}
private void colocarcajadetexto () {
    JTextField caja1 = new JTextField (); //creamos caja
    caja1.setBounds(85, 50, 500, 70); //definimos la posición
    caja1.setText("hola"); //agregamos un texto a la caja
 
    // pedir texto de la caja desde la consola:
    //System.out.println("Texto de la caja: " +caja1.getText());
    panel.add(caja1); //agragamos caja al panel
 
}
}
 
 
Principal (main):
public class principal {
public static void main() {
     ventana v1 = new ventana ();
     v1.setVisible(true);
 
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder