Solución: Semana 3: Evidencia: Manejo de eventos - Parte I

Solución: Elabore un applet llamado BotonesApplet, el cual tome dos números, realizando las operaciones expresadas en los botones de la siguiente manera:

  • Suma el primer y el segundo número, dejando el valor como resultado.
  • Resta el segundo número del primero, dejando el valor como resultado.
  • Multiplica el primer y el segundo número, dejando el valor como resultado.
  • Divide el primer número entre el segundo, dejando el valor como resultado.
  • Deja la raíz del primer número como resultado.
  • Deja la raíz del segundo número como resultado.
  • Deja el valor mayor de ambos números como resultado.
  • Limpia el contenido de todos los campos.

El applet debe contar con la siguiente estructura:


 

import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;

/**
 *
 * @author gfrodriguez
 */
public class BotonesApplet extends javax.swing.JApplet implements ActionListener {

    JFrame frame = new JFrame();
    Panel panel1, panel2, panel3, panel4, panel5;
    GridLayout borderLayout = new GridLayout(5, 1);
    Label labelNumero1, labelNumero2, labelResultado;
    Button buttonSumar, buttonRestar, buttonMultiplicar, buttonDividir, buttonRaiz1, buttonRaiz2, buttonMayor, buttonLimpiar;
    TextField textFieldNumero1, textFieldNumero2, textFieldResultado;

    BotonesApplet() {

        frame.setTitle("BotonesApplet");
        frame.setLayout(borderLayout);

        labelNumero1 = new Label("Numero 1", Label.LEFT);
        labelNumero2 = new Label("Numero 2", Label.LEFT);
        labelResultado = new Label("Resultado", Label.LEFT);

        textFieldNumero1 = new TextField();
        textFieldNumero2 = new TextField();
        textFieldResultado = new TextField();

        buttonSumar = new Button("Suma");
        buttonRestar = new Button("Resta");
        buttonMultiplicar = new Button("Multiplica");
        buttonDividir = new Button("Divide");
        buttonRaiz1 = new Button("Raiz de 1");
        buttonRaiz2 = new Button("Raiz de 2");
        buttonMayor = new Button("Mayor");
        buttonLimpiar = new Button("Limpia");

        buttonSumar.addActionListener(this);
        buttonRestar.addActionListener(this);
        buttonMultiplicar.addActionListener(this);
        buttonDividir.addActionListener(this);
        buttonRaiz1.addActionListener(this);
        buttonRaiz2.addActionListener(this);
        buttonMayor.addActionListener(this);
        buttonLimpiar.addActionListener(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane());
        frame.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(44, 44, 44)
                                                .addComponent(buttonDividir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(buttonRaiz1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGap(29, 29, 29))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(buttonSumar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(buttonRestar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(buttonMultiplicar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(buttonRaiz2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(buttonMayor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(buttonLimpiar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(labelNumero1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(labelNumero2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(labelResultado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(textFieldNumero1)
                                                        .addComponent(textFieldNumero2)
                                                        .addComponent(textFieldResultado))))
                                .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(labelNumero1)
                                        .addComponent(textFieldNumero1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(labelNumero2)
                                        .addComponent(textFieldNumero2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(buttonSumar)
                                        .addComponent(buttonRestar)
                                        .addComponent(buttonMultiplicar))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(buttonDividir)
                                        .addComponent(buttonRaiz1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(buttonRaiz2)
                                        .addComponent(buttonMayor)
                                        .addComponent(buttonLimpiar))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(labelResultado)
                                        .addComponent(textFieldResultado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap())
        );

        frame.setSize(250, 270);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new BotonesApplet();
    }

    public void actionPerformed(ActionEvent e) {
        String numeroResultadoltado;
        double numero1, numero2, numeroResultado;

        if (e.getSource() == buttonSumar && !textFieldNumero2.getText().equals("") && !"".equals(textFieldNumero1.getText())) {
            numero1 = Double.parseDouble(textFieldNumero1.getText());
            numero2 = Double.parseDouble(textFieldNumero2.getText());
            numeroResultado = numero1 + numero2;
            textFieldResultado.setText(String.valueOf(numeroResultado));
        } else if (e.getSource() == buttonRestar && !textFieldNumero2.getText().equals("") && !"".equals(textFieldNumero1.getText())) {
            numero1 = Double.parseDouble(textFieldNumero1.getText());
            numero2 = Double.parseDouble(textFieldNumero2.getText());
            numeroResultado = numero1 - numero2;
            textFieldResultado.setText(String.valueOf(numeroResultado));
        } else if (e.getSource() == buttonMultiplicar && !textFieldNumero2.getText().equals("") && !"".equals(textFieldNumero1.getText())) {
            numero1 = Double.parseDouble(textFieldNumero1.getText());
            numero2 = Double.parseDouble(textFieldNumero2.getText());
            numeroResultado = numero1 * numero2;
            textFieldResultado.setText(String.valueOf(numeroResultado));
        } else if (e.getSource() == buttonDividir && !"".equals(textFieldNumero1.getText()) && !textFieldNumero2.getText().equals("")) {
            numero1 = Double.parseDouble(textFieldNumero1.getText());
            numero2 = Double.parseDouble(textFieldNumero2.getText());
            numeroResultado = numero1 / numero2;
            textFieldResultado.setText(String.valueOf(numeroResultado));
        } else if (e.getSource() == buttonRaiz1 && !"".equals(textFieldNumero1.getText())) {
            numero1 = Double.parseDouble(textFieldNumero1.getText());
            numeroResultado = Math.sqrt(numero1);
            textFieldResultado.setText(String.valueOf(numeroResultado));
        } else if (e.getSource() == buttonRaiz2 && !"".equals(textFieldNumero2.getText())) {
            numero2 = Double.parseDouble(textFieldNumero2.getText());
            numeroResultado = Math.sqrt(numero2);
            textFieldResultado.setText(String.valueOf(numeroResultado));
        } else if (e.getSource() == buttonMayor && !"".equals(textFieldNumero1.getText()) && !textFieldNumero2.getText().equals("")) {
            numero1 = Double.parseDouble(textFieldNumero1.getText());
            numero2 = Double.parseDouble(textFieldNumero2.getText());
            if (numero1 > numero2) {
                numeroResultado = numero1;
            } else {
                numeroResultado = numero2;
            }
            textFieldResultado.setText(String.valueOf(numeroResultado));
        } else if (e.getSource() == buttonLimpiar) {
            textFieldNumero1.setText("");
            textFieldNumero2.setText("");
            textFieldResultado.setText("");
        }
    }

}

0 comments :

Publicar un comentario