Diferencia entre revisiones de «Usuario:ManuelRomero/practica 2»

De WikiEducator
Saltar a: navegación, buscar
 
(Una revisión intermedia por el mismo usuario no mostrado)
Línea 1: Línea 1:
 +
=Gestor Eventos=
 +
*La siguiente prácticacsc
 
<source lang=java>
 
<source lang=java>
 
import javax.swing.JFrame;
 
import javax.swing.JFrame;
Línea 46: Línea 48:
 
}
 
}
 
 
TFTexto.setColumns(10);
+
TFTexto.setColumns(10);import java.awt.event.ActionListener;
 +
import java.awt.event.ActionEvent;
 +
import java.awt.FlowLayout;
 +
import javax.swing.JTextField;
 +
import javax.swing.JLabel;
 +
import javax.swing.JButton;
 +
 
 +
 
 +
import javax.swing.JFrame;
 +
 
 +
public class Eventos1 extends JFrame{
 +
JTextField t = new JTextField("0",10);
 +
JLabel l = new JLabel ("Número de clikcs");
 +
JButton b = new JButton("Sumar");
 +
JButton b2 = new JButton("Restar");
 +
ElEscuchador escuchador = new ElEscuchador();//creo un objeto de la clase escuchador
 +
//ElEscuchador escuchador2 = new ElEscuchador();//creo un objeto de la clase escuchador
 +
 +
//Creo la clase para escuhar que implementa el ActionListener
 +
class ElEscuchador implements ActionListener{
 +
public void actionPerformed(ActionEvent e){
 +
int a;
 +
a=Integer.parseInt(t.getText());
 +
if (e.getSource()==b){
 +
a++;
 +
}
 +
if (e.getSource()==b2){
 +
a--;
 +
}
 +
t.setText(String.valueOf(a));
 +
 
 +
}
 +
 +
}
 +
 +
 +
Eventos1(){
 +
b.addActionListener(escuchador);
 +
b2.addActionListener(escuchador);
 +
setLayout(new FlowLayout());
 +
insertaObjetos();
 +
setTitle("Mi Ventana BorderLayout");
 +
    setBounds(200,200,400,400);
 +
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 +
    this.setVisible(true);
 +
}
 +
private void insertaObjetos(){
 +
add(b);
 +
add(b2);
 +
add(l);
 +
add(t);
 +
 +
}
 +
public static void main(String []a){
 +
Eventos1 e = new Eventos1();
 +
}
 +
}
 +
import java.awt.event.ActionListener;
 +
import java.awt.event.ActionEvent;
 +
import java.awt.FlowLayout;
 +
import javax.swing.JTextField;
 +
import javax.swing.JLabel;
 +
import javax.swing.JButton;
 +
 
 +
 
 +
import javax.swing.JFrame;
 +
 
 +
public class Eventos1 extends JFrame{
 +
JTextField t = new JTextField("0",10);
 +
JLabel l = new JLabel ("Número de clikcs");
 +
JButton b = new JButton("Sumar");
 +
JButton b2 = new JButton("Restar");
 +
ElEscuchador escuchador = new ElEscuchador();//creo un objeto de la clase escuchador
 +
//ElEscuchador escuchador2 = new ElEscuchador();//creo un objeto de la clase escuchador
 +
 +
//Creo la clase para escuhar que implementa el ActionListener
 +
class ElEscuchador implements ActionListener{
 +
public void actionPerformed(ActionEvent e){
 +
int a;
 +
a=Integer.parseInt(t.getText());
 +
if (e.getSource()==b){
 +
a++;
 +
}
 +
if (e.getSource()==b2){
 +
a--;
 +
}
 +
t.setText(String.valueOf(a));
 +
 
 +
}
 +
 +
}
 +
 +
 +
Eventos1(){
 +
b.addActionListener(escuchador);
 +
b2.addActionListener(escuchador);
 +
setLayout(new FlowLayout());
 +
insertaObjetos();
 +
setTitle("Mi Ventana BorderLayout");
 +
    setBounds(200,200,400,400);
 +
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 +
    this.setVisible(true);
 +
}
 +
private void insertaObjetos(){
 +
add(b);
 +
add(b2);
 +
add(l);
 +
add(t);
 +
 +
}
 +
public static void main(String []a){
 +
Eventos1 e = new Eventos1();
 +
}
 +
}
 +
 
 
}
 
}
 
