-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterfazGrafica.java
More file actions
143 lines (123 loc) · 4.15 KB
/
InterfazGrafica.java
File metadata and controls
143 lines (123 loc) · 4.15 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
/**
* Universidad del Valle de Guatemala
* Algoritmos y Estructura de Datos
* Sección: 10
* 30/07/2015
* Hoja de Trabajo 2
*
*/
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
/**
* Esta es la clase <InterfazGrafica> y muestra una Interfaz
* mas amigable con el usuario al tener un botón para buscar el
* archivo deseado para realizar las operacinoes. Busca el archivo
* y tienen otro botón que permite realizar el cálculo de la operacion.
* Por útltimo, tiene un área de texto en el cual muestra el resultado
* de la operación.
*
* @author André Rodas
* @author Rudy Garrido
* @author Yosemite Meléndez
*
*/
public class InterfazGrafica {
private JFrame frame;
private JSlider slider;
private JLabel label;
private JTextField textField;
private JFileChooser fc;
private File file;
private Calculadora<Integer> calculadora = new Calculadora<Integer>();
private JButton btnSeleccionarArchivo;
private JButton buttonCalcular;
private JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InterfazGrafica window = new InterfazGrafica();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public InterfazGrafica(){
initialize();
}
/**
* Crea los elementos de la interfaz gráfica.
*El frame principal, etiquetas para nombre y
*resultado, botones buscar archivo y calcular
*y un área de texto para mostrar el resultado.
*/
public void initialize(){
frame = new JFrame();
frame.setBounds(100, 100, 622, 381);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblCalculadoraPostfix = new JLabel("Calculadora Postfix");
lblCalculadoraPostfix.setFont(new Font("Tahoma", Font.BOLD, 50));
lblCalculadoraPostfix.setBounds(10, 11, 586, 106);
frame.getContentPane().add(lblCalculadoraPostfix);
btnSeleccionarArchivo = new JButton("Seleccionar Archivo");
btnSeleccionarArchivo.setBounds(422, 128, 174, 23);
btnSeleccionarArchivo.addActionListener(new Evento());
frame.getContentPane().add(btnSeleccionarArchivo);
textField = new JTextField();
textField.setBounds(20, 128, 378, 23);
frame.getContentPane().add(textField);
textField.setColumns(10);
buttonCalcular = new JButton("Calcular");
buttonCalcular.setBounds(20, 164, 174, 23);
buttonCalcular.addActionListener(new Evento());
frame.getContentPane().add(buttonCalcular);
JLabel lblResultados = new JLabel("Resultados:");
lblResultados.setBounds(20, 199, 100, 21);
frame.getContentPane().add(lblResultados);
textArea = new JTextArea();
textArea.setBounds(20, 218, 378, 113);
frame.getContentPane().add(textArea);
fc = new JFileChooser();
}
/**
* Clase interna <Evento> para poder obtener el botón al cual se le realiza
* la acción y asi realizar la operación deseada por el usuario.
* Un evento en <btnSeleccionarArchivo> permite abrir el buscador de archivos
* Un envento en <btnCalcular> permite realizar el cálculo de la operacion
*
*/
private class Evento implements ActionListener{
public void actionPerformed(ActionEvent a) {
if(a.getSource()==btnSeleccionarArchivo){
int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("Seleccion");
file = fc.getSelectedFile();
textField.setText(file.getAbsolutePath());
}
}else{
try {
calculadora.readFile(file.getAbsolutePath());
textArea.setText(String.valueOf("El resultado final de su operacion es: " +calculadora.calcular()));
} catch ( Exception e) {
e.printStackTrace();
}
}
}
}
}