-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interfaz.java
243 lines (239 loc) · 7.6 KB
/
Interfaz.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* Luis Antonio Vázquez García
* Estructuras de Datos
* Date creation 26/Agosto/2016
*/
package GUI;
import java.io.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Interfaz extends JFrame implements ActionListener{
private static final int FRAME_WIDTH = 800; //Anchura del Frame
private static final int FRAME_HEIGHT = 400; //Altura del Frame
//Elementos que se van a agregar al frame principal
private JTextArea textArea;
private JScrollPane scroll;
private JMenu fileMenu;
//Para leer archivos
File inFile, outFile;
FileReader fr;
BufferedReader bufReader;
//JFileChooser chooser;
FileOutputStream outFileStream;
PrintWriter outStream;
FileDialog fd;
int arreglo [];
int datoEliminar;
int datoAgregar;
int MAX;
int cont;
/*
* Constructor que empieza todos los parametros por defecto:
* es decir los menus, etc...
*/
public Interfaz(){
Container contentPane;
setTitle ("************** GUI Proyecto 1 **************");
setSize (FRAME_WIDTH, FRAME_HEIGHT);
contentPane = getContentPane();
contentPane.setBackground( Color.white );
textArea = new JTextArea();
textArea.setEditable(false);
Border border = BorderFactory.createLineBorder(Color.BLACK);
textArea.setBorder(border);
setResizable(false);
scroll= new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setBackground(Color.BLACK);
contentPane.add(scroll);
crearMenuAcciones();
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
arreglo = new int[25];
MAX=25;
cont=0;
//for (int i =0;i<arreglo.length;i++){
// arreglo[i] = i;
//}
}
public void crearMenuAcciones(){
/*
* Este método crea los items que va a contener el JMenu y posteriormente van a responder
* a las acciones(actionPerformed)
*/
fileMenu = new JMenu("ACCIONES");
//-----
JMenuItem itemI;
itemI = new JMenuItem("INSERTAR DATO"); //Insertar un dato
itemI.addActionListener( this );
fileMenu.add( itemI );
//-----
JMenuItem itemA;
itemA = new JMenuItem("AGREGAR DATOS DE UN ARCHIVO"); //Agregar de un archivo
itemA.addActionListener( this );
fileMenu.add( itemA );
//----
JMenuItem itemE;
itemE = new JMenuItem("ELIMINAR UN DATO"); //Eliminar un dato
itemE.addActionListener( this );
fileMenu.add( itemE );
//----
JMenuItem itemM;
itemM = new JMenuItem("MOSTRAR DATOS"); //Mostrar los datos
itemM.addActionListener( this );
fileMenu.add( itemM );
//__________
fileMenu.addSeparator(); //Separador para resaltar "salir"
//__________
JMenuItem item;
item = new JMenuItem("**SALIR**"); //salir
item.addActionListener( this );
fileMenu.add( item );
}
public void actionPerformed(ActionEvent event) {
/*
* Este metodo es el que responde a las acciones que crean los items
* del menu que se creó anteriormente, se crea una variable de tipo
* String para que sea comparada con el nombre del item y este haga
* su trabajo correspondiente...
*/
String menuName;
menuName = event.getActionCommand();
if(menuName.equals("**SALIR**")){
System.exit(0);
}else{
if(menuName.equals("INSERTAR DATO")){
try{
datoAgregar= Integer.parseInt(JOptionPane.showInputDialog(this,"Tu dato:","INSERTAR DATO",JOptionPane.INFORMATION_MESSAGE));
agregarDato();
}catch (Exception e){
e.getStackTrace();
}
}
if(event.getActionCommand().equals("AGREGAR DATOS DE UN ARCHIVO")){
abrirArchivo();
}
if(event.getActionCommand().equals("ELIMINAR UN DATO")){
try{
datoEliminar= Integer.parseInt(JOptionPane.showInputDialog(this,"Inserta la posición a eliminar","INSERTAR DATO",JOptionPane.INFORMATION_MESSAGE));
}catch(Exception e){
e.getMessage();
}
eliminarDato();
}
if(event.getActionCommand().equals("MOSTRAR DATOS")){
mostrarDatos();
}
}
}
public void agregarDato(){
/*
* Este método lo tome del de alta_secuencial
* el cual arega el dato que deseemos a la posicion
* libre que este despues del ultimo usado.
*/
if(cont==MAX){
textArea.append("Ya está lleno el arreglo");
}else{
arreglo[cont]=datoAgregar;
cont++;
}
mostrarDatos();
}
public void eliminarDato(){
/*
* Este método elimina la posicion deseada y
* asigna a esa posición 0
*/
//arreglo[datoEliminar]=0;
//cont--;
int arreglocopy[];
int x=0;
arreglocopy=arreglo;
try{
for(int i=0;i<arreglocopy.length; i++){
if(i==datoEliminar){
i++;
}
arreglo[x]=arreglocopy[i];
x++;
}
arreglo[arreglo.length]=0;
cont--;
}catch(Exception e){
e.getMessage();
}
mostrarDatos();
}
public void mostrarDatos(){
/*
* Este método muestra todos los datos que hay en el arreglo los
* cuales ya fueron agregados anteriormente, de lo contrario los
* valores que muestra serán 0,0,0...
*/
textArea.append("Los datos en el arreglo son: \n");
for(int i:arreglo){
String numero;
numero= Integer.toString(i);
textArea.append(" [ "+numero+" ] ,");
textArea.append("");
/*
* Como convertir INT a String lo encontre en: https://leandrotemperoni.wordpress.com/2012/07/27/como-pasar-de-int-a-string-en-java-y-viceversa/
*/
}
textArea.append("\n");
}
private void abrirArchivo(){
/*
* Este método lee un archivo, el cual lo tome del
* ejemplo que está en blackboard...
*/
int reply,i;
String line, archivo, doc, path;
doc = "";
try {
fd = new FileDialog(this, "Selecciona archivo para cargar datos", FileDialog.LOAD);
fd.setVisible(true);
path = fd.getDirectory();
archivo = fd.getFile();
if ((path == null) || (archivo == null)) return;
doc = path + archivo;
textArea.append(doc);
//System.out.println("RUTA = "+doc);
inFile = new File(doc);
fr = new FileReader(inFile);
bufReader = new BufferedReader(fr);
textArea.setText("");
i=cont;
while (true) {
line = bufReader.readLine();
if (line == null) {
return;
}
arreglo[i]=Integer.parseInt(line);
i = i+1;
cont++;
textArea.append(" "+line);
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error in opening a file: \n"+ e.getMessage());
}
finally{
for(i=0;i<arreglo.length;i++)
textArea.append("\n"+"Arreglo[ "+i+" ] = "+arreglo[i]+"\n");
/*
* Lo del "\n" que no sabia lo tome de:
* http://www.lawebdelprogramador.com/foros/Java/937289-Salto-de-linea-del-TextArea.html
*/
}
}
public static void main(String[] args){
Interfaz ventana = new Interfaz();
ventana.setVisible(true);
}
}