jueves, 17 de junio de 2010

PRACTICA 1: Botones



Este es mi primer programa en lenguaje Java.

Se trata de un codigo que muestra en pantalla una serie de botones, a cada boton le corresponde un numero del 1 al 9.
Cada boton realiza las siguientes acciones:
- Se le asigno un color a cada boton, al presionar determinado boton el recuadro al final cambia de acuerdo al color asignado.
- Cada boton tiene asignado un JLabel, el texto de la etiqueta muestra el nombre del numero en Frances.
- En la terminal se imprime el nombre del numero en Español.

A continuacion, el codigo Fuente:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Calculo implements ActionListener {
public void actionPerformed (ActionEvent e) {
String cmd = e.getActionCommand();
if(cmd.equals("uno")) {
System.out.print("uno\n");
this.miEtiqueta.setText("UN");
this.miPanel.setBackground(Color.RED);
}
if(cmd.equals("dos")) {
System.out.print("dos\n");
this.miEtiqueta.setText("DEUX");
this.miPanel.setBackground(Color.ORANGE);
}
if(cmd.equals("tres")) {
System.out.print("tres\n");
this.miEtiqueta.setText("TROIS");
this.miPanel.setBackground(Color.YELLOW);
}
if(cmd.equals("cuatro")) {
System.out.print("cuatro\n");
this.miEtiqueta.setText("QUATRE");
this.miPanel.setBackground(Color.GREEN);
}
if(cmd.equals("cinco")) {
System.out.print("cinco\n");
this.miEtiqueta.setText("CINQ");
this.miPanel.setBackground(Color.BLUE);
}
if(cmd.equals("seis")) {
System.out.print("seis\n");
this.miEtiqueta.setText("SIX");
this.miPanel.setBackground(Color.MAGENTA);
}
if(cmd.equals("siete")) {
System.out.print("siete\n");
this.miEtiqueta.setText("SEPT");
this.miPanel.setBackground(Color.BLACK);
}
if(cmd.equals("ocho")) {
System.out.print("ocho\n");
this.miPanel.setBackground(Color.GRAY);
this.miEtiqueta.setText("HUIT");
}
if(cmd.equals("nueve")) {
System.out.print("nueve\n");
this.miPanel.setBackground(Color.WHITE);
this.miEtiqueta.setText("NEUF");
}
if(cmd.equals("EX")) {
System.exit(1);
}
}

private JPanel miPanel;
private JLabel miEtiqueta;

public Calculo(JPanel p, JLabel l) {
this.miPanel = p;
this.miEtiqueta = l;
}

public static void main (String[] args) {
JFrame ventana = new JFrame();
ventana.setTitle ("Practica con Botones");
ventana.setSize (800,100);
ventana.setLocation (100,200);
ventana.setVisible (true);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel1 = new JPanel();
JPanel fondo = new JPanel();
panel1.setLayout (new GridLayout());
fondo.setLayout (new GridLayout());
panel1.setBackground(Color.WHITE);

JLabel aviso = new JLabel();
Calculo yo = new Calculo(panel1, aviso);

JButton b1 = new JButton("1");
b1.addActionListener(yo);
b1.setActionCommand("uno");
panel1.add(b1);

JButton b2 = new JButton("2");
b2.addActionListener(yo);
b2.setActionCommand("dos");
panel1.add(b2);

JButton b3 = new JButton("3");
b3.addActionListener(yo);
b3.setActionCommand("tres");
panel1.add(b3);

JButton b4 = new JButton("4");
b4.addActionListener(yo);
b4.setActionCommand("cuatro");
panel1.add(b4);

JButton b5 = new JButton("5");
b5.addActionListener(yo);
b5.setActionCommand("cinco");
panel1.add(b5);

JButton b6 = new JButton("6");
b6.addActionListener(yo);
b6.setActionCommand("seis");
panel1.add(b6);

JButton b7 = new JButton("7");
b7.addActionListener(yo);
b7.setActionCommand("siete");
panel1.add(b7);

JButton b8 = new JButton("8");
b8.addActionListener(yo);
b8.setActionCommand("ocho");
panel1.add(b8);

JButton b9 = new JButton("9");
b9.addActionListener(yo);
b9.setActionCommand("nueve");
panel1.add(b9);

JButton salir = new JButton("X");
salir.addActionListener(yo);
salir.setActionCommand("EX");
panel1.add(salir);

panel1.add(aviso);

fondo.add(panel1);
ventana.setContentPane(fondo);

return;
}
}


DESCARGA :Calculo.java

No hay comentarios:

Publicar un comentario