Alguem sabe me dizer se em algum lugar do PJ tem algo falando como criar um controle de usuários, onde o usuário deve entrar com seu respectivo login?
e ai blz… se te ajudar vai um código
package frames;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import agenda.Conexao;
/**
* This code was generated using CloudGarden's Jigloo SWT/Swing GUI Builder,
* which is free for non-commercial use. If Jigloo is being used commercially
* (ie, by a corporation, company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo. Please visit
* www.cloudgarden.com for details. Use of Jigloo implies acceptance of these
* licensing terms. A COMMERCIAL LICENSE
* HAS NOT BEEN PURCHASED for this machine, so Jigloo or this code cannot be
* used legally for any corporate or commercial purpose.
*
*/
/**
* @author ricardo.oliveira
*
* Esta Classe vai fazer o controle de Login para acessar a Agenda
*/
public class FrmLogin extends JDialog {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 3977304330183258937L;
String url = "jdbc:mysql://127.0.0.1:3306/agendatelefone";
private Container container = null;
private JLabel lblFotoAgenda, lblDistincao, lblUsuarioSenhaInvalido;
public JLabel lblLogin, lblUsuario, lblSenha, help;
private JButton btnEntrar;
private JTextField nmUsuario;
private JPasswordField cdSenha;
public boolean naoConfirmouLogin = false;//confirma login
//private Conexao conexaoBD;
/**
* @param novo é um frame e é null
* @param modal é um boolean e é true
* Construtor com parametros para manter uma unica tela ativa
*/
public FrmLogin(JFrame novo, boolean modal) {
super(novo, "LOGIN", modal);
//conexaoBD = new Conexao(url);
container = getContentPane();
container.setLayout(null);
this.initGUI();
}
/**
* @return Um boolean por default
* Este método adiciona todos os componentes da tela
*/
private boolean initGUI() {
try {
{
container.setBackground(new java.awt.Color(0, 108, 217));
container.setVisible(true);
container.setForeground(new java.awt.Color(0, 128, 192));
container.setSize(311, 218);
lblLogin = new JLabel("LOGIN", SwingConstants.LEFT);
lblLogin.setHorizontalTextPosition(SwingConstants.LEFT);
lblLogin.setToolTipText("LOGIN");
lblLogin.setFont(new java.awt.Font("Serif", 1, 20));
lblLogin.setForeground(new java.awt.Color(180,0,0));
lblLogin.setBounds(68, 4, 168, 25);
container.add(lblLogin);
lblLogin.setHorizontalAlignment(SwingConstants.CENTER);
}
{
lblUsuario = new JLabel("Usuário", SwingConstants.LEFT);
lblUsuario.setHorizontalTextPosition(SwingConstants.LEFT);
lblUsuario.setFont(new java.awt.Font("Arial", 1, 14));
lblUsuario.setForeground(Color.BLACK);
lblUsuario.setBounds(24, 38, 55, 25);
container.add(lblUsuario);
lblUsuario.setHorizontalAlignment(SwingConstants.RIGHT);
}
{
lblSenha = new JLabel("senha", SwingConstants.LEFT);
lblSenha.setHorizontalTextPosition(SwingConstants.LEFT);
lblSenha.setFont(new java.awt.Font("Arial", 1, 14));
lblSenha.setForeground(Color.BLACK);
lblSenha.setBounds(24, 79, 45, 25);
container.add(lblSenha);
lblSenha.setText("Senha");
lblSenha.setHorizontalAlignment(SwingConstants.RIGHT);
}
{
cdSenha = new JPasswordField();
container.add(getCdSenha());
cdSenha.setBounds(73, 79, 80, 25);
cdSenha.setForeground(new java.awt.Color(61,108,156));
cdSenha.setFont(new java.awt.Font("Arial",1,14));
cdSenha.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
cdSenhaKeyPressed(evt);
}
});
}
{
nmUsuario = new JTextField();
container.add(getNmUsuario());
nmUsuario.setBounds(83, 38, 170, 25);
nmUsuario.setFont(new java.awt.Font("Arial",1,14));
nmUsuario.setForeground(new java.awt.Color(61,108,156));
nmUsuario.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
nmUsuarioKeyPressed(evt);
}
});
}
{
btnEntrar = new JButton();
container.add(getEntra());
btnEntrar.setBounds(213, 77, 40, 30);
//btnEntrar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("Imagens/logaLogin.JPG")));
btnEntrar.setHorizontalTextPosition(SwingConstants.CENTER);
btnEntrar.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
btnEntrar.setAlignmentY(0.0f);
btnEntrar.setContentAreaFilled(false);
btnEntrar.setToolTipText("Login");
// ação pelo enter
btnEntrar.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
btnEntrarKeyPressed(e);
}
});
// ação pelo mouse
btnEntrar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnentrarActionPerformed(evt);
}
});
}
{
lblUsuarioSenhaInvalido = new JLabel();
container.add(lblUsuarioSenhaInvalido);
lblUsuarioSenhaInvalido.setBounds(3, 111, 307, 20);
lblUsuarioSenhaInvalido.setHorizontalAlignment(SwingConstants.CENTER);
lblUsuarioSenhaInvalido.setFont(new java.awt.Font("Arial", 1, 14));
lblUsuarioSenhaInvalido.setForeground(new java.awt.Color(180,0,0));
}
{
lblDistincao = new JLabel();
container.add(lblDistincao);
lblDistincao.setText("");
lblDistincao.setBounds(3, 136, 307, 20);
lblDistincao.setHorizontalAlignment(SwingConstants.CENTER);
lblDistincao.setFont(new java.awt.Font("Arial",1,14));
lblDistincao.setForeground(new java.awt.Color(180,0,0));
}
/*{
lblFotoAgenda = new JLabel();
container.add(lblFotoAgenda);
lblFotoAgenda.setIcon(new ImageIcon(getClass().getClassLoader().getResource("Imagens/agenda.jpg")));
lblFotoAgenda.setBounds(49, 112, 221, 100);
}*/
{
this.setSize(320, 252);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int largura = this.getSize().width;
int altura = this.getSize().height;
int metadeLargura = (screen.width - largura) / 2;
int metadeAltura = (screen.height - altura) / 2;
this.setLocation(metadeLargura, metadeAltura);
setResizable(false);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"EXCEPTION AO CARREGAR TELA TODA ",
"Tela do Login",JOptionPane.WARNING_MESSAGE);
}
return true;
}
/**
* @param evt
* Botao Entrar confirmando senha e usuario
*/
//ação pelo mouse
private void btnentrarActionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(null,
"coloca aqui a regra!!!!! ",
"Tela do Login",JOptionPane.WARNING_MESSAGE);
/*if (this.buscaTela()) {
this.naoConfirmouLogin = true;
this.dispose();
setVisible(false);
} else {
this.limpaCampo();
this.nmUsuario.requestFocus();
this.lblUsuarioSenhaInvalido.setText("Usuário ou Senha Inválido");
this.lblDistincao.setText("Faz Distinção entre Maiúscula e minúscula");
}*/
}
/**
* @return Um boolean confirmando o login
* Pega usuário / senha do banco e verifica validação
*/
private boolean buscaTela() {
String usuarioTela = this.nmUsuario.getText();
String senhaTela = new String(this.cdSenha.getText());
ResultSet query = null;
if (usuarioTela.trim().length() > 0 && senhaTela.trim().length() > 0) {
StringBuffer pergunta = new StringBuffer();
pergunta.append("SELECT usuario, senha ");
pergunta.append("FROM login ");
pergunta.append("WHERE usuario = '"+usuarioTela+"' ");
pergunta.append("AND senha = '"+senhaTela+"'");
//query = conexaoBD.executeQuery(pergunta.toString());
String usuarioBD = "";
String senhaBD = "";
try {
while (query.next()) {
usuarioBD = query.getString("usuario");
senhaBD = query.getString("senha");
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "EXCEPTION ERRO NO BOTÃO QUE O LOGIN CHAMA ",
"Tela do Login",JOptionPane.WARNING_MESSAGE);
}
if(usuarioTela.compareTo(usuarioBD) == 0
&& senhaTela.compareTo(senhaBD) == 0){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
/**
* Limpa os campos da tela
*/
private void limpaCampo() {
this.nmUsuario.setText("");
this.cdSenha.setText("");
}
/**
* @param evt
* Vai limita o tamanho do campo Usuário
*/
private void nmUsuarioKeyPressed(KeyEvent evt) {
if(this.nmUsuario.getText().length() >= 12){
String sub = this.nmUsuario.getText().substring(0,12);
this.nmUsuario.setText(sub);
}
}
/**
* @param evt
* Vai limita o tamanho do campo Senha
*/
private void cdSenhaKeyPressed(KeyEvent evt) {
if(this.cdSenha.getText().toString().length() >= 8){
String sub = this.cdSenha.getText().toString().substring(0,8);
this.cdSenha.setText(sub);
}
}
/**
* @return cdSenha
*/
private JPasswordField getCdSenha() {
return cdSenha;
}
/**
* @return nmUsuario
*/
public JTextField getNmUsuario() {
return nmUsuario;
}
/**
* @return btnEntrar
*/
private JButton getEntra() {
return btnEntrar;
}
//ação pelo enter
private void btnEntrarKeyPressed(KeyEvent e) {
JOptionPane.showMessageDialog(null, "EXCEPTION ERRO NO BOTÃO QUE O LOGIN CHAMA ",
"Tela do Login",JOptionPane.WARNING_MESSAGE);
/*int valorBotao = e.getKeyCode();
int valorEnter = java.awt.event.KeyEvent.VK_ENTER;
if (valorBotao == valorEnter){
if (this.buscaTela()) {
this.naoConfirmouLogin = true;
this.dispose();
setVisible(false);
}
else {
this.limpaCampo();
this.nmUsuario.requestFocus();
this.lblUsuarioSenhaInvalido.setText("Usuário ou Senha Inválido");
this.lblDistincao.setText("Faz Distinção entre Maiúscula e minúscula");
}
}*/
}
public static void main(String [] ricardo){
FrmLogin fl = new FrmLogin(null, false);
fl.setSize(400,400);
fl.setVisible(true);
}
}
blz ai só faz a tua regra…é q este exemplo eu usava mysql…ve o q tu vai usar…valew…