Olá, estou desenvolvendo um sistema em java que faça cadastros e tals. Criei uma tela de login e tals, mas aí começou os meus problemas. O usuário tem que digitar o seu login e também sua senha, mas quando eu clico no botão enviar, ele não tá executando os camandos certos, e exibe mensagem de erro que foi programada para mostrar somente se o login e a senha não estiverem certos. O problema é que ja testei para ser de outra forma, mas ele não está executando…
criei duas variaves, como segue abaixo:
String nome,senha;
No botão entrar, fiz os seguintes comandos:
nome=tflogin.getText();
senha=jpsenha.getText();
if ((nome=="ramon") && (senha=="oi")) {
Inicial.setVisible(true);
}else{
JOptionPane.showMessageDialog(null,"Não foi possível fazer o login");
}
porém, não está funcionando. Alguém sabe me dizer o porque e como solucionar o problema?
tente usar o método equals para strings o invés de “==”…
Ex:
nome.equals(“ramon”)
Seu código ficaria:
if ((nome.equals("ramon")) && (senha.equals("oi"))) {
Inicial.setVisible(true);
}else{
JOptionPane.showMessageDialog(null,"Não foi possível fazer o login");
}
Humm, valeu man, soh tem mais um problema…
ele não está reconhendo a linha Inicial.setVisible(true)
tipo, tah dizendo “non-static method setVisible(boolean) cannot be reference from a static context…”
e sei la, o resto ta tudo certo…
[quote=Ultralogic]Humm, valeu man, soh tem mais um problema…
ele não está reconhendo a linha Inicial.setVisible(true)
tipo, tah dizendo “non-static method setVisible(boolean) cannot be reference from a static context…”
e sei la, o resto ta tudo certo…[/quote]
Eu imagino q Inicial seja um JFrame, certo?
Se for isso, primeiro crie o objeto e aí chame o setVisible…
Ex:
Inicial inicial = new Inicial();
inicial.setVisible(true);
humm
sim, é um JFrame,
la no JFrame Inicial eu criei as linhas do
[quote]Inicial Inicial=new Inicial();[/quote] logo abaixo do 1° Public
blz, aí não deu erro, e na tela do Login criei a linha Inicial.setVisible(true);, como postei no código acima…
mas não está funcionando, ele não ta nem reconhecedo…
axei estranho, pq a base é a mesma para todas as telas, e tipo, se eu colocar o codigo
System.exit(0);
ele fecha a tela…
sei la cara, to na dúvida…
Olha cara fiz um projeto parecido:
[code] import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class FrameLogin extends FrameInicial{
PanelLogin pl;
public FrameLogin(Cofrinho cof){
setTitle("Tela de Login");
this.add(pl = new PanelLogin(this, cof));
this.pack();
this.add(pl);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setContentPane(pl);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
this.setSize(pl.getSize());
}
public void fechaJanela(){
this.setVisible(false);
this.dispose();
}
class PanelLogin extends PanelInicial{
JPasswordField pf;
JTextField tf;
JButton ok;
String nome;
String senha;
String s;
boolean statusNome;
boolean statusSenha;
private JFrame frame;
JLabel tx1;
JLabel pf1;
private Cofrinho c;
int count = 0;
public PanelLogin(JFrame f, Cofrinho cof){
super(f);
c = cof;
frame = f;
tx1 = new JLabel("Digite o nome");
tf = new JTextField("************");
tf.addActionListener(this);
pf1 = new JLabel("Digite a senha");
pf = new JPasswordField("*********");
pf.addActionListener(this);
ok = new JButton("Seguir");
ok.addActionListener(this);
JPanel buttonPanel = new JPanel(); //use FlowLayout
JPanel buttonPanel2 = new JPanel();
JPanel buttonPanel3 = new JPanel();
buttonPanel.add(tx1);
buttonPanel.add(tf);
buttonPanel2.add(pf1);
buttonPanel2.add(pf);
buttonPanel3.add(ok);
add(buttonPanel, BorderLayout.PAGE_START);
add(buttonPanel2, BorderLayout.CENTER);
add(buttonPanel3, BorderLayout.PAGE_END);
}
public boolean validaLogin(String login, String senha){
boolean usuario = false;
boolean adm = false;
if(c.getNome().equals(login) == true && c.getSenha().equals(senha)){
usuario = true;
}
if(login.equals(c.getNomeAdm()) == true && senha.equals(c.getSenhaAdm()) == true){
adm = true;
}
if(usuario == true | adm == true)
return true;
else
return false;
}
private void gravaObjeto (File f, Cofrinho o){
try {
FileOutputStream fos = new FileOutputStream (f);
ObjectOutputStream os = new ObjectOutputStream (fos);
os.writeObject (o);
os.close ();
}catch (IOException e) {
System.out.println ("Erro ao gravar objeto.");
}
}
public void actionPerformed(ActionEvent e) {
String resp;
boolean status;
if(c != null)
if(e.getSource() == ok){
if(c.getNome() != null && c.getSenha() != null && count < 2){
resp = new String(pf.getPassword());
status = this.validaLogin(tf.getText(), resp);
if(status == true){
FramePrincipal fp = new FramePrincipal(c);
this.setVisible(false); // Faz "desaparecer" essa janela
this.fecha();
}
else if(status == false){
JOptionPane.showMessageDialog(null, "Nome, ou senha Incorretos!", "Failed to Login!", JOptionPane.ERROR_MESSAGE);
count++;
}
}
else if(count >= 2){
JOptionPane.showMessageDialog(null, "O programa vai ser encerrado!", "Failed to Login!", JOptionPane.ERROR_MESSAGE);
c.setNome(c.getNomeAdm());
c.setSenha(c.getSenhaAdm());
File f = new File(caminho); // ver essa linha
this.gravaObjeto(f, c);
fecha();
System.exit(0);
}
else{
c = new Cofrinho();
resp = new String(tf.getText());
c.setNome(resp);
resp = new String(pf.getPassword());
c.setSenha(resp);
status = this.validaLogin(tf.getText(), resp);
if(status == true){
FramePrincipal fp = new FramePrincipal(c);
this.setVisible(false);
this.fecha();
}
}
}
}
public void fecha(){
frame.dispose();
}
}
}
[/code]
A classe "cuida" da janela que faz a parte do login
[quote=Ultralogic]humm
sim, é um JFrame,
la no JFrame Inicial eu criei as linhas do
Se vc pretende abrir a janela a partir da tela de login, vc deve criar o objeto da nova janela na tela de login.
Faça assim:
if ((nome.equals("ramon")) && (senha.equals("oi"))) {
Inicial inicial = new Inicial();
inicial.setVisible(true);
}else{
JOptionPane.showMessageDialog(null,"Não foi possível fazer o login");
}
[quote=Ultralogic]blz, aí não deu erro, e na tela do Login criei a linha Inicial.setVisible(true);, como postei no código acima…
mas não está funcionando, ele não ta nem reconhecedo…
axei estranho, pq a base é a mesma para todas as telas, e tipo, se eu colocar o codigo
System.exit(0);
ele fecha a tela…
sei la cara, to na dúvida…[/quote]
Na sua chamada em “Inicial.setVisible(true);”, vc está tentando chamar um método estático da classe Inicial e o método setVisible não é estático.
O System.exit(0); serve para terminar a execução do seu programa…