laialii

Livro Java 7 Capitulo 8 Página 142 Ex 8.1

Mar 24th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package capitulo8;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. public class GuiLogin extends JFrame{
  6.     private JTextField tfLogin;
  7.     private JLabel lbSenha;
  8.     private JLabel lbLogin;
  9.     private JButton btLogar;
  10.     private JButton btCancelar;
  11.     private JPasswordField pfSenha;
  12.     private  static GuiLogin frame;
  13.  
  14.     public GuiLogin(){
  15.         inicializarComponentes();
  16.         definirEventos();
  17.     }
  18.    
  19.     private void inicializarComponentes(){
  20.         setTitle("Login no Sistema");
  21.         setBounds(0,0,250,200);
  22.         setLayout(null);
  23.         tfLogin = new JTextField(5);
  24.         pfSenha = new JPasswordField("Senha:");
  25.         lbSenha = new JLabel("Senha:");
  26.         lbLogin = new JLabel("Login:");
  27.         btLogar = new JButton("Logar");
  28.         btCancelar = new JButton("Cancelar");
  29.         tfLogin.setBounds(100, 30, 120, 25);
  30.         lbLogin.setBounds(30, 30, 80, 25);
  31.         lbSenha.setBounds(30, 75, 120, 25);
  32.         pfSenha.setBounds(100, 75, 120, 25);
  33.         btLogar.setBounds(20, 120, 100, 25);
  34.         btCancelar.setBounds(125, 120, 100, 25);
  35.         add(tfLogin);
  36.         add(lbSenha);
  37.         add(lbLogin);
  38.         add(pfSenha);
  39.         add(btLogar);
  40.         add(btCancelar);
  41.     }
  42.    
  43.     private void definirEventos(){
  44.         btLogar.addActionListener(new ActionListener(){
  45.             public void actionPerfomed(ActionEvent e){
  46.                 String senha = String.valueOf(pfSenha.getPassword());
  47.                 if(tfLogin.getText().equals("java7") && senha.equals("java7")){
  48.                     frame.setVisible(false);
  49.                     //GuiMenuPrincipal.abrir();
  50.                 }
  51.                 else
  52.                     JOptionPane.showMessageDialog(null,"Login ou Senha incorretas!");
  53.             }
  54.         });
  55.    
  56.     btCancelar.addActionListener(new ActionListener(){
  57.         public void actionPerformed(ActionEvent e){ System.exit(0);}
  58.     });
  59.     }
  60.  
  61.     public static void main(String args[]){
  62.         SwingUtilities.invokeLater(new Runnable(){
  63.             public void run(){
  64.                 frame = new GuiLogin();
  65.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66.                 Dimension tela = Toolkit.getDefaultToolkit().getScreenSize();
  67.                 frame.setLocation((tela.width - frame.getSize().width)/2,
  68.                     (tela.height - frame.getSize().height)/2);
  69.                 frame.setVisible(true);
  70.             }
  71.         });
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment