Dúvidas e 1 erro ao usar o JPasswordField.[RESOLVIDO]

8 respostas
DANIEL35

Olá amigos, estou tentando criar uma tela de login para meu trabalho da faculdade, mas travei em um erro que diz: cannot find symbol method getPassword(). Talvez não esteja encontrando o método... não sei o que é. Afinal alguém poderia me ajudar a resolver este problema?
Obrigado. Vejam o código:

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TelaLogar extends JFrame{
	private TratarBotoesLogin tbl;
	private JButton b1, b2;
	private JPasswordField senha;
	private JLabel l1;
	private JTextField c1;
	
	public TelaLogar(){
	setTitle("LOGIN");
	b1 = new JButton("OK");
	b2 = new JButton("CANCELAR");
	l1 = new JLabel("Digite a senha");
	c1 = new JTextField(6);
	senha = new JPasswordField();
	tbl = new TratarBotoesLogin(b1,b2);
	
	getContentPane().setLayout(new FlowLayout());
	getContentPane().setBackground(Color.black);
	getContentPane().add(l1);
	getContentPane().add(c1);
	getContentPane().add(b1);
	getContentPane().add(b2);
	b1.setForeground(Color.blue);
	b2.setForeground(Color.red);
	b1.addActionListener(tbl);
	b2.addActionListener(tbl);
	l1.setForeground(Color.white);
	
	setVisible(true);
	setSize(200,100);
	setLocationRelativeTo(null);
	
   }
	public static void main(String[] args){
		new TelaLogar();
	}
	
	public class TratarBotoesLogin implements ActionListener{
	private JButton b1, b2;
	public TratarBotoesLogin(JButton ba, JButton bb){
		b1 = ba;
		b2 = bb;
		}
			
	public void actionPerformed(ActionEvent acao){
		if(acao.getSource() == b1){
		String senha = String.valueOf(senha.getPassword());
		if (senha.compareTo("SENHA")==123) {
		JOptionPane.showMessageDialog(null,"Bom trabalho!!");
		}
		}else{
			System.out.println("ERRO");
			System.exit(0);
	   }	
    } 
  }
}

8 Respostas

remixlara

O JPasswordField possui o nome senha e a String que esta recebendo o getPassword(); possui o nome senha também. isso tá confundindo a classe, coloque outro nome e irá resolver seu problema.

DANIEL35
OK. Eu fiz o que você sugeriu e a mensagem de erro sumiu, porém o botão "OK" não está reconhecendo minha senha, que defini como: 123. O que está errado? Teria como dar mais essa força? Obrigado. Vejam o código:
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TelaLogar extends JFrame{
	private TratarBotoesLogin tbl;
	private JButton b1, b2;
	private JPasswordField id;
	private JLabel l1;
	private JTextField c1;
	
	public TelaLogar(){
	setTitle("LOGIN");
	b1 = new JButton("OK");
	b2 = new JButton("CANCELAR");
	l1 = new JLabel("Digite a senha");
	c1 = new JTextField(6);
	id = new JPasswordField();
	tbl = new TratarBotoesLogin(b1,b2);
	
	getContentPane().setLayout(new FlowLayout());
	getContentPane().setBackground(Color.black);
	getContentPane().add(l1);
	getContentPane().add(c1);
	getContentPane().add(b1);
	getContentPane().add(b2);
	b1.setForeground(Color.blue);
	b2.setForeground(Color.red);
	b1.addActionListener(tbl);
	b2.addActionListener(tbl);
	l1.setForeground(Color.white);
	
	setVisible(true);
	setSize(200,100);
	setLocationRelativeTo(null);
	
   }
	public static void main(String[] args){
		new TelaLogar();
	}
	
	public class TratarBotoesLogin implements ActionListener{
	private JButton b1, b2;
	public TratarBotoesLogin(JButton ba, JButton bb){
		b1 = ba;
		b2 = bb;
		}
			
	public void actionPerformed(ActionEvent acao){
		if(acao.getSource() == b1){
		String senha = String.valueOf(id.getPassword());
		if (senha.compareTo("SENHA")==123) {
		JOptionPane.showMessageDialog(null,"Bom trabalho!!");
		}
		}else{
			System.out.println("ERRO");
			System.exit(0);
	   }	
    } 
  }
}
remixlara

Você não adicionou o JPasswordField id na interface, você adicionou o JTextField(6) c1. Ai ele não vai possuir nada mesmo

remova esse JTextField c1 e adiciona o JPasswordField ai ele vai funcionar

DANIEL35
Então remixlara, fiz o que você sugeriu e ainda o botão "OK", não está funcionando. Veja o código:
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
//import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TelaLogar extends JFrame{
	private TratarBotoesLogin tbl;
	private JButton b1, b2;
	private JPasswordField id;
	private JLabel l1;
	private JPasswordField c1;
	
	public TelaLogar(){
	setTitle("LOGIN");
	b1 = new JButton("OK");
	b2 = new JButton("CANCELAR");
	l1 = new JLabel("Digite a senha");
	c1 = new JPasswordField(6);
	id = new JPasswordField();
	tbl = new TratarBotoesLogin(b1,b2);
	
	getContentPane().setLayout(new FlowLayout());
	getContentPane().setBackground(Color.black);
	getContentPane().add(l1);
	getContentPane().add(c1);
	getContentPane().add(b1);
	getContentPane().add(b2);
	b1.setForeground(Color.blue);
	b2.setForeground(Color.red);
	b1.addActionListener(tbl);
	b2.addActionListener(tbl);
	l1.setForeground(Color.white);
	
	setVisible(true);
	setSize(200,100);
	setLocationRelativeTo(null);
	
   }
	public static void main(String[] args){
		new TelaLogar();
	}
	
	public class TratarBotoesLogin implements ActionListener{
	private JButton b1, b2;
	public TratarBotoesLogin(JButton ba, JButton bb){
		b1 = ba;
		b2 = bb;
		}
			
	public void actionPerformed(ActionEvent acao){
		if(acao.getSource() == b1){
		String senha = String.valueOf(id.getPassword());
		if (senha.compareTo("SENHA")==123) {
		JOptionPane.showMessageDialog(null,"Bom trabalho!!");
		}
		}else{
			System.out.println("ERRO");
			System.exit(0);
	   }	
    } 
  }
}
remixlara

um, n tinha reparado mas na comparação você fez errado
vc pode fazer de duas formas:

onde se for igual a 0 eh porque são iguais e se for igual a 1 diferentes

if (senha.compareTo("123") == 0) { }

ou então

equals retorna true se forem iguais e false se não

if (senha.equals("123")) { }

DANIEL35

Infelizmente ainda não funcionou, tentei das duas formas. Veja como eu fiz:

........... ........ public void actionPerformed(ActionEvent acao){ if(acao.getSource() == b1){ String senha = String.valueOf(id.getPassword()); if (senha.equals("123")) { JOptionPane.showMessageDialog(null,"Bom trabalho!!"); } }else{ System.out.println("ERRO"); System.exit(0); } } } }

remixlara

ah certo, tipo cara, reorganize esse código que tá uma bagunça

pelo que eu vi ali você colocou com JPasswordField o c1 agora

então você tem que pegar o password de c1

a num ser que você adicione o id no frame...

mas quem tá no frame é c1 e não id

eu ajeitei aqui, olha ai

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TelaLogar extends JFrame{
	private TratarBotoesLogin tbl;
	private JButton b1, b2;
	private JPasswordField id;
	private JLabel l1;
	private JPasswordField c1;
	
	public TelaLogar(){
	setTitle("LOGIN");
	b1 = new JButton("OK");
	b2 = new JButton("CANCELAR");
	l1 = new JLabel("Digite a senha");
	c1 = new JPasswordField(6);
	id = new JPasswordField();
	tbl = new TratarBotoesLogin(b1,b2);
	id.setPreferredSize(new Dimension(100, 20));
	getContentPane().setLayout(new FlowLayout());
	getContentPane().setBackground(Color.black);
	getContentPane().add(l1);
	getContentPane().add(id);
	getContentPane().add(b1);
	getContentPane().add(b2);
	b1.setForeground(Color.blue);
	b2.setForeground(Color.red);
	b1.addActionListener(tbl);
	b2.addActionListener(tbl);
	l1.setForeground(Color.white);
	
	setVisible(true);
	setSize(200,100);
	setLocationRelativeTo(null);
	
   }
	public static void main(String[] args){
		new TelaLogar();
	}
	
	public class TratarBotoesLogin implements ActionListener{
	private JButton b1, b2;
	public TratarBotoesLogin(JButton ba, JButton bb){
		b1 = ba;
		b2 = bb;
		}
			
	public void actionPerformed(ActionEvent acao){
		if(acao.getSource() == b1){
		
		String senha = String.valueOf(id.getPassword());
		
		if (senha.compareTo("123") == 0) {
		JOptionPane.showMessageDialog(null,"Bom trabalho!!");
		}
		}else{
			System.out.println("ERRO");
			System.exit(0);
	   }	
    } 
  }
}
DANIEL35

Obrigado, amigo. Agora funcionou.

Criado 22 de maio de 2010
Ultima resposta 22 de mai. de 2010
Respostas 8
Participantes 2