Advertisement
Guest User

Menu

a guest
Jun 7th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7. import java.net.Socket;
  8. import java.awt.event.WindowAdapter;
  9. import java.awt.event.WindowEvent;
  10. import java.io.PrintWriter;
  11.  
  12. public class menu extends JFrame implements ActionListener{
  13.  
  14. // variáveis da gui
  15. private JLabel L1;
  16. private JButton LOGIN, SAIR;
  17. private Dimension d1 = new Dimension(350, 60);
  18. private ImageIcon image = new ImageIcon("src/images/logo.png");
  19. private JLabel imageLabel = new JLabel(image);
  20. private Dimension SCREENSIZE = new Dimension(400, 600);
  21. private Font fBotoes = new Font("Arial", Font.BOLD, 25);
  22.  
  23. // variáveis de conexão
  24. private String endereco = "localhost";//ip do servidor
  25. private int porta = 12345;
  26. private Socket cliente;
  27. private PrintWriter escritor;
  28. private BufferedReader leitor;
  29. private boolean logando = true;
  30.  
  31. public menu() {
  32.  
  33. // configurações e layout da janela
  34. setTitle("chat");
  35. setSize(SCREENSIZE);
  36. setLocationRelativeTo(null);
  37. setResizable(false);
  38. setFocusableWindowState(true);
  39. setFocusableWindowState(true);
  40. getContentPane().setBackground(Color.white);
  41. setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  42.  
  43. // label e configurações da imagem do chat -- centro topo
  44. imageLabel.setLocation(15, 25);
  45. imageLabel.setSize(350,100);
  46.  
  47. // layout e configurações da label principal -- centro topo
  48. L1 = new JLabel("Menu Principal", JLabel.CENTER);
  49. L1.setLocation(20, 175);
  50. L1.setSize(350,40);
  51. L1.setFont(new Font("Source Sans Pro", Font.BOLD, 36));
  52. L1.setForeground(new Color(225, 112, 85));
  53.  
  54. // layout e configurações do botão login, -- centro
  55. LOGIN = new JButton("Realizar Login");
  56. LOGIN.setLocation(16,265);
  57. LOGIN.setSize(d1);
  58. LOGIN.setFont(fBotoes);
  59. LOGIN.setBorder(BorderFactory.createLineBorder(new Color(250, 177, 160)));
  60. LOGIN.setBackground(new Color(225, 112, 85));
  61. LOGIN.setForeground(Color.white);
  62. LOGIN.setFocusable(false);
  63. LOGIN.addActionListener(this);
  64.  
  65. // layout e configurações do botão sair -- centro baixo
  66. SAIR = new JButton("Sair");
  67. SAIR.setLocation(16,350);
  68. SAIR.setSize(d1);
  69. SAIR.setFont(fBotoes);
  70. SAIR.setBorder(BorderFactory.createLineBorder(new Color(250, 177, 160)));
  71. SAIR.setBackground(new Color(225, 112, 85));
  72. SAIR.setForeground(Color.white);
  73. SAIR.setFocusable(false);
  74. SAIR.addActionListener(this);
  75.  
  76. // ouvidor do botão "X" chamando o método fecharJanela
  77. addWindowListener(new WindowAdapter() {
  78. @Override
  79. public void windowClosing(WindowEvent e) {
  80. fecharJanela();
  81. }
  82. });
  83.  
  84. getContentPane().setLayout(null);
  85.  
  86. getContentPane().add(imageLabel);
  87. getContentPane().add(L1);
  88. getContentPane().add(LOGIN);
  89. getContentPane().add(SAIR);
  90.  
  91. setVisible(true);
  92.  
  93. }
  94.  
  95. /*
  96. * substituição das funcionalidades de fechar janela por um método que envia o comando ::sair para o servidor
  97. * evita que inconsistências relacionadas à conexão com o usuário ocorram na exução do programa servidor
  98. */
  99. public void fecharJanela() {
  100. if(JOptionPane.showConfirmDialog(this, "Deseja realmente sair?",
  101. "CONFIRMAR PRA SAIR", JOptionPane.INFORMATION_MESSAGE) == 0) {
  102. try {
  103. escritor.println("::sair");
  104. cliente.close();
  105. } catch(Exception erroSair) {}
  106. finally {System.exit(0);}
  107. }
  108. }
  109.  
  110. @Override
  111. public void actionPerformed(ActionEvent e) {
  112. if(e.getSource() == LOGIN) {
  113. // processo de login executando em uma nova thread
  114. new Thread (() -> {
  115. try {
  116. // endereço e porta
  117. cliente = new Socket(endereco, porta);
  118.  
  119. // iniciando leitor e escritor
  120. escritor = new PrintWriter(cliente.getOutputStream(),true);
  121. leitor = new BufferedReader(new InputStreamReader(cliente.getInputStream()));
  122.  
  123.  
  124. // captura de nome de usuário e senha
  125. String username = JOptionPane.showInputDialog("Digite o username:");
  126. String password = JOptionPane.showInputDialog("Digite a senha:");
  127.  
  128. // sinalizando a opção de login para o servidor
  129. escritor.println(1);
  130. // enviando nome de usuário e senha
  131. escritor.println(username);
  132. escritor.println(password);
  133.  
  134. String loginstatus;
  135.  
  136. while(logando) {
  137. // lendo a resposta do envio de login e senha
  138. loginstatus = leitor.readLine();
  139. System.out.println(loginstatus);
  140. if(!loginstatus.equals("logado")) {
  141. if(loginstatus.equals("erro")) {
  142. JOptionPane.showMessageDialog(null, "Erro ao logar! Verifique se o username e senha estão corretos e tente novamente.",
  143. "erro de login", 1);
  144. Thread.currentThread().interrupt();
  145. cliente.close();
  146. break;
  147. }
  148. }
  149. // parando o loop do ouvidor
  150. logando = false;
  151. // enviando as informações do usuário autenticado para outra tela
  152. new Cliente(username, cliente);
  153. dispose();
  154. Thread.currentThread().interrupt();
  155. }
  156. }catch (Exception xlogin) {xlogin.printStackTrace();}
  157. }).start();
  158. }
  159.  
  160. else if(e.getSource() == SAIR) {
  161. fecharJanela();
  162. }
  163.  
  164. }
  165.  
  166. public static void main(String[] args) {
  167. SwingUtilities.invokeLater(new Runnable(){@Override public void run(){
  168. new menu().setVisible(true);}});
  169.  
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement