Galera, estava aprendendo um pouco de Swing aqui e encontrei na net ensinando a criar uma tela de login, eu achei muito complicado pra falar a verdade e acredito que tenha como fazer de uma forma mais simples. Vou lançar meu código e depois as dúvidas.
package Login;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Login extends JFrame {
private JLabel label1;
private JLabel label2;
private JTextField campoTexto1;
private JPasswordField campoSenha1;
private JButton botao1;
private JPanel painel;
public Login() {
super();
create();
this.setVisible(true);
}
private void create() {
label1 = new JLabel();
label2 = new JLabel();
campoTexto1 = new JTextField();
campoSenha1 = new JPasswordField();
botao1 = new JButton();
painel = new JPanel();
// label1
label1.setHorizontalAlignment(SwingConstants.LEFT);
label1.setForeground(new Color(0, 0, 255));
label1.setText("Usuário");
// label2
label2.setHorizontalAlignment(SwingConstants.LEFT);
label2.setForeground(new Color(0, 0, 255));
label2.setText("Usuário");
// campoTexto1
campoTexto1.setForeground(new Color(0, 0, 255));
campoTexto1.setSelectedTextColor(new Color(0, 0, 255));
campoTexto1.setToolTipText("Entre com seu usuário");
campoTexto1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
campoTexto1_actionPerformed(e);
}
});
// campoSenha1
campoSenha1.setForeground(new Color(0, 0, 255));
campoSenha1.setSelectedTextColor(new Color(0, 0, 255));
campoSenha1.setToolTipText("Entre com sua senha");
campoSenha1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
campoSenha1_actionPerformed(e);
}
});
// botao1
botao1.setBackground(new Color(204, 204, 204));
botao1.setForeground(new Color(0, 0, 255));
botao1.setToolTipText("LOGIN");
botao1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
campoTexto1_actionPerformed(e);
}
});
// painel
painel.setLayout(null);
painel.setBorder(BorderFactory.createEtchedBorder());
painel.setBackground(new Color(204, 204, 204));
adicComponente(painel, label1, 5, 10, 106, 18);
adicComponente(painel, label2, 5, 47, 97, 18);
adicComponente(painel, campoTexto1, 110, 10, 183, 22);
adicComponente(painel, campoSenha1, 110, 45, 183, 22);
adicComponente(painel, botao1, 150, 75, 83, 28);
// login
this.setTitle("Login e Senha");
this.setLocation(new Point(76, 182));
this.setSize(new Dimension(335, 141));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setResizable(false);
}
private void adicComponente(Container container, Component c, int x, int y,
int width, int height) {
c.setBounds(x, y, width, height);
container.add(c);
}
private void campoTexto1_actionPerformed(ActionEvent e) {
}
private void campoSenha1_actionPerformed(ActionEvent e) {
}
private void botao1_actionPerformed(ActionEvent e) {
System.out.println("\n TESTE");
String usuario = new String(campoTexto1.getText());
String senha = new String(campoSenha1.getText());
if (usuario.equals("") || senha.equals("")) {
botao1.setEnabled(false);
JLabel errorFields = new JLabel(
"<HTML><FONT COLOR = Blue>Favor inserir usuário e senha!</FONT></HTML>");
JOptionPane.showMessageDialog(null, errorFields);
campoTexto1.setText("");
campoSenha1.setText("");
botao1.setEnabled(true);
this.setVisible(true);
} else {
JLabel optionLabel = new JLabel("<HTML><FONT COLOR = Blue>Você entrou </FONT><FONT COLOR = RED> <B>"+usuario+"</B></FONT> <FONT COLOR = Blue> como seu usuário.<BR> Está correto?</FONT></HTML>");
int confirm =JOptionPane.showConfirmDialog(null,optionLabel);
}
switch(confirm) {
case JOptionPane.YES_OPTION:
botao1.setEnabled(false);
break;
case JOptionPane.NO_OPTION:
botao1.setEnabled(false);
campoTexto1.setText("");
campoSenha1.setText("");
botao1.setEnabled(true);
break;
case JOptionPane.CANCEL_OPTION:
botao1.setEnabled(false);
campoTexto1.setText("");
campoSenha1.setText("");
botao1.setEnabled(true);
break;
}
}
}
E a classe MAIN
package Login;
import javax.swing.*;
public class LoginMain {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new Login();
};
}
As dúvidas que tenho são:
1 - No Eclipse me informa que no SWITCH feito no final da classe Login tem alguma coisa errada, mas não saquei o erro.
2 - Como eu estava aprendendo direto de um site que já tinha as classes prontas teve um TRY no MAIN que deu erro, e pelo o que eu entendi, o motivo é por eu usar LINUX.
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
O erro:
Failed loading L&F:
javax.swing.UnsupportedLookAndFeelException: [The Microsoft Windows Look and Feel - com.sun.java.swing.plaf.windows.WindowsLookAndFeel] not supported on this platform
3 - Vocês acham que da pra dividir toda essa classe Login em várias classes deixando mais OO?
Valeu …/