Import javax.swing.
*;
Import java.awt.*;
Import java.awt.event.*;
Import java.util.HashSet;
Import java.util.Set;
Public class InterfazNumeros extends JFrame {
Private JTextField inputField;
Private JButton submitButton;
Private JTextArea displayArea;
Private Set<Integer> numeros; // Usamos un Set para evitar números
repetidos
Public InterfazNumeros() {
// Configuración de la ventana principal
setTitle(“Introducción de Números”);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Inicializar los componentes
inputField = new JTextField();
submitButton = new JButton(“Ingresar Número”);
displayArea = new JTextArea();
displayArea.setEditable(false);
numeros = new HashSet<>();
// Panel de entrada
JPanel panel = new JPanel();
Panel.setLayout(new BorderLayout());
Panel.add(new JLabel(“Introduce un número entero: “),
BorderLayout.WEST);
Panel.add(inputField, BorderLayout.CENTER);
Panel.add(submitButton, BorderLayout.EAST);
// Añadir componentes a la ventana
Add(panel, BorderLayout.NORTH);
Add(new JScrollPane(displayArea), BorderLayout.CENTER);
// Acción del botón
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String texto = inputField.getText();
Try {
// Convertir el texto en un número entero
Int numero = Integer.parseInt(texto);
// Verificar si el número ya fue ingresado
If (numeros.contains(numero)) {
JOptionPane.showMessageDialog(null, “Este número ya
ha sido ingresado. Por favor, ingresa otro número.”);
} else {
// Si el número es válido, agregarlo al conjunto
Numeros.add(numero);
// Si se han ingresado 10 números, mostrar los números y
deshabilitar la entrada
If (numeros.size() == 10) {
displayNumbers();
inputField.setEnabled(false);
submitButton.setEnabled(false);
JOptionPane.showMessageDialog(null, “Has ingresado
todos los números.”);
} else {
displayArea.append(“Número ingresado: “ + numero +
“\n”);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, “Por favor, ingresa un
número entero válido.”);
inputField.setText(“”); // Limpiar el campo de texto
});
// Método para mostrar los números ingresados en el área de texto
Private void displayNumbers() {
displayArea.setText(“Los 10 números ingresados son:\n”);
for (int num : numeros) {
displayArea.append(num + “\n”);
}
Public static void main(String[] args) {
// Crear y mostrar la interfaz gráfica
SwingUtilities.invokeLater(new Runnable() {
Public void run() {
New InterfazNumeros().setVisible(true);
});