ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
package project1;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.BorderFactory;
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.JProgressBar;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class Frame1 extends JFrame {
private JLabel lblUsuario = new JLabel("Usuario :");
private JLabel lblContraseña = new JLabel("Contraseña :");
private JTextField txtUsuario = new JTextField();
private JPasswordField pswContraseña = new JPasswordField();
private JPanel pnlNumeros = new JPanel();
private JButton btn0 = new JButton();
private JButton btn1 = new JButton();
private JButton btn2 = new JButton();
private JButton btn3 = new JButton();
private JButton btn4 = new JButton();
private JButton btn5 = new JButton();
private JButton btn6 = new JButton();
private JButton btn7 = new JButton();
private JButton btn8 = new JButton();
private JButton btn9 = new JButton();
private JButton btnLimpiar = new JButton("Limpiar");
private JButton btnAceptar = new JButton("Aceptar");
private JButton btnCancelar = new JButton("Cancelar");
private JProgressBar pgbNivel = new
JProgressBar(JProgressBar.HORIZONTAL,1,4);
String strNumeros = "";
public Frame1() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout( null );
this.setSize( new Dimension(320, 190) );
lblUsuario.setBounds(new Rectangle(20,20,80,20));
lblContraseña.setBounds(new Rectangle(20,50,80,20));
txtUsuario.setBounds(new Rectangle(90,20,80,20));
pswContraseña.setBounds(new Rectangle(90,50,80,20));
pgbNivel.setBounds(new Rectangle(90,80,80,20));
btnAceptar.setBounds(new Rectangle(10,110,80,20));
btnCancelar.setBounds(new Rectangle(100,110,80,20));
pnlNumeros.setBounds(new Rectangle(190,20,90,120));
btn1.setBounds(new Rectangle(10,10,20,20));
btn2.setBounds(new Rectangle(35,10,20,20));
btn3.setBounds(new Rectangle(60,10,20,20));
btn4.setBounds(new Rectangle(10,35,20,20));
btn5.setBounds(new Rectangle(35,35,20,20));
btn6.setBounds(new Rectangle(60,35,20,20));
btn7.setBounds(new Rectangle(10,60,20,20));
btn8.setBounds(new Rectangle(35,60,20,20));
btn9.setBounds(new Rectangle(60,60,20,20));
btn0.setBounds(new Rectangle(10,85,20,20));
btnLimpiar.setBounds(new Rectangle(35,85,45,20));
pnlNumeros.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
pnlNumeros.setLayout(null);
pnlNumeros.add(btn1);
pnlNumeros.add(btn2);
pnlNumeros.add(btn3);
pnlNumeros.add(btn4);
pnlNumeros.add(btn5);
pnlNumeros.add(btn6);
pnlNumeros.add(btn7);
pnlNumeros.add(btn8);
pnlNumeros.add(btn9);
pnlNumeros.add(btn0);
pnlNumeros.add(btnLimpiar);
txtUsuario.addCaretListener(new CaretListener() { public void
caretUpdate(CaretEvent e) { ActivarAceptar_caretUpdate(e); } });
pswContraseña.addCaretListener(new CaretListener() { public void
caretUpdate(CaretEvent e) { ActivarAceptar_caretUpdate(e); } });
btnAceptar.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { btnAceptar_actionPerformed(e); } });
btnCancelar.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { btnCancelar_actionPerformed(e); } });
btnLimpiar.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { btnLimpiar_actionPerformed(e); } });
btn0.addActionListener(new ActionListener--es como pulsar un boton ()
{ public void actionPerformed(ActionEvent e) { Digitos(e); } });
btn1.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { Digitos(e); } });
btn2.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { Digitos(e); } });
btn3.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { Digitos(e); } });
btn4.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { Digitos(e); } });
btn5.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { Digitos(e); } });
btn6.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { Digitos(e); } });
btn7.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { Digitos(e); } });
btn8.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { Digitos(e); } });
btn9.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { Digitos(e); } });
this.getContentPane().add(lblUsuario,null);
this.getContentPane().add(lblContraseña,null);
this.getContentPane().add(txtUsuario,null);
this.getContentPane().add(pswContraseña,null);
this.getContentPane().add(pgbNivel,null);
this.getContentPane().add(btnAceptar,null);
this.getContentPane().add(btnCancelar,null);
this.getContentPane().add(pnlNumeros,null);
Diseño();
}
private void Diseño() {
btnAceptar.setMnemonic('a');
btnCancelar.setMnemonic('c');
btn0.setMargin(new Insets(0,0,0,0));
btn1.setMargin(new Insets(0,0,0,0));
btn2.setMargin(new Insets(0,0,0,0));
btn3.setMargin(new Insets(0,0,0,0));
btn4.setMargin(new Insets(0,0,0,0));
btn5.setMargin(new Insets(0,0,0,0));
btn6.setMargin(new Insets(0,0,0,0));
btn7.setMargin(new Insets(0,0,0,0));
btn8.setMargin(new Insets(0,0,0,0));
btn9.setMargin(new Insets(0,0,0,0));
btnLimpiar.setMargin(new Insets(0,0,0,0));
btnAceptar.setEnabled(false);
pswContraseña.setFocusable(false);
GenerarNumeros ( new JButton[]
{ btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0 } );
}
private void btnAceptar_actionPerformed(ActionEvent e) {
String strUsuario = txtUsuario.getText().trim().toLowerCase();
String strContraseña = new String ( pswContraseña.getPassword() );
JOptionPane.showMessageDialog(null, "Usuario " +
( strUsuario.equals("java") && strContraseña.equals("123") ? "" : "in" ) +
"válido" );
}
private void btnCancelar_actionPerformed(ActionEvent e) {
}
private void btnLimpiar_actionPerformed(ActionEvent e) {
pswContraseña.setText("");
pgbNivel.setValue(0);
pgbNivel.setStringPainted(false);
}
private void Digitos(ActionEvent e) {
String strContraseña = new String ( pswContraseña.getPassword() );
int Longitud = strContraseña.length()+1;
if ( Longitud < 5 ) {
pswContraseña.setText( strContraseña + ( (JButton)
e.getSource() ).getText() );
pgbNivel.setStringPainted(true);
pgbNivel.setValue( Longitud );
//pgbNivel.setString("" + ( Longitud*100/4 ) + " %" );
pgbNivel.setString( Longitud < 2 ? "Bajo" : Longitud < 4 ?
"Medio" : "Alto" );
}
}
private void ActivarAceptar_caretUpdate(CaretEvent e) {
btnAceptar.setEnabled( txtUsuario.getText().trim().toLowerCase().length() > 0 &&
new String( pswContraseña.getPassword() ).length() > 0 );
}
private void GenerarNumeros( JButton[] botones ) {
for (int i=0 ; i< botones.length; i++) {
String strNx = "" + new Random().nextInt(10);
while ( strNumeros.indexOf(strNx) > -1 )
strNx = "" + new Random().nextInt(10);
botones[i].setText(strNx);
strNumeros += strNx;
}
}
}

