BotonContador.java
package paqueteprincipal;
import javax.swing.JButton;
public class BotonContador extends JButton {
//propiedades
int pulsaciones;
int pu;
//constructor
public BotonContador() {
pulsaciones=0;
pu=0;
}
//asigna una cantidad de pulsaciones
public void setPulsaciones(int p) {
pulsaciones=p;
}
public void setpu(int p) {
pu=p;
}
//devuelve las pulsaciones del botón
public int getPulsaciones() {
return pulsaciones;
}
public int getpu() {
return pu;
}
//incrementa en uno las pulsaciones
public void incrementa() {
pulsaciones++;
}
public void incrementa1() {
pu=pu+2;
}
//decrementa en uno las pulsaciones
public void decrementa() {
pulsaciones--;
}
//pone las pulsaciones a cero
public void reiniciar() {
pulsaciones=0;
}
public void reiniciar1() {
pu=0;
}
// public void iniciar() {
//
// pul=n;
//
// }
//aumenta las pulsaciones en una cantidad c
public void aumenta(int c) {
pulsaciones=pulsaciones+c;
}
public void aumenta1(int c) {
pu=pu+c;
}
//disminuye las pulsaciones en una cantidad c
public void disminuye(int c) {
pulsaciones=pulsaciones-c;
}
}
ventanaprincipal.java
package paqueteprincipal;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class ventanaprincipal extends javax.swing.JFrame {
BotonContador btnBotonA;
BotonContador btnBotonB;
BotonContador btnBotonC;
/**
* Creates new form ventanaprincipal
*/
public ventanaprincipal() {
initComponents();
CreacionVentana();
}
public void CreacionVentana(){
this.setTitle("Ejercicio de Herencia");
this.setSize(450,300);
btnBotonA= new BotonContador();
btnBotonA.setText("Botón A");
btnBotonA.setBounds(10,10,100,30);
this.getContentPane().add(btnBotonA);
btnBotonB= new BotonContador();
btnBotonB.setText("Botón B");
btnBotonB.setBounds(130,10,100,30);
this.getContentPane().add(btnBotonB);
btnBotonC= new BotonContador();
btnBotonC.setText("Botón C");
btnBotonC.setBounds(250,10,100,30);
this.getContentPane().add(btnBotonC);
btnBotonA.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
PulsacionBotonA(evt);
}
});
btnBotonB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
PulsacionBotonB(evt);
}});
btnBotonC.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
PulsacionBotonC(evt);
}});
}
private void PulsacionBotonA(ActionEvent evt) {
btnBotonA.incrementa();
}
private void PulsacionBotonB(ActionEvent evt) {
btnBotonB.incrementa();
}
private void PulsacionBotonC(ActionEvent evt) {
btnBotonC.incrementa1();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String info;
info="El botón A se ha pulsado "+btnBotonA.getPulsaciones()+"\n";
info=info+"El botón B se ha pulsado "+btnBotonB.getPulsaciones()+"\n";
info=info+"El botón C se ha pulsado "+btnBotonC.getpu()+"\n";
JOptionPane.showMessageDialog(null, info);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
btnBotonA.reiniciar();
btnBotonB.reiniciar();
btnBotonC.reiniciar1();
jTextField1.setText("");
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int n;
n=Integer.parseInt(jTextField1.getText());
btnBotonA.setPulsaciones(n);
btnBotonB.setPulsaciones(n);
btnBotonC.setpu(n);
JOptionPane.showMessageDialog(null, "El numero a iniciar es"+ n);
}
________________________________________________________________________________
Una vez terminado el código la ejecución será de la sgte manera:
import javax.swing.JButton;
public class BotonContador extends JButton {
//propiedades
int pulsaciones;
int pu;
//constructor
public BotonContador() {
pulsaciones=0;
pu=0;
}
//asigna una cantidad de pulsaciones
public void setPulsaciones(int p) {
pulsaciones=p;
}
public void setpu(int p) {
pu=p;
}
//devuelve las pulsaciones del botón
public int getPulsaciones() {
return pulsaciones;
}
public int getpu() {
return pu;
}
//incrementa en uno las pulsaciones
public void incrementa() {
pulsaciones++;
}
public void incrementa1() {
pu=pu+2;
}
//decrementa en uno las pulsaciones
public void decrementa() {
pulsaciones--;
}
//pone las pulsaciones a cero
public void reiniciar() {
pulsaciones=0;
}
public void reiniciar1() {
pu=0;
}
// public void iniciar() {
//
// pul=n;
//
// }
//aumenta las pulsaciones en una cantidad c
public void aumenta(int c) {
pulsaciones=pulsaciones+c;
}
public void aumenta1(int c) {
pu=pu+c;
}
//disminuye las pulsaciones en una cantidad c
public void disminuye(int c) {
pulsaciones=pulsaciones-c;
}
}
ventanaprincipal.java
package paqueteprincipal;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class ventanaprincipal extends javax.swing.JFrame {
BotonContador btnBotonA;
BotonContador btnBotonB;
BotonContador btnBotonC;
/**
* Creates new form ventanaprincipal
*/
public ventanaprincipal() {
initComponents();
CreacionVentana();
}
public void CreacionVentana(){
this.setTitle("Ejercicio de Herencia");
this.setSize(450,300);
btnBotonA= new BotonContador();
btnBotonA.setText("Botón A");
btnBotonA.setBounds(10,10,100,30);
this.getContentPane().add(btnBotonA);
btnBotonB= new BotonContador();
btnBotonB.setText("Botón B");
btnBotonB.setBounds(130,10,100,30);
this.getContentPane().add(btnBotonB);
btnBotonC= new BotonContador();
btnBotonC.setText("Botón C");
btnBotonC.setBounds(250,10,100,30);
this.getContentPane().add(btnBotonC);
btnBotonA.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
PulsacionBotonA(evt);
}
});
btnBotonB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
PulsacionBotonB(evt);
}});
btnBotonC.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
PulsacionBotonC(evt);
}});
}
private void PulsacionBotonA(ActionEvent evt) {
btnBotonA.incrementa();
}
private void PulsacionBotonB(ActionEvent evt) {
btnBotonB.incrementa();
}
private void PulsacionBotonC(ActionEvent evt) {
btnBotonC.incrementa1();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String info;
info="El botón A se ha pulsado "+btnBotonA.getPulsaciones()+"\n";
info=info+"El botón B se ha pulsado "+btnBotonB.getPulsaciones()+"\n";
info=info+"El botón C se ha pulsado "+btnBotonC.getpu()+"\n";
JOptionPane.showMessageDialog(null, info);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
btnBotonA.reiniciar();
btnBotonB.reiniciar();
btnBotonC.reiniciar1();
jTextField1.setText("");
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int n;
n=Integer.parseInt(jTextField1.getText());
btnBotonA.setPulsaciones(n);
btnBotonB.setPulsaciones(n);
btnBotonC.setpu(n);
JOptionPane.showMessageDialog(null, "El numero a iniciar es"+ n);
}
________________________________________________________________________________
Una vez terminado el código la ejecución será de la sgte manera:
Pulsar los Botones A, B, C (2 veces)
Ver pulsaciones
Ingresar un número en la caja de texto
Pulsar el botón iniciar
Ver Pulsaciones, notamos que todos iniciaron con el número ingresado(8)
Pulsar los Botones A, B, C (2 veces
c/u)
Ver Pulsaciones
Botón Reiniciar
Ver Pulsaciones
Disculpe, podría agregar la clase main? DD:
ResponderEliminar