Duvidas com o JTextField

2 respostas
M

Olá a todos!

Gostaria de clicar em um botão e o conteúdo da caixa de texto mudasse, mas não está acontecendo nada… o que falta?

Olhem o codigo e vejam se está faltando algo…

package untitled2;

import java.awt.event.<em>;

import java.awt.AWTEvent.</em>;

import javax.swing.<em>;

import java.awt.</em>;

public class Application1 extends JFrame implements ActionListener{

JTextField texto = new JTextField(“Isto é uma caixa de texto”);

public void MudaTexto(){

texto.setText(“Mudou o texto”);

}
public static void main(String args[]){

JTextField texto = new JTextField();

JFrame frame = new JFrame(Meu frame);

frame.setSize(200,200);

JButton botao=new JButton(Mudar texto);

Container cp=frame.getContentPane() ;

cp.setLayout(null);

botao.setBounds(50,50,100,50);

texto.setText(Isto é uma caixa);

texto.setColumns(50);

texto.setBounds(20,20,150,20);
botao.addActionListener(new Application1());
cp.add(botao);
cp.add(texto);
frame.show();
}

public void actionPerformed(ActionEvent e){

if (e.getActionCommand()==Mudar texto)

MudaTexto();

}

}

Ficaria grato se alguém pudessem me ajudar…
ValeuZZZ!!!

2 Respostas

massardi

Tenta o seguinte

import java.awt.event.*; 
import java.awt.AWTEvent.*; 
import javax.swing.*; 
import java.awt.*; 
public class Application extends JFrame implements ActionListener&#123; 

JTextField texto = new JTextField&#40;"Isto é uma caixa de texto"&#41;; 
JButton botao=new JButton&#40;"Mudar texto"&#41;; 

public void MudaTexto&#40;&#41;&#123; 
texto.setText&#40;"Mudou o texto"&#41;; 

&#125; 

public Application&#40;&#41;&#123;
	botao.addActionListener&#40;this&#41;;
	
&#125;

public static void main&#40;String args&#91;&#93;&#41;&#123; 
Application ap = new Application&#40;&#41;;
JFrame frame = new JFrame&#40;"Meu frame"&#41;; 
frame.setSize&#40;200,200&#41;; 

Container cp=frame.getContentPane&#40;&#41; ; 
cp.setLayout&#40;null&#41;; 
ap.botao.setBounds&#40;50,50,100,50&#41;; 
ap.texto.setText&#40;"Isto é uma caixa"&#41;; 
ap.texto.setColumns&#40;50&#41;; 
ap.texto.setBounds&#40;20,20,150,20&#41;; 


cp.add&#40;ap.botao&#41;; 
cp.add&#40;ap.texto&#41;; 
frame.show&#40;&#41;; 

&#125; 
public void actionPerformed&#40;ActionEvent e&#41;&#123; 
if &#40;e.getActionCommand&#40;&#41;=="Mudar texto"&#41; 
MudaTexto&#40;&#41;; 
&#125; 
&#125;
M

Vou tentar o que vc propôs, qualquer dificuldade eu volto!!

ValeuZZZ!!!

Criado 26 de maio de 2003
Ultima resposta 27 de mai. de 2003
Respostas 2
Participantes 2