More Related Content

Login

  • 1. package project1; import java.awt.Dimension; import java.awt.Insets; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.BorderFactory; 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.JProgressBar; import javax.swing.JTextField; import javax.swing.border.BevelBorder; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; public class Frame1 extends JFrame { private JLabel lblUsuario = new JLabel("Usuario :"); private JLabel lblContraseña = new JLabel("Contraseña :"); private JTextField txtUsuario = new JTextField(); private JPasswordField pswContraseña = new JPasswordField(); private JPanel pnlNumeros = new JPanel(); private JButton btn0 = new JButton(); private JButton btn1 = new JButton(); private JButton btn2 = new JButton(); private JButton btn3 = new JButton(); private JButton btn4 = new JButton(); private JButton btn5 = new JButton(); private JButton btn6 = new JButton(); private JButton btn7 = new JButton(); private JButton btn8 = new JButton(); private JButton btn9 = new JButton(); private JButton btnLimpiar = new JButton("Limpiar"); private JButton btnAceptar = new JButton("Aceptar"); private JButton btnCancelar = new JButton("Cancelar"); private JProgressBar pgbNivel = new JProgressBar(JProgressBar.HORIZONTAL,1,4); String strNumeros = ""; public Frame1() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { this.getContentPane().setLayout( null ); this.setSize( new Dimension(320, 190) ); lblUsuario.setBounds(new Rectangle(20,20,80,20)); lblContraseña.setBounds(new Rectangle(20,50,80,20)); txtUsuario.setBounds(new Rectangle(90,20,80,20)); pswContraseña.setBounds(new Rectangle(90,50,80,20));
  • 2. pgbNivel.setBounds(new Rectangle(90,80,80,20)); btnAceptar.setBounds(new Rectangle(10,110,80,20)); btnCancelar.setBounds(new Rectangle(100,110,80,20)); pnlNumeros.setBounds(new Rectangle(190,20,90,120)); btn1.setBounds(new Rectangle(10,10,20,20)); btn2.setBounds(new Rectangle(35,10,20,20)); btn3.setBounds(new Rectangle(60,10,20,20)); btn4.setBounds(new Rectangle(10,35,20,20)); btn5.setBounds(new Rectangle(35,35,20,20)); btn6.setBounds(new Rectangle(60,35,20,20)); btn7.setBounds(new Rectangle(10,60,20,20)); btn8.setBounds(new Rectangle(35,60,20,20)); btn9.setBounds(new Rectangle(60,60,20,20)); btn0.setBounds(new Rectangle(10,85,20,20)); btnLimpiar.setBounds(new Rectangle(35,85,45,20)); pnlNumeros.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); pnlNumeros.setLayout(null); pnlNumeros.add(btn1); pnlNumeros.add(btn2); pnlNumeros.add(btn3); pnlNumeros.add(btn4); pnlNumeros.add(btn5); pnlNumeros.add(btn6); pnlNumeros.add(btn7); pnlNumeros.add(btn8); pnlNumeros.add(btn9); pnlNumeros.add(btn0); pnlNumeros.add(btnLimpiar); txtUsuario.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent e) { ActivarAceptar_caretUpdate(e); } }); pswContraseña.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent e) { ActivarAceptar_caretUpdate(e); } }); btnAceptar.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { btnAceptar_actionPerformed(e); } }); btnCancelar.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { btnCancelar_actionPerformed(e); } }); btnLimpiar.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { btnLimpiar_actionPerformed(e); } }); btn0.addActionListener(new ActionListener--es como pulsar un boton () { public void actionPerformed(ActionEvent e) { Digitos(e); } }); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Digitos(e); } }); btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Digitos(e); } }); btn3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Digitos(e); } }); btn4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Digitos(e); } }); btn5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Digitos(e); } }); btn6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Digitos(e); } }); btn7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Digitos(e); } }); btn8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Digitos(e); } }); btn9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Digitos(e); } }); this.getContentPane().add(lblUsuario,null); this.getContentPane().add(lblContraseña,null); this.getContentPane().add(txtUsuario,null);
  • 3. this.getContentPane().add(pswContraseña,null); this.getContentPane().add(pgbNivel,null); this.getContentPane().add(btnAceptar,null); this.getContentPane().add(btnCancelar,null); this.getContentPane().add(pnlNumeros,null); Diseño(); } private void Diseño() { btnAceptar.setMnemonic('a'); btnCancelar.setMnemonic('c'); btn0.setMargin(new Insets(0,0,0,0)); btn1.setMargin(new Insets(0,0,0,0)); btn2.setMargin(new Insets(0,0,0,0)); btn3.setMargin(new Insets(0,0,0,0)); btn4.setMargin(new Insets(0,0,0,0)); btn5.setMargin(new Insets(0,0,0,0)); btn6.setMargin(new Insets(0,0,0,0)); btn7.setMargin(new Insets(0,0,0,0)); btn8.setMargin(new Insets(0,0,0,0)); btn9.setMargin(new Insets(0,0,0,0)); btnLimpiar.setMargin(new Insets(0,0,0,0)); btnAceptar.setEnabled(false); pswContraseña.setFocusable(false); GenerarNumeros ( new JButton[] { btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0 } ); } private void btnAceptar_actionPerformed(ActionEvent e) { String strUsuario = txtUsuario.getText().trim().toLowerCase(); String strContraseña = new String ( pswContraseña.getPassword() ); JOptionPane.showMessageDialog(null, "Usuario " + ( strUsuario.equals("java") && strContraseña.equals("123") ? "" : "in" ) + "válido" ); } private void btnCancelar_actionPerformed(ActionEvent e) { } private void btnLimpiar_actionPerformed(ActionEvent e) { pswContraseña.setText(""); pgbNivel.setValue(0); pgbNivel.setStringPainted(false); } private void Digitos(ActionEvent e) { String strContraseña = new String ( pswContraseña.getPassword() ); int Longitud = strContraseña.length()+1; if ( Longitud < 5 ) { pswContraseña.setText( strContraseña + ( (JButton) e.getSource() ).getText() ); pgbNivel.setStringPainted(true); pgbNivel.setValue( Longitud ); //pgbNivel.setString("" + ( Longitud*100/4 ) + " %" ); pgbNivel.setString( Longitud < 2 ? "Bajo" : Longitud < 4 ? "Medio" : "Alto" ); } } private void ActivarAceptar_caretUpdate(CaretEvent e) { btnAceptar.setEnabled( txtUsuario.getText().trim().toLowerCase().length() > 0 && new String( pswContraseña.getPassword() ).length() > 0 );
  • 4. } private void GenerarNumeros( JButton[] botones ) { for (int i=0 ; i< botones.length; i++) { String strNx = "" + new Random().nextInt(10); while ( strNumeros.indexOf(strNx) > -1 ) strNx = "" + new Random().nextInt(10); botones[i].setText(strNx); strNumeros += strNx; } } }