Last active
October 19, 2024 15:55
-
-
Save kauefraga/18a5c622b273fd8fa4e159ec87814771 to your computer and use it in GitHub Desktop.
Apresentação de linguagem de programação - Introdução ao Swing e AWT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
public class IntroducaoAoSwingEAwt { | |
public static void main(String[] args) { | |
Frame frame = new Frame("Login"); | |
Label lUsuario = new Label("Nome de usuário"); | |
TextField tUsuario = new TextField(); | |
Label lSenha = new Label("Senha"); | |
TextField tSenha = new TextField(); | |
Label lResposta = new Label(); | |
Button bEntrar = new Button("Entrar"); | |
bEntrar.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String usuario = tUsuario.getText(); | |
String senha = tSenha.getText(); | |
System.out.println("Usuario digitado: " + usuario); | |
if (usuario.equals("admin") && senha.equals("cafe")) { | |
lResposta.setText("Administrador autenticado"); | |
} else if (usuario.equals("junior") && senha.equals("1234")) { | |
lResposta.setText("Juninho autorizado"); | |
} else { | |
lResposta.setText("Usuário desconhecido"); | |
} | |
} | |
}); | |
frame.setLayout(new GridLayout(7, 1)); | |
frame.add(lUsuario); | |
frame.add(tUsuario); | |
frame.add(lSenha); | |
frame.add(tSenha); | |
frame.add(new Label()); | |
frame.add(bEntrar); | |
frame.add(lResposta); | |
frame.setSize(350, 400); | |
frame.setVisible(true); | |
frame.addWindowListener(new WindowAdapter() { | |
@Override | |
public void windowClosing(WindowEvent we) { | |
System.exit(0); | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
public class IntroducaoAoSwingEAwt { | |
public static void main(String[] args) { | |
JFrame frame = new JFrame("Login"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setSize(350, 400); | |
frame.setLayout(new GridLayout(7, 1)); | |
JLabel lUsuario = new JLabel("Nome de usuário"); | |
JTextField tUsuario = new JTextField(); | |
JLabel lSenha = new JLabel("Senha"); | |
JPasswordField pSenha = new JPasswordField(); | |
JButton bEntrar = new JButton("Entrar"); | |
JLabel lResposta = new JLabel(); | |
bEntrar.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String usuario = tUsuario.getText(); | |
String senha = new String(pSenha.getPassword()); | |
System.out.println("Usuario digitado: " + usuario); | |
if (usuario.equals("admin") && senha.equals("cafe")) { | |
lResposta.setText("Administrador autenticado"); | |
} else if (usuario.equals("junior") && senha.equals("1234")) { | |
lResposta.setText("Juninho autorizado"); | |
} else { | |
lResposta.setText("Usuário desconhecido"); | |
} | |
} | |
}); | |
frame.add(lUsuario); | |
frame.add(tUsuario); | |
frame.add(lSenha); | |
frame.add(pSenha); | |
frame.add(new JLabel()); | |
frame.add(bEntrar); | |
frame.add(lResposta); | |
frame.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A diferença de
texto.equals("blabla")
etexto == "blabla"
é que o método.equals
compara o conteúdo das strings, enquanto o operador de igualdade (==
) compara a referência das strings, que pode ser diferente mesmo que o conteúdo das strings seja igual