Powered By Blogger

martes, 18 de mayo de 2010

Otro Ejemplo de Eventos en Java

Este ejemplo es un applet que permite realizar operaciones básica arisméticas una vez introducido números en cuadros de texto, tambien posee un conjunto de validaciones sobre éstas operaciones.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.applet.Applet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
*
* @author renan
*/
public class BotonesApplet extends Applet implements ActionListener{
JLabel etiqueta1,etiqueta2,etiqueta3;
JTextField campo1,campo2,campo3;
JButton suma_bot, resta_bot, multi_bot, divi_bot, mayor_bot, limp_bot, raiz1_bot, raiz2_bot;
float var,var2,var3;
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() {

etiqueta1 = new JLabel("Primer Número: ");
etiqueta2 = new JLabel("Segundo Número: ");
etiqueta3 = new JLabel("Resultado: ");

campo1 = new JTextField(10);
campo1.setHorizontalAlignment(JTextField.RIGHT);//para que los numero se escriban a la derecha
campo2 = new JTextField(10);
campo2.setHorizontalAlignment(JTextField.RIGHT);
campo3 = new JTextField(15);
campo3.setHorizontalAlignment(JTextField.RIGHT);
campo3.setEditable(false);

suma_bot = new JButton("Sumar");
resta_bot = new JButton("Restar");
multi_bot = new JButton("Multiplicar");
divi_bot = new JButton("Dividir");
raiz1_bot = new JButton("Raiz del primero");
raiz2_bot = new JButton("Raiz del segundo");
mayor_bot = new JButton("Mayor");
limp_bot = new JButton("Limpiar Todo");

add(etiqueta1);
add(campo1);
add(etiqueta2);
add(campo2);
add(suma_bot);
add(resta_bot);
add(multi_bot);
add(divi_bot);
add(raiz1_bot);
add(raiz2_bot);
add(mayor_bot);
add(limp_bot);
add(etiqueta3);
add(campo3);

suma_bot.addActionListener(this);
resta_bot.addActionListener(this);
multi_bot.addActionListener(this);
divi_bot.addActionListener(this);
limp_bot.addActionListener(this);
mayor_bot.addActionListener(this);
raiz1_bot.addActionListener(this);
raiz2_bot.addActionListener(this);
// TODO start asynchronous download of heavy resources

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == suma_bot)
{
if(verificar())
{
var2 = Float.valueOf(campo1.getText());
var3 = Float.valueOf(campo2.getText());
campo3.setText(String.valueOf(var2 + var3));
}
else
JOptionPane.showMessageDialog(this, "Error, alguno de los campos estan vacios");
}

if(e.getSource() == resta_bot)
{
if(verificar())
{
var2 = Float.valueOf(campo1.getText());
var3 = Float.valueOf(campo2.getText());
campo3.setText(String.valueOf(var2 - var3));
}
else
JOptionPane.showMessageDialog(this, "Error, alguno de los campos estan vacios");
}

if(e.getSource() == multi_bot)
{
if(verificar())
{
var2 = Float.valueOf(campo1.getText());
var3 = Float.valueOf(campo2.getText());
campo3.setText(String.valueOf(var2 * var3));
}
else
JOptionPane.showMessageDialog(this, "Error, alguno de los campos estan vacios");
}

if(e.getSource() == divi_bot)
{
if(verificar())
{
var2 = Float.valueOf(campo1.getText());
var3 = Float.valueOf(campo2.getText());
if(var3 == 0)
JOptionPane.showMessageDialog(this, "Error, no es posibe la división entre cero");
else
campo3.setText(String.valueOf(var2 / var3));
}
else
JOptionPane.showMessageDialog(this, "Error, alguno de los campos estan vacios");
}

if(e.getSource() == mayor_bot)
{
if(verificar())
{
var2 = Float.valueOf(campo1.getText());
var3 = Float.valueOf(campo2.getText());
if(var2 > var3)
campo3.setText(String.valueOf(var2));
if(var2 <>
campo3.setText(String.valueOf(var3));
if(var2 == var3)
campo3.setText("Son iguales");
}
else
JOptionPane.showMessageDialog(this, "Error, alguno de los campos estan vacios");

}

if(e.getSource() == raiz1_bot)
{
var = Float.valueOf(campo1.getText());
if(var<0) var =" Float.valueOf(campo2.getText());">

No hay comentarios:

Publicar un comentario

Escribe tu comentario aqui...