Repository navigation
Expand file tree
/
Copy pathConversorDeUnidadesGUI.java
More file actions
160 lines (134 loc) · 6.7 KB
/
Copy pathConversorDeUnidadesGUI.java
File metadata and controls
160 lines (134 loc) · 6.7 KB
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
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.text.DecimalFormat;
public class ConversorDeUnidadesGUI extends JFrame {
private JComboBox<String> comboTipo, comboConversion;
private JTextField txtValor, txtTasa;
private JLabel lblResultado, lblTasa;
private JTextArea areaHistorial;
private final DecimalFormat df = new DecimalFormat("#.##");
private final String[] monedas = {
"Soles (PEN) a Dólares (USD)",
"Dólares (USD) a Soles (PEN)",
"Soles (PEN) a Euros (EUR)",
"Euros (EUR) a Soles (PEN)"
};
private final double[] tasasDefecto = {0.27, 3.75, 0.25, 4.00};
public ConversorDeUnidadesGUI() {
setTitle("Conversor Avanzado Pro");
setSize(460, 580);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout(10, 10));
JPanel panelSuperior = new JPanel(new GridLayout(7, 2, 5, 5));
comboTipo = new JComboBox<>(new String[]{"Temperatura", "Moneda", "Longitud"});
comboTipo.addActionListener(e -> actualizarOpciones());
comboConversion = new JComboBox<>();
comboConversion.addActionListener(e -> actualizarTasaDefecto());
txtValor = new JTextField();
lblTasa = new JLabel(" Tasa:");
txtTasa = new JTextField("1.0");
JButton btnConvertir = new JButton("Convertir");
JButton btnInvertir = new JButton("Invertir");
JButton btnCopiar = new JButton("Copiar Resultado");
JButton btnLimpiar = new JButton("Limpiar Historial");
lblResultado = new JLabel(" Resultado: ");
panelSuperior.add(new JLabel(" Tipo:")); panelSuperior.add(comboTipo);
panelSuperior.add(new JLabel(" Conversión:")); panelSuperior.add(comboConversion);
panelSuperior.add(new JLabel(" Valor:")); panelSuperior.add(txtValor);
panelSuperior.add(lblTasa); panelSuperior.add(txtTasa);
panelSuperior.add(btnConvertir); panelSuperior.add(btnInvertir);
panelSuperior.add(btnCopiar); panelSuperior.add(btnLimpiar);
panelSuperior.add(lblResultado);
areaHistorial = new JTextArea(8, 30);
areaHistorial.setEditable(false);
btnConvertir.addActionListener(e -> realizarConversion());
btnInvertir.addActionListener(e -> invertirUnidades());
btnCopiar.addActionListener(e -> copiarResultado());
btnLimpiar.addActionListener(e -> areaHistorial.setText(""));
add(panelSuperior, BorderLayout.NORTH);
add(new JScrollPane(areaHistorial), BorderLayout.CENTER);
actualizarOpciones();
}
private void actualizarOpciones() {
comboConversion.removeAllItems();
String tipo = (String) comboTipo.getSelectedItem();
if ("Temperatura".equals(tipo)) {
comboConversion.addItem("Celsius a Fahrenheit");
comboConversion.addItem("Fahrenheit a Celsius");
lblTasa.setVisible(false); txtTasa.setVisible(false);
} else if ("Moneda".equals(tipo)) {
for (String m : monedas) {
comboConversion.addItem(m);
}
lblTasa.setText(" Tasa Cambio:"); lblTasa.setVisible(true); txtTasa.setVisible(true);
actualizarTasaDefecto();
} else {
comboConversion.addItem("Metros a Kilómetros");
comboConversion.addItem("Kilómetros a Millas");
comboConversion.addItem("Millas a Kilómetros");
lblTasa.setVisible(false); txtTasa.setVisible(false);
}
}
private void actualizarTasaDefecto() {
String tipo = (String) comboTipo.getSelectedItem();
if ("Moneda".equals(tipo)) {
int index = comboConversion.getSelectedIndex();
if (index >= 0 && index < tasasDefecto.length) {
txtTasa.setText(String.valueOf(tasasDefecto[index]));
}
}
}
private void invertirUnidades() {
String tipo = (String) comboTipo.getSelectedItem();
int index = comboConversion.getSelectedIndex();
if ("Temperatura".equals(tipo) && index >= 0 && index < 2) {
comboConversion.setSelectedIndex(index == 0 ? 1 : 0);
} else if ("Moneda".equals(tipo)) {
if (index == 0) comboConversion.setSelectedIndex(1);
else if (index == 1) comboConversion.setSelectedIndex(0);
else if (index == 2) comboConversion.setSelectedIndex(3);
else if (index == 3) comboConversion.setSelectedIndex(2);
} else if ("Longitud".equals(tipo)) {
if (index == 1) comboConversion.setSelectedIndex(2);
else if (index == 2) comboConversion.setSelectedIndex(1);
}
}
private void copiarResultado() {
String textoResultado = lblResultado.getText().replace(" Resultado: ", "");
if (!textoResultado.isEmpty() && !textoResultado.equals(" ")) {
StringSelection seleccion = new StringSelection(textoResultado);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(seleccion, null);
JOptionPane.showMessageDialog(this, "Resultado copiado al portapapeles.", "Información", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "No hay resultado para copiar.", "Aviso", JOptionPane.WARNING_MESSAGE);
}
}
private void realizarConversion() {
try {
double valor = Double.parseDouble(txtValor.getText());
double resultado = 0;
String tipo = (String) comboTipo.getSelectedItem();
int index = comboConversion.getSelectedIndex();
if ("Temperatura".equals(tipo)) {
resultado = (index == 0) ? (valor * 9/5) + 32 : (valor - 32) * 5/9;
} else if ("Moneda".equals(tipo)) {
double tasa = Double.parseDouble(txtTasa.getText());
resultado = valor * tasa;
} else {
if (index == 0) resultado = valor / 1000;
else if (index == 1) resultado = valor * 0.621371;
else if (index == 2) resultado = valor / 0.621371;
}
String resText = df.format(resultado);
lblResultado.setText(" Resultado: " + resText);
areaHistorial.append(comboConversion.getSelectedItem() + ": " + valor + " -> " + resText + "\n");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Por favor, ingrese un número válido.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ConversorDeUnidadesGUI().setVisible(true));
}
}