> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud5-solucions/interfaces-graficas.md).

# Interfaces gráficas

1.

```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class VentanaPersonalizada extends JFrame {

    private JPanel panel;
    private JButton botonContador;
    private int contador = 0;

    public VentanaPersonalizada() {
        // Configuración da ventá
        setTitle("Ventana con Contador");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // Centrar a ventá

        // Crear panel
        panel = new JPanel();
        panel.setLayout(new FlowLayout());

        // Crear botón
        botonContador = new JButton("Contador");
        botonContador.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                contador++;
                System.out.println("Pulsacións: " + contador);
            }
        });

        // Engadir botón ao panel
        panel.add(botonContador);

        // Engadir panel á ventá
        add(panel);

        // Facer visible
        setVisible(true);
    }

}
```

```java
package Interfaces3;

public class App {
    public static void main(String[] args) {
        VentanaPersonalizada ventana = new VentanaPersonalizada();
    }
}

```

2\.

```java
package Interfaces3;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class VentanaPersonalizada extends JFrame {

    private JPanel panel;
    private JButton botonContador;
    private int contador = 0;

    public VentanaPersonalizada() {
        // Configuración da ventá
        setTitle("Ventana con Contador");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // Centrar a ventá
        Image i;
        try {
            i = ImageIO.read(new File("C:\\Users\\iagom\\Desktop\\programacion.png"));
            setIconImage(i);
        } catch (IOException e){
            System.out.println("Error al leer el Icono: " + e.getMessage());
        }

        // Crear panel
        panel = new JPanel();
        panel.setLayout(new FlowLayout());
        //Cambiamos el color a azul claro
        panel.setBackground(new Color(173, 216, 230));
        // Engadimos un borde ao panel
        panel.setBorder(new LineBorder(Color.GREEN, 3));

        // Crear botón
        botonContador = new JButton("Contador");
        botonContador.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                contador++;
                System.out.println("Pulsacións: " + contador);
            }
        });

        // Engadir botón ao panel
        panel.add(botonContador);

        // Engadir panel á ventá
        add(panel);

        // Facer visible
        setVisible(true);
    }

}
```

3\.

```java
package Interfaces3;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;

class PanelEtiqueta extends JPanel {

    private JLabel etiqueta;

    public PanelEtiqueta() {
        // Configuración do panel
        setLayout(new FlowLayout());
        setBackground(new Color(220, 220, 255)); // Cor azul clarita
        setBorder(new LineBorder(Color.BLUE, 2));

        // Crear etiqueta
        etiqueta = new JLabel("Pulsacións: 0");
        etiqueta.setFont(new Font("Arial", Font.BOLD, 16));
        add(etiqueta);
    }

    // Método para actualizar a etiqueta
    public void actualizarContador(int valor) {
        etiqueta.setText("Pulsacións: " + valor);
    }

    public JLabel getEtiqueta() {
        return etiqueta;
    }
}

```

```java
package Interfaces3;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class VentanaPersonalizada extends JFrame {

    private JPanel panel;
    private JButton botonContador;
    private PanelEtiqueta panelSuperior;
    private int contador = 0;

    public VentanaPersonalizada() {
        // Configuración da ventá
        setTitle("Ventana con Contador");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // Centrar a ventá
        Image i;
        try {
            i = ImageIO.read(new File("C:\\...\\programacion.png"));
            setIconImage(i);
        } catch (IOException e){
            System.out.println("Error al leer el Icono: " + e.getMessage());
        }

        // Crear panel
        panelSuperior = new PanelEtiqueta();
        add(panelSuperior, BorderLayout.NORTH);

        panel = new JPanel();
        panel.setLayout(new FlowLayout());
        //Cambiamos el color a azul claro
        panel.setBackground(new Color(173, 216, 230));
        // Engadimos un borde ao panel
        panel.setBorder(new LineBorder(Color.GREEN, 3));

        // Crear botón
        botonContador = new JButton("Contador");
        botonContador.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                contador++;
                System.out.println("Pulsacións: " + contador);
                panelSuperior.actualizarContador(contador);
            }
        });

        // Engadir botón ao panel
        panel.add(botonContador);

        // Engadir panel á ventá
        add(panel);

        // Facer visible
        setVisible(true);
    }

    public JPanel getPanel() {
        return panel;
    }

    public JButton getBotonContador() {
        return botonContador;
    }

    public PanelEtiqueta getPanelSuperior() {
        return panelSuperior;
    }

    public int getContador() {
        return contador;
    }
}

```

