Foros del Web

Foros del Web (http://www.forosdelweb.com/)
-   Java (http://www.forosdelweb.com/f45/)
-   -   ayuda para este novato con textfield (http://www.forosdelweb.com/f45/ayuda-para-este-novato-con-textfield-428704/)

mveraa 25/09/2006 12:29

ayuda para este novato con textfield
 
hola maestros tengo este codigo en donde aprendo de apocos java , necesecito aprender a llenar una caja de texto. pero no me funciona , no me sale el cursor en la caja de texto para ingresar valores.
¿alguien me puede orientar?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class cinco
{
public static void main(String[] args)
{
JFrame frame=new JFrame();
JButton boton=new JButton("Un botó");
JTextField text=new JTextField();

frame.getContentPane().setLayout(new BorderLayout());
frame.add(boton, BorderLayout.SOUTH);
frame.add(text, BorderLayout.NORTH);




//escucha boton
boton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("me apretaron");

}
});


text.addKeyListener(new java.awt.event.KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
text.setText(e.getKeyChar() );

}
});






frame.pack();
frame.setVisible(true);




}





}


un saludo

mveraa 25/09/2006 12:36

me marca estos errores.

1. ERROR in cinco.java
(at line 43)
text.setText(e.getKeyChar() );
^^^^
Cannot refer to a non-final variable text inside an inner class defined in a different method
----------
2. ERROR in cinco.java
(at line 43)
text.setText(e.getKeyChar() );
^^^^^^^
The method setText(String) in the type JTextComponent is not applicable for the arguments (char)

eledil 25/09/2006 13:45

mmm bueno no soy tan bueno n java pero creo q el primer error q te da te dice q JTextField en este caso debe ser estar declarado como final, y el segundo te dice que l metodo de Jtext ".setText(String)" debe recibir como parametro un String y tu le estabas mandando un "char"...espero q te sirva, ...salu2

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class prueba
{
public static void main(String[] args)
{
JFrame frame=new JFrame();
final JButton boton=new JButton("Un botó");
final JTextField text=new JTextField();

frame.getContentPane().setLayout(new BorderLayout());
frame.add(boton, BorderLayout.SOUTH);
frame.add(text, BorderLayout.NORTH);
//escucha boton
boton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText("Me apretaron!!");
}
});
frame.pack();
frame.setVisible(true);
}
}

mveraa 25/09/2006 14:32

gracias por tu ayuda


La zona horaria es GMT -6. Ahora son las 07:01.

Desarrollado por vBulletin® Versión 3.8.7
Derechos de Autor ©2000 - 2026, Jelsoft Enterprises Ltd.