Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.43 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. public class ClientGUI extends JFrame implements Thread.UncaughtExceptionHandler,
  9.         ActionListener {
  10.  
  11.     private static final int WIDTH = 400;
  12.         private static final int HEIGHT = 300;
  13.  
  14.         private final JTextArea log = new JTextArea();
  15.  
  16.         private final JPanel panelTop = new JPanel(new GridLayout(2, 3));
  17.         private final JTextField tfIPAddress = new JTextField("127.0.0.1");
  18.         private final JTextField tfPort = new JTextField("8189");
  19.         private final JCheckBox cbAlwaysOnTop = new JCheckBox("Always on top");
  20.         private final JTextField tfLogin = new JTextField("ivan_igorevich");
  21.         private final JPasswordField tfPassword = new JPasswordField("123456");
  22.         private final JButton btnLogin = new JButton("Login");
  23.  
  24.         private final JPanel panelBottom = new JPanel(new BorderLayout());
  25.         private final JButton btnDisconnect = new JButton("Disconnect");
  26.         private final JTextField tfMessage = new JTextField();
  27.         private final JButton btnSend = new JButton("Send");
  28.         private final JList<String> userList = new JList<>();
  29.  
  30.         public static void main(String[] args) {
  31.             SwingUtilities.invokeLater(new Runnable() {
  32.                 @Override
  33.                 public void run() {
  34.                     new ClientGUI();
  35.                 }
  36.             });
  37.         }
  38.  
  39.         ClientGUI() {
  40.             Thread.setDefaultUncaughtExceptionHandler(this);
  41.             setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  42.             setLocationRelativeTo(null);
  43.             setSize(WIDTH, HEIGHT);
  44.             setTitle("Chat Client");
  45.  
  46.             panelTop.add(tfIPAddress);
  47.             panelTop.add(tfPort);
  48.             panelTop.add(cbAlwaysOnTop);
  49.             panelTop.add(tfLogin);
  50.             panelTop.add(tfPassword);
  51.             panelTop.add(btnLogin);
  52.  
  53.             panelBottom.add(btnDisconnect, BorderLayout.WEST);
  54.             panelBottom.add(tfMessage, BorderLayout.CENTER);
  55.             panelBottom.add(btnSend, BorderLayout.EAST);
  56.  
  57.             btnSend.addActionListener(this);
  58.  
  59.  
  60.             log.setEditable(false);
  61.             JScrollPane scrollLog = new JScrollPane(log);
  62.             JScrollPane scrollUsers = new JScrollPane(userList);
  63.             scrollUsers.setPreferredSize(new Dimension(100, 0));
  64.  
  65.             String[] users = {"user1_000_000_000", "user2", "user3", "user4", "user5",
  66.                     "user6", "user7", "user8", "user9", "user10" };
  67.             userList.setListData(users);
  68.  
  69.             add(panelTop, BorderLayout.NORTH);
  70.             add(panelBottom, BorderLayout.SOUTH);
  71.             add(scrollLog, BorderLayout.CENTER);
  72.             add(scrollUsers, BorderLayout.EAST);
  73.             btnSend.addActionListener(new ActionListener() {
  74.                System.out.println("Отправленно");
  75.                @Override
  76.                 public void actionPerformed(ActionEvent actionEvent) {
  77.  
  78.                     sendMessage();
  79.                 }});
  80.             JRootPane rootPane = SwingUtilities.getRootPane(btnSend);
  81.             rootPane.setDefaultButton(btnSend);
  82.  
  83.             setVisible(true);
  84.  
  85.         }
  86.  
  87. //    Отправлять сообщения в лог по нажатию кнопки или по нажатию клавиши Enter.
  88. //    Создать лог в файле (записи должны делаться при отправке сообщений).
  89.  
  90.         @Override
  91.         public void actionPerformed(ActionEvent e) {
  92.            
  93.             Object src = e.getSource();
  94.             if (src == cbAlwaysOnTop) {
  95.                 setAlwaysOnTop(cbAlwaysOnTop.isSelected());
  96.             } else if (src == btnSend) {
  97.                 //useful code here
  98.             } else {
  99.                 throw new RuntimeException("Unknown source: " + src);
  100.             }
  101.         }
  102. //    public void sendMessage ( ActionEvent e){
  103. //            Object src = e.getSource();
  104. //            if (src == btnSend) {
  105. //                StringBuffer outText = new StringBuffer();
  106. //                outText.append(tfMessage.getText() + "\n");
  107. //            } else if(src == rootPane){
  108. //                StringBuffer outText = new StringBuffer();
  109. //                outText.append(tfMessage.getText() + "\n");
  110. //            }
  111. //    }
  112.     public void sendMessage ( ){
  113.         System.out.println("Отправленно");
  114.             Object src = e.getSource();
  115.             if (src == btnSend) {
  116.                 StringBuffer outText = new StringBuffer();
  117.                 outText.append(tfMessage.getText() + "\n");
  118.             } else if(src == rootPane){
  119.                 StringBuffer outText = new StringBuffer();
  120.                 outText.append(tfMessage.getText() + "\n");
  121.             }
  122.     }
  123.     @Override
  124.         public void uncaughtException(Thread t, Throwable e) {
  125.             e.printStackTrace();
  126.             String msg;
  127.             StackTraceElement[] ste = e.getStackTrace();
  128.             if (ste.length == 0) {
  129.                 msg = "Empty stack trace";
  130.             } else {
  131.                 msg = e.getClass().getCanonicalName() + ": " +
  132.                         e.getMessage() + "\n" + "\t at " + ste[0];
  133.             }
  134.             JOptionPane.showMessageDialog(this, msg, "Exception", JOptionPane.ERROR_MESSAGE);
  135.             System.exit(1);
  136.         }
  137.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement