Diferencia entre revisiones de «Usuario:ManuelRomero/practica 2»
De WikiEducator
| (2 revisiones intermedias por el mismo usuario no mostrado) | |||
| Línea 1: | Línea 1: | ||
| − | <source | + | =Gestor Eventos= |
| + | *La siguiente prácticacsc | ||
| + | <source lang=java> | ||
import javax.swing.JFrame; | import javax.swing.JFrame; | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
import javax.swing.JTextField; | import javax.swing.JTextField; | ||
import javax.swing.JPasswordField; | import javax.swing.JPasswordField; | ||
import javax.swing.JFormattedTextField; | import javax.swing.JFormattedTextField; | ||
| + | import javax.swing.text.MaskFormatter; | ||
| − | import java. | + | import java.text.ParseException; |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
import java.awt.FlowLayout; | import java.awt.FlowLayout; | ||
| Línea 92: | Línea 19: | ||
JFormattedTextField TFTexto ; | JFormattedTextField TFTexto ; | ||
JFormattedTextField TFNumero; | JFormattedTextField TFNumero; | ||
| + | JFormattedTextField TFDNI; | ||
CajaTexto(){ | CajaTexto(){ | ||
| Línea 111: | Línea 39: | ||
TFTexto =new JFormattedTextField(new String()); | TFTexto =new JFormattedTextField(new String()); | ||
TFTexto.setColumns(10); | 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 | + | import java.awt.FlowLayout; |
| − | + | ||
import javax.swing.JTextField; | import javax.swing.JTextField; | ||
| − | import javax.swing. | + | import javax.swing.JLabel; |
| − | import javax.swing. | + | 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 | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | public | + | //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 | + | import java.awt.event.ActionListener; |
| − | + | import java.awt.event.ActionEvent; | |
| + | import java.awt.FlowLayout; | ||
import javax.swing.JTextField; | import javax.swing.JTextField; | ||
| − | import javax.swing. | + | import javax.swing.JLabel; |
| − | import javax.swing. | + | 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 | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | public | + | //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"); | |
| − | setTitle("Mi Ventana "); | + | |
setBounds(200,200,400,400); | setBounds(200,200,400,400); | ||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
this.setVisible(true); | 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(){ | ||
add(TFNumero); | add(TFNumero); | ||
add(TFTexto); | add(TFTexto); | ||
| + | add(TFDNI); | ||
} | } | ||
| Línea 458: | Línea 175: | ||
} | } | ||
| − | |||
| + | </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.JTextField; | ||
| − | import javax.swing. | + | import javax.swing.JLabel; |
| − | import javax.swing. | + | 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 | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | public | + | //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 16:37 11 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(); } }