forked from kant003/JavaPracticeHacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ejercicio02.java
226 lines (194 loc) · 6.75 KB
/
Ejercicio02.java
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
package ejerciciosInterfaces;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Ejercicio02 {
private JFrame frame;
private JTextField text1;
private JTextField text2;
private JTextField text3;
private JTextField text4;
private JComboBox<String> comboBox;
private JPanel panel;
private JPanel panel_1;
private JButton btnCalcular;
private JButton btnLimpiar;
private JLabel lblResultado;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Ejercicio02 window = new Ejercicio02();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Ejercicio02() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 302, 273);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
JLabel lblNum1 = new JLabel("Numero 1");
GridBagConstraints gbc_lblNum1 = new GridBagConstraints();
gbc_lblNum1.anchor = GridBagConstraints.EAST;
gbc_lblNum1.insets = new Insets(15, 15, 5, 5);
gbc_lblNum1.gridx = 0;
gbc_lblNum1.gridy = 0;
frame.getContentPane().add(lblNum1, gbc_lblNum1);
text1 = new JTextField();
GridBagConstraints gbc_text1 = new GridBagConstraints();
gbc_text1.anchor = GridBagConstraints.EAST;
gbc_text1.insets = new Insets(15, 0, 5, 15);
gbc_text1.gridx = 1;
gbc_text1.gridy = 0;
frame.getContentPane().add(text1, gbc_text1);
text1.setColumns(10);
JLabel lblNum2 = new JLabel("Numero 2");
GridBagConstraints gbc_lblNum2 = new GridBagConstraints();
gbc_lblNum2.anchor = GridBagConstraints.EAST;
gbc_lblNum2.insets = new Insets(0, 0, 5, 5);
gbc_lblNum2.gridx = 0;
gbc_lblNum2.gridy = 1;
frame.getContentPane().add(lblNum2, gbc_lblNum2);
text2 = new JTextField();
GridBagConstraints gbc_text2 = new GridBagConstraints();
gbc_text2.anchor = GridBagConstraints.EAST;
gbc_text2.insets = new Insets(0, 0, 5, 15);
gbc_text2.gridx = 1;
gbc_text2.gridy = 1;
frame.getContentPane().add(text2, gbc_text2);
text2.setColumns(10);
JLabel lblNum3 = new JLabel("Numero 3");
GridBagConstraints gbc_lblNum3 = new GridBagConstraints();
gbc_lblNum3.anchor = GridBagConstraints.EAST;
gbc_lblNum3.insets = new Insets(0, 0, 5, 5);
gbc_lblNum3.gridx = 0;
gbc_lblNum3.gridy = 2;
frame.getContentPane().add(lblNum3, gbc_lblNum3);
text3 = new JTextField();
GridBagConstraints gbc_text3 = new GridBagConstraints();
gbc_text3.anchor = GridBagConstraints.EAST;
gbc_text3.insets = new Insets(0, 0, 5, 15);
gbc_text3.gridx = 1;
gbc_text3.gridy = 2;
frame.getContentPane().add(text3, gbc_text3);
text3.setColumns(10);
JLabel lblNum4 = new JLabel("Numero 4");
GridBagConstraints gbc_lblNum4 = new GridBagConstraints();
gbc_lblNum4.anchor = GridBagConstraints.EAST;
gbc_lblNum4.insets = new Insets(0, 0, 5, 5);
gbc_lblNum4.gridx = 0;
gbc_lblNum4.gridy = 3;
frame.getContentPane().add(lblNum4, gbc_lblNum4);
text4 = new JTextField();
GridBagConstraints gbc_text4 = new GridBagConstraints();
gbc_text4.anchor = GridBagConstraints.EAST;
gbc_text4.insets = new Insets(0, 0, 5, 15);
gbc_text4.gridx = 1;
gbc_text4.gridy = 3;
frame.getContentPane().add(text4, gbc_text4);
text4.setColumns(10);
comboBox = new JComboBox<String>();
comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"SUMAR", "RESTAR", "MULTIPLICAR", "DIVIDIR"}));
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(15, 0, 5, 15);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 1;
gbc_comboBox.gridy = 4;
frame.getContentPane().add(comboBox, gbc_comboBox);
panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 1;
gbc_panel.gridy = 5;
frame.getContentPane().add(panel, gbc_panel);
panel.setLayout(new BorderLayout(0, 0));
panel_1 = new JPanel();
panel.add(panel_1, BorderLayout.NORTH);
panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
btnCalcular = new JButton("CALCULAR");
btnCalcular.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String numero1 = text1.getText();
String numero2 = text2.getText();
String numero3 = text3.getText();
String numero4 = text4.getText();
try {
// convertimos a numero
float n1 = Float.parseFloat(numero1);
float n2 = Float.parseFloat(numero2);
float n3 = Float.parseFloat(numero3);
float n4 = Float.parseFloat(numero4);
float r = 0;
// realizamos el la suma
if(comboBox.getSelectedIndex()==0) {
r = n1 + n2 + n3 + n4;
}
if(comboBox.getSelectedIndex()==1) {
r = n1-n2-n3-n4;
}
if(comboBox.getSelectedIndex()==2) {
r = n1*n2*n3*n4;
}
if(comboBox.getSelectedIndex()==3) {
r = n1/n2/n3/n4;
}
// escribimos el resultado
lblResultado.setText(r + "");
} catch (NumberFormatException ex) {
lblResultado.setText("ERROR");
JOptionPane.showMessageDialog(frame, "No se puede realizar el calculo", "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
});
panel_1.add(btnCalcular);
btnLimpiar = new JButton("LIMPIAR");
btnLimpiar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblResultado.setText("-");
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("");
}
});
panel_1.add(btnLimpiar);
lblResultado = new JLabel("-");
panel.add(lblResultado, BorderLayout.WEST);
}
}