Repetir el programa anterior, pero utilizando el gestor de diseño GridLayout, definiendo 2 filas y 2 columnas. En la primera fila se incluyen dos JTextField, y en la segunda dos JLabel. Mirar en la ayuda
el método setFont, y la clase Font.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package unidad4ejercicio5;
import java.awt.Font;
import java.awt.Image;
import java.text.DecimalFormat;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class VentanaExplica extends javax.swing.JFrame {
/**
* Creates new form VentanaExplica
*/
public VentanaExplica() {
initComponents();
txtEuros.setFont(this.getFuenteLetra());
ImageIcon logo=new ImageIcon(getClass().getResource("logocarballeira.png"));
//Image icono = logo.getImage().getScaledInstance(120, 120, Image.SCALE_DEFAULT);
//Image icono=logo.getImage();
txtEuros.setFont(this.getFuenteLetra());
this.setIconImage(logo.getImage());
this.setTitle("Conversor Euros/Pesetas");
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
txtEuros = new javax.swing.JTextField();
txtPesetas = new javax.swing.JTextField();
lblEuros = new javax.swing.JLabel();
lblPesetas = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(420, 300));
jPanel1.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
jPanel1ComponentResized(evt);
}
});
jPanel1.setLayout(new java.awt.GridLayout(2, 0));
txtEuros.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtEurosKeyReleased(evt);
}
});
jPanel1.add(txtEuros);
txtPesetas.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtPesetasKeyReleased(evt);
}
});
jPanel1.add(txtPesetas);
lblEuros.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblEuros.setText("Euros:");
jPanel1.add(lblEuros);
lblPesetas.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblPesetas.setText("Pesetas:");
jPanel1.add(lblPesetas);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
private void txtEurosKeyReleased(java.awt.event.KeyEvent evt) {
convAPesetas();
}
private void txtPesetasKeyReleased(java.awt.event.KeyEvent evt) {
convAEuros();
}
private void jPanel1ComponentResized(java.awt.event.ComponentEvent evt) {
txtEuros.setFont(this.getFuenteLetra());
repaint();
}
/**
* @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(VentanaExplica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(VentanaExplica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(VentanaExplica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(VentanaExplica.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 VentanaExplica().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel lblEuros;
private javax.swing.JLabel lblPesetas;
private javax.swing.JTextField txtEuros;
private javax.swing.JTextField txtPesetas;
// End of variables declaration
private void convAPesetas() {
double pesetas,euros;
DecimalFormat formato = new DecimalFormat("######.##");
if (!txtEuros.getText().trim().equals(""))
try {
euros = Double.parseDouble(txtEuros.getText());
pesetas=euros*166.386;
txtPesetas.setText(formato.format(pesetas));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Los datos introducidos no se pueden convertir a moneda", "Error en la conversión", JOptionPane.ERROR_MESSAGE);
txtEuros.setText("");
txtEuros.requestFocus();
}
}
private void convAEuros() {
double pesetas,euros;
DecimalFormat formato = new DecimalFormat("######.##");
if (!txtPesetas.getText().trim().equals(""))
try {
pesetas = Double.parseDouble(txtPesetas.getText());
euros=pesetas/166.386;
txtEuros.setText(String.valueOf(formato.format(euros)));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Los datos introducidos no se pueden convertir a moneda", "Error en la conversión", JOptionPane.ERROR_MESSAGE);
txtPesetas.setText("");
txtPesetas.requestFocus();
}
}
private Font getFuenteLetra(){
Font fuente;
int alto= txtEuros.getHeight();
fuente=new Font(Font.SANS_SERIF,Font.BOLD+Font.ITALIC,alto*2/7);
return fuente;
}
}
No hay comentarios:
Publicar un comentario