private void asignarObjetos(){
 
private void asignarObjetos(){
Línea 58: Línea 174:
 
}
 
}
  
 +
}
 +
 +
</source>
 +
<source lang=java>
 +
import java.awt.event.ActionListener;
 +
import java.awt.event.ActionEvent;
 +
import java.awt.FlowLayout;
 +
import javax.swing.JTextField;
 +
import javax.swing.JLabel;
 +
import javax.swing.JButton;
 +
 +
 +
import javax.swing.JFrame;
 +
 +
public class Eventos1 extends JFrame{
 +
JTextField t = new JTextField("0",10);
 +
JLabel l = new JLabel ("Número de clikcs");
 +
JButton b = new JButton("Sumar");
 +
JButton b2 = new JButton("Restar");
 +
ElEscuchador escuchador = new ElEscuchador();//creo un objeto de la clase escuchador
 +
//ElEscuchador escuchador2 = new ElEscuchador();//creo un objeto de la clase escuchador
 +
 +
//Creo la clase para escuhar que implementa el ActionListener
 +
class ElEscuchador implements ActionListener{
 +
public void actionPerformed(ActionEvent e){
 +
int a;
 +
a=Integer.parseInt(t.getText());
 +
if (e.getSource()==b){
 +
a++;
 +
}
 +
if (e.getSource()==b2){
 +
a--;
 +
}
 +
t.setText(String.valueOf(a));
 +
 +
}
 +
 +
}
 +
 +
 +
Eventos1(){
 +
b.addActionListener(escuchador);
 +
b2.addActionListener(escuchador);
 +
setLayout(new FlowLayout());
 +
insertaObjetos();
 +
setTitle("Mi Ventana BorderLayout");
 +
    setBounds(200,200,400,400);
 +
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 +
    this.setVisible(true);
 +
}
 +
private void insertaObjetos(){
 +
add(b);
 +
add(b2);
 +
add(l);
 +
add(t);
 +
 +
}
 +
public static void main(String []a){
 +
Eventos1 e = new Eventos1();
 +
}
 
}
 
}
  
 
</source>
 
</source>

Última revisión de 04:37 12 abr 2012

Gestor Eventos

  • La siguiente prácticacsc
import javax.swing.JFrame;
 
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
 
import java.text.ParseException;
 
 
import java.awt.FlowLayout;
 
public class CajaTexto extends JFrame{
	JTextField Texto = new JTextField(10);
	JPasswordField TPass = new JPasswordField(10);
	JFormattedTextField TFTexto ;
	JFormattedTextField TFNumero;
	JFormattedTextField TFDNI;
 
