1
C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más / Java ventana para ingresar password por teclado o botones swing
« en: 25 de Noviembre 2016, 01:17 »
Hola !!
Alguien que me pueda ayudar con la siguiente duda
He creado una ventana en la que debo ingresar un password, mediante teclado o bien presionando los botones que he dispuesto para eso, ademas debo agregar el nombre de usuario... lo logre pero con un problema! si apretó el botón respectivo al nº1 por ejemplo queda registrado en el jTextField pero al querer apretar otro botón sobre escribe el valor en la posición que ya se había ocupado... por ende a menos que la clave solo tenga un dígito no me resulta como necesito.
les dejo el código a ver si me pueden ayudar a resolverlo
Alguien que me pueda ayudar con la siguiente duda
He creado una ventana en la que debo ingresar un password, mediante teclado o bien presionando los botones que he dispuesto para eso, ademas debo agregar el nombre de usuario... lo logre pero con un problema! si apretó el botón respectivo al nº1 por ejemplo queda registrado en el jTextField pero al querer apretar otro botón sobre escribe el valor en la posición que ya se había ocupado... por ende a menos que la clave solo tenga un dígito no me resulta como necesito.
les dejo el código a ver si me pueden ayudar a resolverlo
Código: [Seleccionar]
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Ventana_inicio extends JFrame implements ActionListener{
private JButton btnn0;
private JButton btnn1;
private JButton btnn2;
private JButton btnn3;
private JButton btnn4;
private JButton btnn5;
private JButton btnn6;
private JButton btnn7;
private JButton btnn8;
private JButton btnn9;
private JButton btnLimbiar;
private JButton btnIniciar;
private JButton btnCancelar;
private JLabel jlUsuario;
private JLabel jlContraseña;
private JTextField jtUsuario;
private JPasswordField jpfContraseña;
public Ventana_inicio(){
setSize(1000,700);
setTitle("Sistema de ventas");
initcomponents();
}
private void initcomponents() {
getContentPane().setLayout(null);
btnn1 = new JButton("1");
getContentPane().add(btnn1);
btnn1.setBounds(300, 300, 70, 70);
btnn1.addActionListener(this);
btnn2 = new JButton("2");
getContentPane().add(btnn2);
btnn2.setBounds(370, 300, 70, 70);
btnn2.addActionListener(this);
btnn3 = new JButton("3");
getContentPane().add(btnn3);
btnn3.setBounds(440, 300, 70, 70);
btnn3.addActionListener(this);
btnn4 = new JButton("4");
getContentPane().add(btnn4);
btnn4.setBounds(300, 370, 70, 70);
btnn4.addActionListener(this);
btnn5 = new JButton("5");
getContentPane().add(btnn5);
btnn5.setBounds(370, 370, 70, 70);
btnn5.addActionListener(this);
btnn6 = new JButton("6");
getContentPane().add(btnn6);
btnn6.setBounds(440, 370, 70, 70);
btnn6.addActionListener(this);
btnn7 = new JButton("7");
getContentPane().add(btnn7);
btnn7.setBounds(300, 440, 70, 70);
btnn7.addActionListener(this);
btnn8 = new JButton("8");
getContentPane().add(btnn8);
btnn8.setBounds(370, 440, 70, 70);
btnn8.addActionListener(this);
btnn9 = new JButton("9");
getContentPane().add(btnn9);
btnn9.setBounds(440, 440, 70, 70);
btnn9.addActionListener(this);
btnn0 = new JButton("0");
getContentPane().add(btnn0);
btnn0.setBounds(300, 510, 70, 70);
btnn0.addActionListener(this);
btnLimbiar = new JButton("Limpiar");
getContentPane().add(btnLimbiar);
btnLimbiar.setBounds(370, 510, 140, 70);
btnLimbiar.addActionListener(this);
jlUsuario = new JLabel("Usuario: ");
getContentPane().add(jlUsuario);
jlUsuario.setBounds(600, 300, 100, 20);
jtUsuario = new JTextField("");
getContentPane().add(jtUsuario);
jtUsuario.setBounds(700, 300, 140, 20);
jlContraseña = new JLabel("Contraseña: ");
getContentPane().add(jlContraseña);
jlContraseña.setBounds(600, 340, 100, 20);
jpfContraseña= new JPasswordField("");
getContentPane().add(jpfContraseña);
jpfContraseña.setBounds(700, 340, 140, 20);
btnIniciar = new JButton("Iniciar");
getContentPane().add(btnIniciar);
btnIniciar.setBounds(640, 450, 140, 140);
btnIniciar.addActionListener(this);
btnCancelar = new JButton("Cancelar");
getContentPane().add(btnCancelar);
btnCancelar.setBounds(780, 450, 140, 140);
btnCancelar.addActionListener(this);
}
public static void main(String[] args) {
new Ventana_inicio().setVisible(true);
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==btnn0){
jpfContraseña.setText("0");
}
if (e.getSource()==btnn1){
jpfContraseña.setText("1");
}
if (e.getSource()==btnn2){
jpfContraseña.setText("2");
}
if (e.getSource()==btnn3){
jpfContraseña.setText("3");
}
if (e.getSource()==btnn4){
jpfContraseña.setText("4");
}
if (e.getSource()==btnn5){
jpfContraseña.setText("5");
}
if (e.getSource()==btnn6){
jpfContraseña.setText("6");
}
if (e.getSource()==btnn7){
jpfContraseña.setText("7");
}
if (e.getSource()==btnn8){
jpfContraseña.setText("8");
}
if (e.getSource()==btnn9){
jpfContraseña.setText("9");
}
if (e.getSource()==btnLimbiar){
jpfContraseña.setText("");
jtUsuario.setText("");
}
if(e.getSource()==btnIniciar){
if( jtUsuario.getText().equals("juan") && jpfContraseña.getText().equals("1") ){
Ventana_intermedia ventana = new Ventana_intermedia ();
ventana.setVisible(true);
}
else{
if( !jtUsuario.getText().equals("admin") )
JOptionPane.showMessageDialog(null,"USUARIO INCORRECTO");
else
if( !jpfContraseña.getText().equals("1") )
JOptionPane.showMessageDialog(null,"PASSWORD INCORRECTO");
}
}
if(e.getSource()==btnCancelar){
System.exit(0);
}
}
}