Está en la página 1de 16

Programa 2.

Campos de texto
TextField
Interfaz
Implementar una GUI que ActionListener
permita ingresar el color
con el que se desea pintar
el panel en una caja de
texto
Cuando el Que
usuario teclea
datos en un implementa la
TextField o Tal evento es interfaz
JTextField procesado ActionListener
,JPasswordField, por un objeto (paquete
y despus java.awt.event)
presiona Intro, un ..
evento ocurre.
Mtodos de TextField
package camposcolor; PanelTexto.add(Cajacolor);

Panel Panelrojo=new Panel();


import java.awt.*;
Panelrojo.setBackground(Color.gray);
public class CamposColor { Panel Panelazul= new Panel();
public static void main(String[] Panel Panelverde=new Panel();

args) { Panelverde.setBackground(Color.gray);

GridLayout panelvar=new GridLayout(1,3);


Frame MiFrame = new Frame("Prueba
varios.setLayout(panelvar);
Campo de texto");
varios.add(Panelrojo);
Panel base=new Panel();
varios.add(Panelazul);
MiFrame.add(bas);
varios.add(Panelverde);
GridLayout paneles=new Cajacolor.addActionListener(new TextColor(Panelrojo));
GridLayout(2,1); Cajacolor.addActionListener(new TextColor(Panelazul));
base.setLayout(paneles); Cajacolor.addActionListener(new

Panel PanelTexto = new Panel(); TextColor(Panelverde));

Panel varios=new Panel(); MiFrame.setVisible(true);

base.add(PanelTexto); MiFrame.setSize(300,180);

}
base.add(varios);
}
TextField Cajacolor = new TextField(15);
package camposcolor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class TextColor implements ActionListener { El mtodo


private Panel panel; getSource()identifica
al componente que
public TextColor(Panel panel) {
genero el evento
this.panel = panel; }
@Override

public void actionPerformed(ActionEvent e) {


TextField fuente = (TextField) e.getSource();
El mtodo getText nos
String Seleccioncolor = fuente.getText();
permite recuperar el
if (Seleccioncolor.equals("Rojo")) { texto escrito en el
panel.setBackground(Color.red); componente.
} else if (Seleccioncolor.equals("Azul")) {
panel.setBackground(Color.blue);}
else if (Seleccioncolor.equals("Verde")) {
panel.setBackground(Color.green);
}
}
}
Campos de texto
Programa 3. TextField
Interfaz
ActionListener

Construir un conversor
de temperaturas
package conversiones;
import java.awt.*;
public class Conversiones {
public static void main(String[] args) {
Frame ventana = new Frame("Conversor de temperaturas");
// ventana.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
// Crear los componentes
Label etiqueta1 = new Label("temperatura C");
TextField campoDeTexto = new TextField(20);
TextField Resultado = new TextField(20);
Button botonF = new Button("F");
Button botonK=new Button ("K");
Label etiqueta2 = new Label("Pulse para obtener la temperatura" );
// Label resultado=new Label("La temperatura es");
Panel panelDeContenido = new Panel();
Panel centi=new Panel();
Panel ntemp=new Panel();
GridLayout lay1=new GridLayout (2,1);
GridLayout lay2=new GridLayout (1,5);
GridLayout lay3=new GridLayout ();
ventana.add(panelDeContenido);
panelDeContenido.setLayout(lay1);
centi.setLayout(lay2);
ntemp.setLayout(lay3);
panelDeContenido.add(centi);
panelDeContenido.add(ntemp);
centi.add(etiqueta1);
centi.add(campoDeTexto);
centi.add(botonF);
centi.add(botonK);
ntemp.add(Resultado);
centi.add(etiqueta2);
botonF.addActionListener(new temperaturas(campoDeTexto,Resultado
));
botonK.addActionListener(new Temperaturas(campoDeTexto,Resultado
));
ventana.setSize(300, 200);
ventana.setVisible(true);
}
}
package conversiones;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Temperaturas implements ActionListener {
TextField cTexto;
TextField res;
//Label etiqF;
public Temperaturas(TextField cTexto, TextField res){
this.cTexto=cTexto;
this.res=res;
}
@Override
public void actionPerformed(ActionEvent e) {
Button botones= (Button)e.getSource();
String temp=botones.getLabel();
if (temp.equals("F")) {
double centigrados = Double.parseDouble(cTexto.getText());
double faren = centigrados * 166.386;
res.setText( "la temperatura en F es"+faren);
} else
if (temp.equals("K")) {
double centigrados = Double.parseDouble(cTexto.getText());
double kel = centigrados * 30.787;
res.setText( "la temperatura en K es"+kel);
}
Interfaz
TextListener

TextField
Text Area

Programa 4
TextListener evento que indica que un objeto
texto por ejemplo
TextComponent ha cambiado
su texto.
addTextListener Mtodo para registrar el objeto
fuente con el objeto de evento
textValueChanged(TextEvent) Cuando el texto del
componente cambia el mtodo
textValueChanged es invocado
y se le suministra un TextEvent.
import java.awt.event.*;
class ConvierteDolares{
TextField tPesos,tDolares;
Frame v;
ManejaEventosTexto oyeTexto;
ManejaEventosBoton oyeBoton;
Button b;
class ManejaEventosTexto implements TextListener{
public void textValueChanged(TextEvent e){
Object fuente=e.getSource();
/*if (fuente==tPesos){ System.out.println(tPesos.getText() );}*/
}
}
import java.awt.event.*;
class ConvierteDolares{
TextField tPesos,tDolares;
Frame v;
ManejaEventosTexto oyeTexto;
ManejaEventosBoton oyeBoton;
Button b;
class ManejaEventosTexto implements TextListener{
public void textValueChanged(TextEvent e){
Object fuente=e.getSource();
/*if (fuente==tPesos){ System.out.println(tPesos.getText() );}*/
}
}
import java.awt.*;
class EventosTexto{

Grame v=new Frame();


v.setLayout(new FlowLayout() );
tf1 = new TextField();
b=new Button("Mostrar Texto");
areaTexto = new TextArea("",5, 40,TextArea.SCROLLBARS_NONE);
areaTexto.setBackground(Color.white);
oyeTexto=new ManejaEventosTexto();
oyeBoton=new ManejaEventosBoton();
tf1.addTextListener(oyeTexto);
areaTexto.addTextListener(oyeTexto);
b.addActionListener(oyeBoton);
v.add(tf1);
v.add(areaTexto);
v.add(b);
v.add(etiqueta1=new Label( ) ); v.add(etiqueta2=new Label( ) );
v.pack();
v.setVisible(true);
}
}
import java.awt.*;
class ManejaEventosTexto implements TextListener{

TextField tf1;
TextArea areaTexto;
Label etiqueta1, etiqueta2;

public ManejaEventosTexto (TextField tf1,TextArea areaTexto){


this. tf1=tf1;
this. areaTexto=areaTexto;
}

public void textValueChanged(TextEvent e){


Object fuente=e.getSource();
if (fuente==tf1){
String txt1=new String(tf1.getText() );
}
if (fuente==areaTexto){
String txt2=new String(areaTexto.getText() );
}
}

}
import java.awt.*;
Class ManejaEventosBoton implements ActionListener{

TextField tf1;
TextArea areaTexto;
Frame v;
String txt1,txt2;
Label etiqueta1, etiqueta2;
ManejaEventosTexto oyeTexto;
ManejaEventosBoton oyeBoton;
Button b;

public void actionPerformed(ActionEvent evento){


Object fuente=evento.getSource();
if(fuente==b)
etiqueta1.setText(txt1);
etiqueta2.setText(txt2);

}
}

También podría gustarte