miércoles, 23 de mayo de 2012

Interface Grafica para la AGENDA

//CLASE PRINCIPAL

//


/**
*
* @author cvleonardo
*/

package vista;

import modelo.negocio.Funcion;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class GUITest extends JFrame {
public JButton enviar;
public JTextField nombreJTF;
public JLabel nombreLb;

public GUITest(){
componentes();
}

public void componentes(){
setSize(500, 500);
setLocation(100, 100);

nombreLb = new JLabel();
nombreLb.setBounds(70, 30, 153, 20);
nombreLb.setText("introduce nombre: ");
getContentPane().add(nombreLb);

nombreJTF = new JTextField();
nombreJTF.setBounds(220, 30, 73, 20);
getContentPane().add(nombreJTF);

enviar = new JButton();
enviar.setText("Enviar");
enviar.setBounds(211, 109, 73, 23);
enviar.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed
(java.awt.event.ActionEvent evt) {
enviarOperacion(evt);
}
});
getContentPane().add(enviar);

setDefaultCloseOperation(
javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
}
public void enviarOperacion(java.awt.event.ActionEvent evt){
Funcion fn = new Funcion();
String dato = nombreJTF.getText();
JOptionPane.showMessageDialog(null, fn.crearNombre(dato));
}
public static void main(String[] args) {
new GUITest().setVisible(true);
}
}


//CLASE SECUNDARIA
//


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package modelo.negocio;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

/**
*
* @author cvleonardo
*/


public class Funcion {
public String crearNombre(String dato){
try{
File arch = new File("d:\\nombre.txt");
FileWriter fw = new FileWriter(arch, true);
PrintWriter pw = new PrintWriter(fw);
pw.print(dato);
fw.close();
return "Transaccion correcta!";
}catch(Exception e){
return "Transaccion incorrecta!";
}
}
}


RESULTADO

Método corregido y aumentado "leerContacto"

public void leerContacto(int id) {
//*aportacion especial 4s21
//* ADONAY JESUS MERINO FILIO
//*
//*//
System.out.println("\n");
try {
File arch = new File("d:\\Agenda.txt");
FileReader fr = new FileReader(arch);
BufferedReader br = new BufferedReader(fr);
LineNumberReader lr = new LineNumberReader(fr);
String datos1 = null;
String[] dat;
int o = 0, k, u = 0;
k = (id * 4);
while ((datos1 = lr.readLine()) != null) {
o = lr.getLineNumber();
if (o == k - 3) {
u++;
System.out.println(datos1);
k++;
if (u == 4) {
System.out.println("\n");
break;
}
}
}
lr.close();
} catch (Exception e) {
}
}