Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.Socket;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.PrintWriter;
- public class menu extends JFrame implements ActionListener{
- // variáveis da gui
- private JLabel L1;
- private JButton LOGIN, SAIR;
- private Dimension d1 = new Dimension(350, 60);
- private ImageIcon image = new ImageIcon("src/images/logo.png");
- private JLabel imageLabel = new JLabel(image);
- private Dimension SCREENSIZE = new Dimension(400, 600);
- private Font fBotoes = new Font("Arial", Font.BOLD, 25);
- // variáveis de conexão
- private String endereco = "localhost";//ip do servidor
- private int porta = 12345;
- private Socket cliente;
- private PrintWriter escritor;
- private BufferedReader leitor;
- private boolean logando = true;
- public menu() {
- // configurações e layout da janela
- setTitle("chat");
- setSize(SCREENSIZE);
- setLocationRelativeTo(null);
- setResizable(false);
- setFocusableWindowState(true);
- setFocusableWindowState(true);
- getContentPane().setBackground(Color.white);
- setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
- // label e configurações da imagem do chat -- centro topo
- imageLabel.setLocation(15, 25);
- imageLabel.setSize(350,100);
- // layout e configurações da label principal -- centro topo
- L1 = new JLabel("Menu Principal", JLabel.CENTER);
- L1.setLocation(20, 175);
- L1.setSize(350,40);
- L1.setFont(new Font("Source Sans Pro", Font.BOLD, 36));
- L1.setForeground(new Color(225, 112, 85));
- // layout e configurações do botão login, -- centro
- LOGIN = new JButton("Realizar Login");
- LOGIN.setLocation(16,265);
- LOGIN.setSize(d1);
- LOGIN.setFont(fBotoes);
- LOGIN.setBorder(BorderFactory.createLineBorder(new Color(250, 177, 160)));
- LOGIN.setBackground(new Color(225, 112, 85));
- LOGIN.setForeground(Color.white);
- LOGIN.setFocusable(false);
- LOGIN.addActionListener(this);
- // layout e configurações do botão sair -- centro baixo
- SAIR = new JButton("Sair");
- SAIR.setLocation(16,350);
- SAIR.setSize(d1);
- SAIR.setFont(fBotoes);
- SAIR.setBorder(BorderFactory.createLineBorder(new Color(250, 177, 160)));
- SAIR.setBackground(new Color(225, 112, 85));
- SAIR.setForeground(Color.white);
- SAIR.setFocusable(false);
- SAIR.addActionListener(this);
- // ouvidor do botão "X" chamando o método fecharJanela
- addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosing(WindowEvent e) {
- fecharJanela();
- }
- });
- getContentPane().setLayout(null);
- getContentPane().add(imageLabel);
- getContentPane().add(L1);
- getContentPane().add(LOGIN);
- getContentPane().add(SAIR);
- setVisible(true);
- }
- /*
- * substituição das funcionalidades de fechar janela por um método que envia o comando ::sair para o servidor
- * evita que inconsistências relacionadas à conexão com o usuário ocorram na exução do programa servidor
- */
- public void fecharJanela() {
- if(JOptionPane.showConfirmDialog(this, "Deseja realmente sair?",
- "CONFIRMAR PRA SAIR", JOptionPane.INFORMATION_MESSAGE) == 0) {
- try {
- escritor.println("::sair");
- cliente.close();
- } catch(Exception erroSair) {}
- finally {System.exit(0);}
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource() == LOGIN) {
- // processo de login executando em uma nova thread
- new Thread (() -> {
- try {
- // endereço e porta
- cliente = new Socket(endereco, porta);
- // iniciando leitor e escritor
- escritor = new PrintWriter(cliente.getOutputStream(),true);
- leitor = new BufferedReader(new InputStreamReader(cliente.getInputStream()));
- // captura de nome de usuário e senha
- String username = JOptionPane.showInputDialog("Digite o username:");
- String password = JOptionPane.showInputDialog("Digite a senha:");
- // sinalizando a opção de login para o servidor
- escritor.println(1);
- // enviando nome de usuário e senha
- escritor.println(username);
- escritor.println(password);
- String loginstatus;
- while(logando) {
- // lendo a resposta do envio de login e senha
- loginstatus = leitor.readLine();
- System.out.println(loginstatus);
- if(!loginstatus.equals("logado")) {
- if(loginstatus.equals("erro")) {
- JOptionPane.showMessageDialog(null, "Erro ao logar! Verifique se o username e senha estão corretos e tente novamente.",
- "erro de login", 1);
- Thread.currentThread().interrupt();
- cliente.close();
- break;
- }
- }
- // parando o loop do ouvidor
- logando = false;
- // enviando as informações do usuário autenticado para outra tela
- new Cliente(username, cliente);
- dispose();
- Thread.currentThread().interrupt();
- }
- }catch (Exception xlogin) {xlogin.printStackTrace();}
- }).start();
- }
- else if(e.getSource() == SAIR) {
- fecharJanela();
- }
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable(){@Override public void run(){
- new menu().setVisible(true);}});
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement