Oi pessoal,
Estou tentando validar uma simples tela de login/senha para depois acrescentar no meu projeto, mas o keyPressed não faz efeito, me ajudam …
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class LoginSwingEventoC extends JFrame implements ActionListener,
KeyListener, WindowListener {
JButton jbut1, jbut2, jbut3;
JTextField jtext1, jtext2;
JPasswordField password;
char charArray[];
LoginSwingEventoC()
{
JLabel jlbl1 = new JLabel("Nome: ");
JLabel jlbl2 = new JLabel("Senha: ");
jtext1 = new JTextField(20);
jtext2 = new JTextField(20);
jbut1 = new JButton("OK");
jbut2 = new JButton("LIMPAR");
jbut3 = new JButton("CANCEL");
password = new JPasswordField("1234");
charArray = new char[4];
jbut1.addActionListener(this);
jbut2.addActionListener(this);
jbut3.addActionListener(this);
password.addActionListener(this);
jtext2.keyListener(
new keyPressed(){
public void actionPerformed(KeyEvent e)
{
String psaux = new String(password.getPassword());
String s = new String("1234");
for(int i=0; i < psaux.length()-1; i++)
if(psaux.charAt(i)== s.charAt(i))
jtext2.setText("*");
}
});
JPanel jmain = new JPanel();
jmain.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
p1.setLayout(new GridLayout(2,1));
p1.add(jlbl1);
p1.add(jlbl2);
p2.setLayout(new GridLayout(2,1));
p2.add(jtext1);
p2.add(jtext2);
p3.setLayout(new FlowLayout(FlowLayout.CENTER));
p3.add(jbut1);
p3.add(jbut2);
p3.add(jbut3);
jmain.add("West", p1);
jmain.add("East", p2);
jmain.add("South", p3);
setContentPane(jmain);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent ev)
{
Object src = ev.getSource();
String r = "";
if(src==jbut1)
JOptionPane.showMessageDialog(null, password);
else if(src == jbut2)
JOptionPane.showMessageDialog(null, password);
else if(src == jbut3)
System.exit(0);
}
public void windowActivated(WindowEvent evt) {}
public void windowClosed(WindowEvent evt) {}
public void windowDeactivated(WindowEvent evt) {}
public void windowDeiconified(WindowEvent evt) {}
public void windowIconified(WindowEvent evt) {}
public void windowOpened(WindowEvent evt) {}
public void windowClosing(WindowEvent event) {
System.exit(0);
}
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {}
public void keyTyped(KeyEvent event) {}
public static void main (String args[])
{
LoginSwingEventoC app = new LoginSwingEventoC();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}