[JTextField] Como acessar os dados

1 resposta
R

Olá pessoal,

Estou iniciando em Java, e estou fazendo vários testes simples para fixar melhor a idéia da linguagem.

Gostaria de saber como acessar o conteúdo de "var" no JOptionPane no final do código. Seria usando métodos acessores?

import java.awt.*; //FlowLayout - ActionEvent 
import java.awt.event.*;//- ActionListener;
import javax.swing.*; // para JFrame e JOptionPane

public class GetInputFromGraphicJFrame extends JFrame {
	
	public GetInputFromGraphicJFrame(){
		JTextField txtNome = new JTextField(null, 40);// text field
		getContentPane().add(txtNome); // colocar no frame o campo
	    String var = (String)txtNome.getText();
		JButton btnEnviar = new JButton("Enviar");
		getContentPane().add(btnEnviar); // colocar no frame o botao
		MyActionListener listener = new MyActionListener(); // ação do botão 
		btnEnviar.addActionListener(listener); // ação

	}	

	public static void main(String[] args) {
		GetInputFromGraphicJFrame tela = new GetInputFromGraphicJFrame();//
		tela.setTitle("Coletor de texto");
		tela.setSize(600, 100);
		tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		tela.setLocationRelativeTo(null); //deve ser antes do setVisible
		tela.setVisible(true);
		tela.setLayout(new FlowLayout());//*posiciona os componentes um após o outro	
	}

	public class MyActionListener implements ActionListener {
		  public void actionPerformed(ActionEvent event) {
			  JOptionPane.showMessageDialog(null, var + " - é o que digitou.","AVISO", JOptionPane.PLAIN_MESSAGE);// mensagem
 
		  }
		}
}

1 Resposta

R

Resolvido.

import java.awt.*; //FlowLayout - ActionEvent 
import java.awt.event.*;//- ActionListener;
import javax.swing.*; // para JFrame e JOptionPane

public class GetInputFromGraphicJFrame extends JFrame {
	
	public GetInputFromGraphicJFrame(){
		final JTextField txtNome = new JTextField(null, 40);// text field
		getContentPane().add(txtNome); // colocar no frame o campo
	    String var = (String)txtNome.getText();
		JButton btnEnviar = new JButton("Enviar");
		getContentPane().add(btnEnviar); // colocar no frame o botao
		//MyActionListener listener = new MyActionListener(); // ação do botão 
//		btnEnviar.addActionListener(listener); // ação

	    btnEnviar.addActionListener(new ActionListener(){
	        public void actionPerformed(ActionEvent e){
	          String txt = txtNome.getText();
	          JOptionPane.showMessageDialog(null, "O texto digitado no JTextField é " + txt);
	        }
	    });

	}	

	public static void main(String[] args) {
		GetInputFromGraphicJFrame tela = new GetInputFromGraphicJFrame();//
		tela.setTitle("Coletor de texto");
		tela.setSize(600, 100);
		tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		tela.setLocationRelativeTo(null); //deve ser antes do setVisible
		tela.setVisible(true);
		tela.setLayout(new FlowLayout());//*posiciona os componentes um após o outro	
	}

	//public class MyActionListener implements ActionListener {
		//  public void actionPerformed(ActionEvent event) {
			//  JOptionPane.showMessageDialog(null, var + " - é o que digitou.","AVISO", JOptionPane.PLAIN_MESSAGE);// mensagem
 
		  //}
		//}
}
Criado 20 de maio de 2010
Ultima resposta 20 de mai. de 2010
Respostas 1
Participantes 1