GUI with arrangement

2

I have to make a window, enter some values and see them reflected in a panel, create the arrangement, but print the values that I have not set in null, I need to do two nested cycles, but I'm stuck and I do not know anymore how to do it. I pass the code, I hope you can help me.

import javax.swing.*;
import java.awt.event.*;

public class Ventana extends JFrame implements ActionListener {

    Ventana v;
    JLabel lnombre, ldireccion, lcorreo;
    JTextField tfnombre, tfdireccion, tfcorreo;
    JButton botona, botonc, botons;
    JTextArea area;
    JPanel panel;
    String Datos[][];
    String mensaje = "";
    int P = 0;

    public Ventana() {

        lnombre = new JLabel("Nombre");
        lnombre.setBounds(15, 20, 450, 20);

        ldireccion = new JLabel("Direccion");
        ldireccion.setBounds(15, 50, 450, 20);

        lcorreo = new JLabel("Correo");
        lcorreo.setBounds(15, 80, 450, 20);

        tfnombre = new JTextField();
        tfnombre.setBounds(100, 20, 450, 20);

        tfdireccion = new JTextField();
        tfdireccion.setBounds(100, 50, 450, 20);

        tfcorreo = new JTextField();
        tfcorreo.setBounds(100, 80, 450, 20);

        botona = new JButton("Aceptar");
        botona.setBounds(60, 300, 140, 20);
        botona.addActionListener(this);

        botonc = new JButton("Cancelar");
        botonc.setBounds(220, 300, 140, 20);
        botonc.addActionListener(this);

        botons = new JButton("Salir");
        botons.setBounds(380, 300, 140, 20);
        botons.addActionListener(this);

        area = new JTextArea();
        area.setBounds(10, 120, 565, 150);

        panel = new JPanel();
        panel.setLayout(null);

        panel.add(lnombre);
        panel.add(ldireccion);
        panel.add(lcorreo);
        panel.add(tfnombre);
        panel.add(tfdireccion);
        panel.add(tfcorreo);
        panel.add(botona);
        panel.add(botonc);
        panel.add(botons);
        panel.add(area);

        add(panel);
        setSize(600, 380);
        setTitle("Unidad 1");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        Datos = new String[5][3];
    }

    public static void main(String[] args) {

        Ventana v = new Ventana();

    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == botona) {
            Capturar();
            P = P + 1;
        }
        if (e.getSource() == botonc) {
            Borrar();
        }
        if (e.getSource() == botons) {
            Salir();
        }
    }

    void Capturar() {
        Guardar();
        Mostrar(Datos);
        // area.setText("Los datos ingresados son: \n Nombre : " + tfnombre.getText() + "\n 
        //Direccion : " + tfdireccion.getText() + "\n 
        //Correo : " + tfcorreo.getText());
    }

    void Borrar() {
        area.setText("");
    }

    void Salir() {
        super.dispose();
    }

    void Guardar() {
        if (P < 5) {
            Datos[P][0] = tfnombre.getText();
            Datos[P][1] = tfdireccion.getText();
            Datos[P][2] = tfcorreo.getText();
        } else {
            mensaje = "Memoria llena.";
        }
        tfnombre.setText("");
        tfdireccion.setText("");
        tfcorreo.setText("");
    }

    void Mostrar(String[][] a) {


       // if(a[i][j] != "" || a[i][j] !=null);
        area.setText("Nombre: " + a[0][0] + "     Direccion: " + a[0][1] + "     Correo: " + a[0][2] + "\n"
                + "Nombre: " + a[1][0] + "     Direccion: " + a[1][1] + "     Correo: " + a[1][2] + "\n"
                + "Nombre: " + a[2][0] + "     Direccion: " + a[2][1] + "     Correo: " + a[2][2] + "\n"
                + "Nombre: " + a[3][0] + "     Direccion: " + a[3][1] + "     Correo: " + a[3][2] + "\n"
                + "Nombre: " + a[4][0] + "     Direccion: " + a[4][1] + "     Correo: " + a[4][2] + "\n" + mensaje);
    }
}
    
asked by Ivan 04.05.2016 в 21:16
source

1 answer

2

I tried to modify the code to a minimum, I do not know if it's the idea you have in mind:

 void Guardar() {
    if (P < 5
            && !estaVacio(tfnombre.getText())) {
        Datos[P][0] = tfnombre.getText();
        Datos[P][1] = tfdireccion.getText();
        Datos[P][2] = tfcorreo.getText();
        P = P
                + 1;
    } else {
        mensaje = "Memoria llena.";
    }
    tfnombre.setText("");
    tfdireccion.setText("");
    tfcorreo.setText("");
}


void Mostrar(final String[][] a) {
    area.setText("");
    for (int i = 0; i < 5; i++) {

        if (!estaVacio(a[i][0])) {
            area.setText(area.getText()
                    + "Nombre: " + a[i][0] + "     Direccion: " + a[i][1] + "     Correo: " + a[i][2] + "\n");
        }
    }

}

private boolean estaVacio(final String nombre) {

    return nombre == null
            || nombre.isEmpty();

}

I have put the increase of P = P + 1 inside Save and I have deleted it from actionPerformed, so it does not increase if the name has not been entered. I hope it serves you.

    
answered by 04.05.2016 / 21:53
source