Choice, Imágenes, Audio

martes, 6 de agosto de 2013


Choice
Choice Ayuda a utilizar menús de opciones, para facilitar la selección de una opción.
   Choice ch1, ch2; //declaro
      ch1 = new Choice(); // creo con el constructor Choice().
      ch2 = new Choice();
      ch1.add("elemento 1"); //añado un elemento
      ch1.add("elemento 2");
      ch2.add("elemento 1"); //añado un elemento
      ch2.add("elemento 2");
      ch1.addItemListener(this);
      ch2.addItemListener(this);

Al crear un objeto de este tipo se le añaden las opciones que se desea aparezcan en el menú, utilizando el método add() o addItem() y de esta manera se van añadiendo las opciones al menú. Para saber qué valor se seleccionó se utiliza el método getSelectedItem() o el método getSelectedIndex(), el primero toma el String del elemento seleccionado y el segundo te da el índice del elemento seleccionado. 

El paquete java.awt.event define varios tipos de eventos que son generados por varios elementos de interfase de usuario. 




EVENTO

Clase
de Listener

ActionEvent
Generado cuando un botón es seleccionado, una lista (vista mas adelante) tiene un doble clic o un menú es seleccionado
ActionListener
Define un método para recibir eventos de acción
ItemEvent
Generado cuando una caja de chequeo es
 seleccionada con el ratón.
ItemListener
Define un método para reconocer cuando el estado de un campo cambia
 




Poner una imagen en un Applet
Cuando se realiza un proyecto java, sabemos que se crean directorios automáticamente para almacenar los archivos .java , .class
/clase/bin/archivo.class
/clase/src/archivo.java

Las imágenes que se desee subir deben estar en el directorio /clase/bin/








Para Audio


import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Calculadora extends Applet implements ActionListener{

Button bsu, bres, bmul, bdiv, bc, bigual;
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, bpu, b0, bce; 
TextField  t1, t2, t4;
Float n, s; // variables GLOBALES para uso dentro de todo el applet
int operador; // variables para la comparcion

public void init(){
      resize(50,200);
}

public Calculadora() {
      t4 = new TextField("",10);
     b1  = new Button("1");
b2  = new Button("2");
b3  = new Button("3");
b4  = new Button("4");
b5  = new Button("5");
b6  = new Button("6");
b7  = new Button("7");
b8  = new Button("8");
b9  = new Button("9");
b0  = new Button("0");
bpu  = new Button(".");
bce  = new Button("CE");
      bsu = new Button("+");
      bres = new Button("-");
     bigual = new Button("=");
      bmul = new Button("*");
      bdiv = new Button("/");
      bc = new Button("C");
add(t4);
      add(b1);
add(b2);
 add(b3);
 add(b4);
 add(b5);
 add(b6);
 add(b7);
 add(b8);
 add(b9);
 add(b0);
 add(bpu);
 add(bce);
 add(bsu);
 add(bres);
 add(bigual);
 add(bmul);
 add(bdiv);
 add(bc);
      b1.addActionListener(this);
      b2.addActionListener(this);
      b3.addActionListener(this);
      b4.addActionListener(this);
      b5.addActionListener(this);
      b6.addActionListener(this);
      b7.addActionListener(this);
      b8.addActionListener(this);
      b9.addActionListener(this);
      bpu.addActionListener(this);
      b0.addActionListener(this);
      bce.addActionListener(this);
bsu.addActionListener(this);
      bres.addActionListener(this);
bigual.addActionListener(this);
       bmul.addActionListener(this);
       bdiv.addActionListener(this);
       bc.addActionListener(this);
    }

public void actionPerformed(ActionEvent ae) {

// para agregar el numero 1 al cuadro de texto
if (ae.getSource()== b1){ t4.setText(""+t4.getText()+"1");
}

if (ae.getSource()== b2){ // para agregar el numero 2 al cuadro de texto
t4.setText(""+t4.getText()+"2");
}

if (ae.getSource()== b3){ // para agregar el numero 3 en comillas al cuadro de texto
t4.setText(""+t4.getText()+"3");
}

if (ae.getSource()== b4){// para agregar el numero 4 en comillas al cuadro de texto
t4.setText(""+t4.getText()+"4");
}

if (ae.getSource()== b5){// para agregar el numero 5 en comillas al cuadro de texto
t4.setText(""+t4.getText()+"5");
}

if (ae.getSource()== b6){// para agregar el numero 6 en comillas al cuadro de texto
t4.setText(""+t4.getText()+"6");
}

if (ae.getSource()== b7){// para agregar el numero 7 en comillas al cuadro de texto
t4.setText(""+t4.getText()+"7");
}
if (ae.getSource()== b8){// para agregar el numero 8 en comillas al cuadro de texto
t4.setText(""+t4.getText()+"8");
}
if (ae.getSource()== b9){// para agregar el numero 9 al cuadro de texto
t4.setText(""+t4.getText()+"9");
}

if (ae.getSource()== b0){// para agregar el numero 0  al cuadro de texto
t4.setText(""+t4.getText()+"0");
}
// agrega un punto

if (ae.getSource()== bpu){
t4.setText(""+t4.getText()+"."); bpu.setEnabled(false); }
// desactiva el uso del punto

if (ae.getSource()== bce){
t4.setText("");
}



if (ae.getSource()== bsu){// suma
if(t4.getText()!=""){
n = Float.parseFloat(t4.getText());
operador = 1;
}
t4.setText("");
bpu.setEnabled(true); // activa el uso del punto
}

if (ae.getSource()== bres){ // resta
if(t4.getText()!=""){
n = Float.parseFloat(t4.getText());
operador =2;
}
t4.setText("");
bpu.setEnabled(true);
}

if (ae.getSource()== bmul){ // multiplica
if(t4.getText()!=""){
n = Float.parseFloat(t4.getText());
operador=3;
}
t4.setText("");
bpu.setEnabled(true);
}

if (ae.getSource()== bdiv){ // divide
if(t4.getText()!=""){
n = Float.parseFloat(t4.getText());
operador=4;
}
t4.setText("");
bpu.setEnabled(true);
}

if (ae.getSource()== bigual){ // Boton de igual

if(t4.getText()!="" && operador == 1){ // para sumar DEPENDE EL VALOR DE operador=1
s = Float.parseFloat(t4.getText());
t4.setText(""+(n+s));
bpu.setEnabled(true);
}

if(t4.getText()!="" && operador == 2){ // para restar
s = Float.parseFloat(t4.getText());
t4.setText(""+(n-s));
bpu.setEnabled(true);
}
if(t4.getText()!="" && operador == 3){ // para multiplicar
s = Float.parseFloat(t4.getText());
t4.setText(""+(n*s));
bpu.setEnabled(true);
}
if(t4.getText()!="" && operador == 4){ // para divdir
s = Float.parseFloat(t4.getText());
t4.setText(""+(n/s));
bpu.setEnabled(true);
}

}


if (ae.getSource()== bc){ // borrar todo
n = (Float) null; // anula el valor de la variable en este punto
t4.setText("");
}

}
}







0 comentarios:

Publicar un comentario