4\.

```java
package Interfaces3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class DialogoPersonalizado extends JDialog {

    private JTextField campoNome;
    private JRadioButton rbHome;
    private JRadioButton rbMuller;
    private JComboBox<String> comboTitulacion;

    public DialogoPersonalizado(JFrame parent) {
        super(parent, "Formulario", true); // true = modal

        setSize(300, 250);
        setLocationRelativeTo(parent);

        // 🔹 Layout recomendado
        setLayout(new GridLayout(4, 2, 5, 5));

        // --- Nome ---
        add(new JLabel("Nome e apelidos:"));
        campoNome = new JTextField();
        add(campoNome);

        // --- Sexo ---
        add(new JLabel("Sexo:"));
        JPanel panelRadio = new JPanel(new FlowLayout());

        rbHome = new JRadioButton("Home");
        rbMuller = new JRadioButton("Muller");

        ButtonGroup grupoSexo = new ButtonGroup();
        grupoSexo.add(rbHome);
        grupoSexo.add(rbMuller);

        panelRadio.add(rbHome);
        panelRadio.add(rbMuller);

        add(panelRadio);

        // --- Titulación ---
        add(new JLabel("Titulación:"));

        String[] titulacions = {"DAW", "DAM", "ASIR"};
        comboTitulacion = new JComboBox<>(titulacions);
        add(comboTitulacion);

        // Evento ao pechar o diálogo
        addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {

            }

            @Override
            public void windowClosing(WindowEvent e) {
                recuperarDatos();
            }

            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("Ventana cerrada");
            }

            @Override
            public void windowIconified(WindowEvent e) {

            }

            @Override
            public void windowDeiconified(WindowEvent e) {

            }

            @Override
            public void windowActivated(WindowEvent e) {

            }

            @Override
            public void windowDeactivated(WindowEvent e) {

            }
        });
    }

    private String recuperarDatos() {
        String nome = campoNome.getText();

        String sexo = "";
        if (rbHome.isSelected()) {
            sexo = "Home";
        } else if (rbMuller.isSelected()) {
            sexo = "Muller";
        }

        String titulacion = (String) comboTitulacion.getSelectedItem();

        return "Nome: " + nome + ", Sexo: " + sexo + ", Titulación: " + titulacion;

    }
}
```

```java
package Interfaces3;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class VentanaPersonalizada extends JFrame {

    private JPanel panel;
    private JButton botonContador;
    private PanelEtiqueta panelSuperior;
    private JButton botonNuevaVentana;
    private int contador = 0;

    public VentanaPersonalizada() {
        // Configuración da ventá
        setTitle("Ventana con Contador");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // Centrar a ventá
        Image i;
        try {
            i = ImageIO.read(new File("C:\\Users\\iagom\\Desktop\\programacion.png"));
            setIconImage(i);
        } catch (IOException e){
            System.out.println("Error al leer el Icono: " + e.getMessage());
        }

        // Crear panel
        panelSuperior = new PanelEtiqueta();
        add(panelSuperior, BorderLayout.NORTH);

        panel = new JPanel();
        panel.setLayout(new FlowLayout());
        //Cambiamos el color a azul claro
        panel.setBackground(new Color(173, 216, 230));
        // Engadimos un borde ao panel
        panel.setBorder(new LineBorder(Color.GREEN, 3));

        // Crear botón
        botonContador = new JButton("Contador");
        botonContador.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                contador++;
                System.out.println("Pulsacións: " + contador);
                panelSuperior.actualizarContador(contador);
            }
        });

        botonNuevaVentana = new JButton("Nueva Ventana");
        botonNuevaVentana.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DialogoPersonalizado dialogo = new DialogoPersonalizado(VentanaPersonalizada.this);
                dialogo.setVisible(true);
            }
        });

        // Engadir botón ao panel
        panel.add(botonContador);
        panel.add(botonNuevaVentana);

        // Engadir panel á ventá
        add(panel);

        // Facer visible
        setVisible(true);
    }

    public JPanel getPanel() {
        return panel;
    }

    public JButton getBotonContador() {
        return botonContador;
    }

    public PanelEtiqueta getPanelSuperior() {
        return panelSuperior;
    }

    public int getContador() {
        return contador;
    }
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/exercicios-java/ud5-solucions/interfaces-graficas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
