Incluir 2 opciones
This commit is contained in:
parent
c80ffcc34e
commit
44069205d5
@ -2,6 +2,8 @@ package org.lapaloma.jjoo;
|
|||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
import java.awt.Component;
|
||||||
|
import java.awt.Container;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.FlowLayout;
|
import java.awt.FlowLayout;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
@ -10,10 +12,12 @@ import java.awt.datatransfer.Clipboard;
|
|||||||
import java.awt.datatransfer.StringSelection;
|
import java.awt.datatransfer.StringSelection;
|
||||||
|
|
||||||
import javax.swing.BorderFactory;
|
import javax.swing.BorderFactory;
|
||||||
|
import javax.swing.ButtonGroup;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JRadioButton;
|
||||||
import javax.swing.JScrollPane;
|
import javax.swing.JScrollPane;
|
||||||
import javax.swing.JTextArea;
|
import javax.swing.JTextArea;
|
||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
@ -21,41 +25,59 @@ import javax.swing.Timer;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* ClienteIAJuegosOlimpicos: Clase que realiza ....
|
* ClienteIAJuegosOlimpicos
|
||||||
*
|
*
|
||||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
* @author Isidoro
|
||||||
* @date 7 feb 2026
|
* @date 7 feb 2026
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class ClienteJuegosOlimpicosIA extends JFrame {
|
public class ClienteJuegosOlimpicosIA extends JFrame {
|
||||||
|
|
||||||
private static final long serialVersionUID = -6074564949314600423L;
|
private static final long serialVersionUID = -6074564949314600423L;
|
||||||
|
|
||||||
private JTextArea txtPeticion;
|
private JTextArea txtPeticion;
|
||||||
private JButton btnEnviar;
|
private JButton btnEnviar;
|
||||||
|
|
||||||
|
private JRadioButton rbOpcion1;
|
||||||
|
private JRadioButton rbOpcion2;
|
||||||
|
|
||||||
|
// Panel que se activa / desactiva según la opción
|
||||||
|
private JPanel panelContenido;
|
||||||
|
|
||||||
public ClienteJuegosOlimpicosIA() {
|
public ClienteJuegosOlimpicosIA() {
|
||||||
// Configuración básica de la ventana
|
|
||||||
|
// ================= CONFIGURACIÓN VENTANA =================
|
||||||
setTitle("Cliente IA - Acceso a Datos con IA (Groq)");
|
setTitle("Cliente IA - Acceso a Datos con IA (Groq)");
|
||||||
setSize(500, 300);
|
setSize(500, 300);
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
setLayout(new BorderLayout(10, 10));
|
setLayout(new BorderLayout(10, 10));
|
||||||
|
|
||||||
// Panel superior con FlowLayout alineado a la izquierda
|
// ================= PANEL NORTE =================
|
||||||
JPanel panelNorte = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
JPanel panelNorte = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
||||||
|
|
||||||
JLabel lblTitulo = new JLabel("Petición a procesar (sobre deportistas):");
|
JLabel lblTitulo = new JLabel("Petición a procesar (sobre deportistas):");
|
||||||
lblTitulo.setFont(new Font("SansSerif", Font.BOLD, 13));
|
lblTitulo.setFont(new Font("SansSerif", Font.BOLD, 13));
|
||||||
|
|
||||||
// Esto asegura que la etiqueta tenga espacio suficiente independientemente del
|
|
||||||
// layout
|
|
||||||
lblTitulo.setPreferredSize(new Dimension(300, 30));
|
lblTitulo.setPreferredSize(new Dimension(300, 30));
|
||||||
|
|
||||||
|
rbOpcion1 = new JRadioButton("Opción 1");
|
||||||
|
rbOpcion2 = new JRadioButton("Opción 2");
|
||||||
|
|
||||||
|
ButtonGroup grupo = new ButtonGroup();
|
||||||
|
grupo.add(rbOpcion1);
|
||||||
|
grupo.add(rbOpcion2);
|
||||||
|
|
||||||
|
rbOpcion1.setSelected(true);
|
||||||
|
|
||||||
panelNorte.add(lblTitulo);
|
panelNorte.add(lblTitulo);
|
||||||
|
panelNorte.add(rbOpcion1);
|
||||||
|
panelNorte.add(rbOpcion2);
|
||||||
|
|
||||||
add(panelNorte, BorderLayout.NORTH);
|
add(panelNorte, BorderLayout.NORTH);
|
||||||
|
|
||||||
// Caja de texto central (tipo chat)
|
// ================= PANEL CONTENIDO =================
|
||||||
|
panelContenido = new JPanel(new BorderLayout());
|
||||||
|
|
||||||
|
// Caja de texto central
|
||||||
txtPeticion = new JTextArea();
|
txtPeticion = new JTextArea();
|
||||||
txtPeticion.setLineWrap(true);
|
txtPeticion.setLineWrap(true);
|
||||||
txtPeticion.setWrapStyleWord(true);
|
txtPeticion.setWrapStyleWord(true);
|
||||||
@ -63,83 +85,98 @@ public class ClienteJuegosOlimpicosIA extends JFrame {
|
|||||||
JScrollPane scrollPane = new JScrollPane(txtPeticion);
|
JScrollPane scrollPane = new JScrollPane(txtPeticion);
|
||||||
scrollPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10),
|
scrollPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10),
|
||||||
BorderFactory.createLineBorder(Color.GRAY)));
|
BorderFactory.createLineBorder(Color.GRAY)));
|
||||||
add(scrollPane, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
|
// ---------- Panel ejemplo ----------
|
||||||
|
|
||||||
// Contenedor para la parte inferior del panel central
|
|
||||||
JPanel panelEjemplo = new JPanel(new BorderLayout());
|
JPanel panelEjemplo = new JPanel(new BorderLayout());
|
||||||
|
|
||||||
// Usamos JTextArea para que el texto pueda seleccionarse y copiarse
|
|
||||||
JTextArea txtEjemplo = new JTextArea(
|
JTextArea txtEjemplo = new JTextArea(
|
||||||
"Ejemplo: Obtén información completa, precisa y ordenada de atletas que participaron en juegos olímpicos de la siguiente disciplinas: Atletismo, natación y gimnasia. Serán 6 deportistas (3 hombres y 3 mujeres) por disciplina. Proporciona información de deportistas de distintos juegos olímpicos.");
|
"Ejemplo: Obtén información completa, precisa y ordenada de atletas que participaron en juegos olímpicos "
|
||||||
|
+ "de la siguiente disciplinas: Atletismo, natación y gimnasia. Serán 6 deportistas "
|
||||||
|
+ "(3 hombres y 3 mujeres) por disciplina. Proporciona información de deportistas "
|
||||||
|
+ "de distintos juegos olímpicos.");
|
||||||
|
|
||||||
txtEjemplo.setEditable(false); // No se puede borrar/escribir
|
txtEjemplo.setEditable(false);
|
||||||
txtEjemplo.setLineWrap(true);
|
txtEjemplo.setLineWrap(true);
|
||||||
txtEjemplo.setWrapStyleWord(true);
|
txtEjemplo.setWrapStyleWord(true);
|
||||||
txtEjemplo.setBackground(getBackground()); // Color igual al del panel
|
txtEjemplo.setBackground(getBackground());
|
||||||
txtEjemplo.setForeground(new Color(100, 100, 100));
|
txtEjemplo.setForeground(new Color(100, 100, 100));
|
||||||
txtEjemplo.setFont(new Font("SansSerif", Font.ITALIC, 11));
|
txtEjemplo.setFont(new Font("SansSerif", Font.ITALIC, 11));
|
||||||
txtEjemplo.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
|
txtEjemplo.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
|
||||||
|
|
||||||
// Botón de copiar pequeño
|
|
||||||
JButton btnCopiar = new JButton("📋"); // Puedes usar un icono .png si prefieres
|
|
||||||
btnCopiar.setToolTipText("Copiar texto al portapapeles");
|
|
||||||
btnCopiar.setPreferredSize(new Dimension(50, 30));
|
|
||||||
|
|
||||||
// Acción del botón
|
JButton btnCopiar = new JButton("📋");
|
||||||
|
btnCopiar.setPreferredSize(new Dimension(50, 30));
|
||||||
|
btnCopiar.setToolTipText("Copiar texto al portapapeles");
|
||||||
|
|
||||||
btnCopiar.addActionListener(e -> {
|
btnCopiar.addActionListener(e -> {
|
||||||
copiarAlPortapapeles(txtEjemplo.getText());
|
copiarAlPortapapeles(txtEjemplo.getText());
|
||||||
// Opcional: feedback visual rápido
|
btnCopiar.setText("✅");
|
||||||
btnCopiar.setText("✅");
|
new Timer(1500, ev -> btnCopiar.setText("📋")).start();
|
||||||
new Timer(1500, ev -> btnCopiar.setText("📋")).start();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Montaje
|
|
||||||
panelEjemplo.add(txtEjemplo, BorderLayout.CENTER);
|
panelEjemplo.add(txtEjemplo, BorderLayout.CENTER);
|
||||||
panelEjemplo.add(btnCopiar, BorderLayout.EAST); // Botón a la derecha
|
panelEjemplo.add(btnCopiar, BorderLayout.EAST);
|
||||||
|
|
||||||
// Añadir al panel central igual que antes
|
|
||||||
JPanel panelCentral = new JPanel(new BorderLayout());
|
JPanel panelCentral = new JPanel(new BorderLayout());
|
||||||
panelCentral.add(scrollPane, BorderLayout.CENTER);
|
panelCentral.add(scrollPane, BorderLayout.CENTER);
|
||||||
panelCentral.add(panelEjemplo, BorderLayout.SOUTH);
|
panelCentral.add(panelEjemplo, BorderLayout.SOUTH);
|
||||||
|
|
||||||
add(panelCentral, BorderLayout.CENTER);
|
panelContenido.add(panelCentral, BorderLayout.CENTER);
|
||||||
|
|
||||||
// Panel inferior para el botón de envío
|
add(panelContenido, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
// ================= PANEL SUR (BOTÓN COMÚN) =================
|
||||||
JPanel panelSur = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
JPanel panelSur = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
||||||
|
|
||||||
btnEnviar = new JButton("Enviar ➤");
|
btnEnviar = new JButton("Enviar ➤");
|
||||||
btnEnviar.setBackground(new Color(66, 133, 244));
|
btnEnviar.setBackground(new Color(66, 133, 244));
|
||||||
btnEnviar.setForeground(Color.WHITE);
|
btnEnviar.setForeground(Color.WHITE);
|
||||||
btnEnviar.setFocusPainted(false);
|
btnEnviar.setFocusPainted(false);
|
||||||
|
|
||||||
btnEnviar.addActionListener(e -> procesarPeticionCliente());
|
btnEnviar.addActionListener(e -> {
|
||||||
|
if (rbOpcion1.isSelected()) {
|
||||||
|
procesarOpcion1();
|
||||||
|
} else {
|
||||||
|
procesarOpcion2();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
panelSur.add(btnEnviar);
|
panelSur.add(btnEnviar);
|
||||||
add(panelSur, BorderLayout.SOUTH);
|
add(panelSur, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
// ================= ESTADO INICIAL =================
|
||||||
|
habilitarComponentes(panelContenido, false);
|
||||||
|
|
||||||
|
rbOpcion1.addActionListener(e -> habilitarComponentes(panelContenido, false));
|
||||||
|
rbOpcion2.addActionListener(e -> habilitarComponentes(panelContenido, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ================= MÉTODOS AUXILIARES =================
|
||||||
|
|
||||||
|
private void habilitarComponentes(Container container, boolean habilitar) {
|
||||||
|
for (Component c : container.getComponents()) {
|
||||||
|
c.setEnabled(habilitar);
|
||||||
|
if (c instanceof Container) {
|
||||||
|
habilitarComponentes((Container) c, habilitar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void copiarAlPortapapeles(String texto) {
|
private void copiarAlPortapapeles(String texto) {
|
||||||
StringSelection selection = new StringSelection(texto.replace("Ejemplo: ", ""));
|
StringSelection selection = new StringSelection(texto.replace("Ejemplo: ", ""));
|
||||||
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
||||||
clipboard.setContents(selection, selection);
|
clipboard.setContents(selection, selection);
|
||||||
}
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SwingUtilities.invokeLater(() -> {
|
|
||||||
new ClienteJuegosOlimpicosIA().setVisible(true);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void procesarPeticionCliente() {
|
private void procesarOpcion1() {
|
||||||
// TODO Auto-generated method stub
|
System.out.println("Procesando Opción 1 (sin contenido editable)");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void procesarOpcion2() {
|
||||||
String textoPrompt = txtPeticion.getText();
|
String textoPrompt = txtPeticion.getText();
|
||||||
|
System.out.println("Procesando Opción 2: " + textoPrompt);
|
||||||
System.out.println("Enviando petición: " + textoPrompt);
|
|
||||||
|
|
||||||
|
|
||||||
// Aquí se llamaría al método que hace la petición a la IA y procesa la respuesta
|
|
||||||
// Los parámetros de la petición (URL, API Key, modelo, contexto)
|
|
||||||
// están en el fichero de configuración conf.properties en la carpeta de recursos
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(() -> new ClienteJuegosOlimpicosIA().setVisible(true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user