import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
public class CelsiusAFahrenheitGUI implements ActionListener {
JFrame converterFrame;
JPanel converterPanel;
JTextField tempCelsuis;
JLabel celsiusLabel, fahrenheitLabel;
JButton convertTemp;
public CelsiusAFahrenheitGUI() {
converterFrame = new JFrame("Conversor de Celsius a Fahrenheit");
converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
converterFrame.setSize(300,80);
converterFrame.setLocation(400,300);
converterPanel = new JPanel(new GridLayout(2,2));
addWidgets();
converterFrame.getRootPane().setDefaultButton(convertTemp);
converterFrame.getContentPane().add(converterPanel);
converterFrame.setResizable(false);
converterFrame.setDefaultLookAndFeelDecorated(true);
converterFrame.setVisible(true);
}
private void addWidgets(){
ImageIcon convertion = createImageIcon("color1.gif","Convertir temperatura");//en color1.gif pon la direccion de tu imagen a mostrar
tempCelsuis = new JTextField();
tempCelsuis.setText("37.0");
celsiusLabel = new JLabel("Celsius",SwingConstants.LEFT);
convertTemp = new JButton(convertion);
fahrenheitLabel = new JLabel("Fahrenheit",SwingConstants.LEFT);
celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
convertTemp.addActionListener(this);
tempCelsuis.addActionListener(this);
converterPanel.add(tempCelsuis);
converterPanel.add(celsiusLabel);
converterPanel.add(convertTemp);
converterPanel.add(fahrenheitLabel);
}
public void actionPerformed(ActionEvent e){
try{
int tempFar = (int)((Double.parseDouble(tempCelsuis.getText())) * 1.8 +32 );
if(tempFar <= 32 )
fahrenheitLabel.setText("<html><font Color=blue>"+tempFar+"°</font> Fahrenheit</html>");
else if(tempFar <= 80)
fahrenheitLabel.setText("<html><font Color=green>"+tempFar+"°</font> Fahrenheit</html>");
else
fahrenheitLabel.setText("<html><font Color=red>"+tempFar+"°</font> Fahrenheit</html>");
tempCelsuis.requestFocus();
}
catch(NumberFormatException se){
}
}
protected static ImageIcon createImageIcon(String path, String descripcion){
java.net.URL img = CelsiusAFahrenheitGUI.class.getResource(path);
if(img != null)
return new ImageIcon(img , descripcion);
else
System.err.println("No se encuentra la imagen: "+path);
return null;
}
public static void main (String[] args) {
CelsiusAFahrenheitGUI conv = new CelsiusAFahrenheitGUI();
}
}
No hay comentarios:
Publicar un comentario