-
-
Save parzibyte/7647abbe057799ecef51571674090567 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | |
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template | |
*/ | |
package me.parzibyte.traductorswing; | |
import java.util.HashMap; | |
import javax.swing.JOptionPane; | |
/** | |
* | |
* @author parzibyte | |
*/ | |
public class FramePrincipal extends javax.swing.JFrame { | |
/** | |
* Creates new form FramePrincipal | |
*/ | |
public FramePrincipal() { | |
initComponents(); | |
} | |
/** | |
* This method is called from within the constructor to initialize the form. | |
* WARNING: Do NOT modify this code. The content of this method is always | |
* regenerated by the Form Editor. | |
*/ | |
@SuppressWarnings("unchecked") | |
// <editor-fold defaultstate="collapsed" desc="Generated Code"> | |
private void initComponents() { | |
jTextFieldEntrada = new javax.swing.JTextField(); | |
jLabel1 = new javax.swing.JLabel(); | |
jButtonTraducir = new javax.swing.JButton(); | |
jLabelResultado = new javax.swing.JLabel(); | |
jLabel2 = new javax.swing.JLabel(); | |
jLabel3 = new javax.swing.JLabel(); | |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
jTextFieldEntrada.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jTextFieldEntradaActionPerformed(evt); | |
} | |
}); | |
jLabel1.setText("Ingresa el texto:"); | |
jLabel1.setAutoscrolls(true); | |
jButtonTraducir.setText("Traducir"); | |
jButtonTraducir.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonTraducirActionPerformed(evt); | |
} | |
}); | |
jLabel2.setText("Resultado:"); | |
jLabel3.setText("parzibyte.me"); | |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
getContentPane().setLayout(layout); | |
layout.setHorizontalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addContainerGap() | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) | |
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() | |
.addComponent(jLabel2) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addComponent(jLabelResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) | |
.addComponent(jTextFieldEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jButtonTraducir, javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))) | |
.addContainerGap(16, Short.MAX_VALUE)) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jLabel3) | |
.addGap(164, 164, 164)) | |
); | |
layout.setVerticalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addGap(14, 14, 14) | |
.addComponent(jLabel1) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addComponent(jTextFieldEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addComponent(jButtonTraducir) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) | |
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jLabelResultado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE) | |
.addComponent(jLabel3) | |
.addContainerGap()) | |
); | |
jLabel1.getAccessibleContext().setAccessibleName(""); | |
jLabel1.getAccessibleContext().setAccessibleDescription(""); | |
pack(); | |
}// </editor-fold> | |
private void jTextFieldEntradaActionPerformed(java.awt.event.ActionEvent evt) { | |
// TODO add your handling code here: | |
} | |
private void jButtonTraducirActionPerformed(java.awt.event.ActionEvent evt) { | |
String entrada = this.jTextFieldEntrada.getText(); | |
if (entrada.length() <= 0) { | |
JOptionPane.showMessageDialog(null, "No has ingresado nada para traducir"); | |
return; | |
} | |
HashMap<String, String> traducciones = new HashMap<>(); | |
// Lo único que aprendí en mi clase de inglés: | |
traducciones.put("hola", "hello"); | |
traducciones.put("perro", "dog"); | |
traducciones.put("gato", "cat"); | |
traducciones.put("¿Puedo ir al baño?", "May I go to the bathroom?"); | |
// Aquí más traducciones... | |
String traduccion = traducciones.get(entrada); | |
if (traduccion == null) { | |
this.jLabelResultado.setText("No encontrado"); | |
} else { | |
this.jLabelResultado.setText(traduccion); | |
} | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String args[]) { | |
/* Set the Nimbus look and feel */ | |
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> | |
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. | |
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html | |
*/ | |
try { | |
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { | |
if ("Nimbus".equals(info.getName())) { | |
javax.swing.UIManager.setLookAndFeel(info.getClassName()); | |
break; | |
} | |
} | |
} catch (ClassNotFoundException ex) { | |
java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (InstantiationException ex) { | |
java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (IllegalAccessException ex) { | |
java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} | |
//</editor-fold> | |
/* Create and display the form */ | |
java.awt.EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
new FramePrincipal().setVisible(true); | |
} | |
}); | |
} | |
// Variables declaration - do not modify | |
private javax.swing.JButton jButtonTraducir; | |
private javax.swing.JLabel jLabel1; | |
private javax.swing.JLabel jLabel2; | |
private javax.swing.JLabel jLabel3; | |
private javax.swing.JLabel jLabelResultado; | |
private javax.swing.JTextField jTextFieldEntrada; | |
// End of variables declaration | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment