Está en la página 1de 9

Instituto Tecnológico de La Piedad

INGENIERIA EN SISTEMAS COMPUTACIONALES

ACTIVIDAD 2

LA GESTIÓN DE EVENTOS

UNIDAD 1

TÓPICOS AVANZADOS DE PROGRAMACIÓN

PRESENTADO POR:
NOMBRE(S): FABIAN SANCHEZ HERNANDEZ 19640234

LA PIEDAD, MICHOACÁN 15/MARZO/2021


Av. Tecnológico #2000. Meseta de los Laureles, C.P. 59310. Apartado Postal 22
La Piedad, Mich. Tel 01 (352) 52 62294, 52 62359, 52 60680,
www.tecnm│ www.itlapiedad.edu.mx
La Norma Mexicana NMX-R-025-SCFI, “Igualdad Laboral y No
Discriminación”. El número de registro: RPrIL-072 y fecha de
inicio: 2017-04-10 y término de la certificación 2021-04-10. 

LA GESTIÓN DE EVENTOS.
Desarrollo:

MiApplet

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MiApplet extends JFrame implements ActionListener {

String cadena = "";


int nVeces = 0;
JButton miBoton;
JLabel etiqueta;

public MiApplet() {
super("AppletViewer: MiApplet");
setLayout(new FlowLayout());
miBoton = new JButton("Boton 1");
etiqueta = new JLabel();
add(miBoton);
add(etiqueta);
miBoton.addActionListener(this);

@Override
public void actionPerformed(ActionEvent evento) {
nVeces++;
cadena = ("Usted ha presionado el boton " + nVeces + " veces");
etiqueta.setText(cadena);
}

BotonesMouseListener
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class BotonesMouseListener extends JFrame implements ActionListener {

String cadena = "";


int nVeces = 0;
JButton miBoton, miBoton2, miBoton3;
JLabel etiqueta;

public BotonesMouseListener() {
super("Home prueba");
//botón 1
setLayout(new FlowLayout());
miBoton = new JButton("SI");
add(miBoton);
miBoton.addActionListener(this);
System.out.println("");
//boton 2
miBoton2 = new JButton("NO");
add(miBoton2);
miBoton2.addActionListener(this);
System.out.println("");

//boton 3
miBoton3 = new JButton("OTRO");

add(miBoton3);
miBoton3.addActionListener(this);

System.out.println(" ");

//Etiqueta
etiqueta = new JLabel();
add(etiqueta);

@Override
public void actionPerformed(ActionEvent evento) {
if (evento.getSource() == miBoton) {

cadena = ("Ha pulsado el boton: SI");


etiqueta.setText(cadena);
}

if (evento.getSource() == miBoton2) {

cadena = ("Ha pulsado el boton: NO");


etiqueta.setText(cadena);
}

if (evento.getSource() == miBoton3) {
cadena = ("Ha pulsado el boton: OTRO");
etiqueta.setText(cadena);
}

BotonesMouseListenerV1

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class BotonesMouseListenerV1 extends JFrame implements ItemListener {

JLabel texto;
JCheckBox negritas, italica, rojo, arial15;

public BotonesMouseListenerV1() {
super("Prueba de JCheckBox");

setLayout(new FlowLayout());
texto = new JLabel("Estos son los CheckBox");
texto.setFont(new Font("Serif", Font.PLAIN, 14));
add(texto);

negritas = new JCheckBox("Negritas");


italica = new JCheckBox("Italica");
rojo = new JCheckBox("Rojo");
arial15 = new JCheckBox("Arial 15");
add(negritas);
add(italica);
add(rojo);
add(arial15);

negritas.addItemListener(this);
arial15.addItemListener(this);
italica.addItemListener(this);
rojo.addItemListener(this);
arial15.addItemListener(this);

@Override
public void itemStateChanged(ItemEvent event) {
Font f = null;
Color c = null;

if (negritas.isSelected() && italica.isSelected() && rojo.isSelected()) {


f = new Font("Serif", Font.BOLD + Font.ITALIC, 12);
c = Color.RED;

} else if (negritas.isSelected() && italica.isSelected()) {


f = new Font("Serif", Font.BOLD + Font.ITALIC, 14);

} else if (negritas.isSelected() && rojo.isSelected()) {


f = new Font("Serif", Font.BOLD, 14);
c = Color.RED;

} else if (italica.isSelected() && rojo.isSelected()) {


f = new Font("Serif", Font.ITALIC, 14);
c = Color.RED;

} else if (negritas.isSelected()) {
f = new Font("Serif", Font.BOLD, 14);

} else if (italica.isSelected()) {
f = new Font("Serif", Font.ITALIC, 14);
} else if (rojo.isSelected()) {
c = Color.RED;

} else if (arial15.isSelected()) {
f = new Font("Arial15", Font.ITALIC, 15);

texto.setFont(f);
texto.setForeground(c);

}
}

Test:

import javax.swing.JFrame;

public class test {


public static void main(String args[]) {
BotonesMouseListener pruebaMiBoton = new BotonesMouseListener();
pruebaMiBoton.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pruebaMiBoton.setSize(400,100);
pruebaMiBoton.setVisible(true);

Capturas
Conclusiones:
En conclusión este programa utilizamos todos los conocimientos antes vistos en clase
pero mejor implementados

También podría gustarte