	CajaTexto(){
		//Asigno layout
 
		preparaObjetos();
		asignarObjetos();
 
		//Operaciones de visualizar la ventana
		setBounds(100,100,400,400);
		setTitle("Ventana de cajas de texto");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		setLayout(new FlowLayout());
	}
	private void preparaObjetos(){
		TFNumero= new JFormattedTextField(new Integer(0));
		TFNumero.setColumns(10);
		TFTexto =new JFormattedTextField(new String());
		TFTexto.setColumns(10);
		try{
		     MaskFormatter fDNI = new MaskFormatter("##.###.###-U");
		     fDNI.setPlaceholderCharacter('_');
		     TFDNI =new JFormattedTextField(fDNI);
		     TFDNI.setColumns(15);
		}catch(ParseException e){
 
		}
 
		TFTexto.setColumns(10);import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
 
 
import javax.swing.JFrame;
 
public class Eventos1 extends JFrame{
	JTextField t = new JTextField("0",10);
	JLabel l = new JLabel ("Número de clikcs");
	JButton b = new JButton("Sumar");
	JButton b2 = new JButton("Restar");
	ElEscuchador escuchador = new ElEscuchador();//creo un objeto de la clase escuchador
	//ElEscuchador escuchador2 = new ElEscuchador();//creo un objeto de la clase escuchador	
 
	//Creo la clase para escuhar que implementa el ActionListener
	class ElEscuchador implements ActionListener{
		public void actionPerformed(ActionEvent e){
			int a;
			a=Integer.parseInt(t.getText());
			if (e.getSource()==b){
				a++;
			}
			if (e.getSource()==b2){
				a--;
			}
			t.setText(String.valueOf(a));
 
		}
 
	}
 
 
	Eventos1(){
	 	 b.addActionListener(escuchador);
 	 	 b2.addActionListener(escuchador);
		 setLayout(new FlowLayout());
		 insertaObjetos();
		 setTitle("Mi Ventana BorderLayout");
	     setBounds(200,200,400,400);
	     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	     this.setVisible(true);
	}
	private void insertaObjetos(){
		add(b);
		add(b2);
		add(l);
		add(t);
 
	}
 public static void main(String []a){
	 Eventos1 e = new Eventos1();
 }
}
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
 
 
import javax.swing.JFrame;
 
public class Eventos1 extends JFrame{
	JTextField t = new JTextField("0",10);
	JLabel l = new JLabel ("Número de clikcs");
	JButton b = new JButton("Sumar");
	JButton b2 = new JButton("Restar");
	ElEscuchador escuchador = new ElEscuchador();//creo un objeto de la clase escuchador
	//ElEscuchador escuchador2 = new ElEscuchador();//creo un objeto de la clase escuchador	
 
	//Creo la clase para escuhar que implementa el ActionListener
	class ElEscuchador implements ActionListener{
		public void actionPerformed(ActionEvent e){
			int a;
			a=Integer.parseInt(t.getText());
			if (e.getSource()==b){
				a++;
			}
			if (e.getSource()==b2){
				a--;
			}
			t.setText(String.valueOf(a));
 
		}
 
	}
 
 
	Eventos1(){
	 	 b.addActionListener(escuchador);
 	 	 b2.addActionListener(escuchador);
		 setLayout(new FlowLayout());
		 insertaObjetos();
		 setTitle("Mi Ventana BorderLayout");
	     setBounds(200,200,400,400);
	     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	     this.setVisible(true);
	}
	private void insertaObjetos(){
		add(b);
		add(b2);
		add(l);
		add(t);
 
	}
 public static void main(String []a){
	 Eventos1 e = new Eventos1();
 }
}
 
	}
	private void asignarObjetos(){
		add(TFNumero);
		add(TFTexto);
		add(TFDNI);
	}
 
	public static void main(String []a){
		CajaTexto t = new CajaTexto();
	}
 
}
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
 
 
import javax.swing.JFrame;
 
public class Eventos1 extends JFrame{
	JTextField t = new JTextField("0",10);
	JLabel l = new JLabel ("Número de clikcs");
	JButton b = new JButton("Sumar");
	JButton b2 = new JButton("Restar");
	ElEscuchador escuchador = new ElEscuchador();//creo un objeto de la clase escuchador
	//ElEscuchador escuchador2 = new ElEscuchador();//creo un objeto de la clase escuchador	
 
	//Creo la clase para escuhar que implementa el ActionListener
	class ElEscuchador implements ActionListener{
		public void actionPerformed(ActionEvent e){
			int a;
			a=Integer.parseInt(t.getText());
			if (e.getSource()==b){
				a++;
			}
			if (e.getSource()==b2){
				a--;
			}
			t.setText(String.valueOf(a));
 
		}
 
	}
 
 
	Eventos1(){
	 	 b.addActionListener(escuchador);
 	 	 b2.addActionListener(escuchador);
		 setLayout(new FlowLayout());
		 insertaObjetos();
		 setTitle("Mi Ventana BorderLayout");
	     setBounds(200,200,400,400);
	     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	     this.setVisible(true);
	}
	private void insertaObjetos(){
		add(b);
		add(b2);
		add(l);
		add(t);
 
	}
 public static void main(String []a){
	 Eventos1 e = new Eventos1();
 }
}