Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class ClientGUI extends JFrame implements Thread.UncaughtExceptionHandler,
- ActionListener {
- private static final int WIDTH = 400;
- private static final int HEIGHT = 300;
- private final JTextArea log = new JTextArea();
- private final JPanel panelTop = new JPanel(new GridLayout(2, 3));
- private final JTextField tfIPAddress = new JTextField("127.0.0.1");
- private final JTextField tfPort = new JTextField("8189");
- private final JCheckBox cbAlwaysOnTop = new JCheckBox("Always on top");
- private final JTextField tfLogin = new JTextField("ivan_igorevich");
- private final JPasswordField tfPassword = new JPasswordField("123456");
- private final JButton btnLogin = new JButton("Login");
- private final JPanel panelBottom = new JPanel(new BorderLayout());
- private final JButton btnDisconnect = new JButton("Disconnect");
- private final JTextField tfMessage = new JTextField();
- private final JButton btnSend = new JButton("Send");
- private final JList<String> userList = new JList<>();
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- new ClientGUI();
- }
- });
- }
- ClientGUI() {
- Thread.setDefaultUncaughtExceptionHandler(this);
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- setLocationRelativeTo(null);
- setSize(WIDTH, HEIGHT);
- setTitle("Chat Client");
- panelTop.add(tfIPAddress);
- panelTop.add(tfPort);
- panelTop.add(cbAlwaysOnTop);
- panelTop.add(tfLogin);
- panelTop.add(tfPassword);
- panelTop.add(btnLogin);
- panelBottom.add(btnDisconnect, BorderLayout.WEST);
- panelBottom.add(tfMessage, BorderLayout.CENTER);
- panelBottom.add(btnSend, BorderLayout.EAST);
- btnSend.addActionListener(this);
- log.setEditable(false);
- JScrollPane scrollLog = new JScrollPane(log);
- JScrollPane scrollUsers = new JScrollPane(userList);
- scrollUsers.setPreferredSize(new Dimension(100, 0));
- String[] users = {"user1_000_000_000", "user2", "user3", "user4", "user5",
- "user6", "user7", "user8", "user9", "user10" };
- userList.setListData(users);
- add(panelTop, BorderLayout.NORTH);
- add(panelBottom, BorderLayout.SOUTH);
- add(scrollLog, BorderLayout.CENTER);
- add(scrollUsers, BorderLayout.EAST);
- btnSend.addActionListener(new ActionListener() {
- System.out.println("Отправленно");
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- sendMessage();
- }});
- JRootPane rootPane = SwingUtilities.getRootPane(btnSend);
- rootPane.setDefaultButton(btnSend);
- setVisible(true);
- }
- // Отправлять сообщения в лог по нажатию кнопки или по нажатию клавиши Enter.
- // Создать лог в файле (записи должны делаться при отправке сообщений).
- @Override
- public void actionPerformed(ActionEvent e) {
- Object src = e.getSource();
- if (src == cbAlwaysOnTop) {
- setAlwaysOnTop(cbAlwaysOnTop.isSelected());
- } else if (src == btnSend) {
- //useful code here
- } else {
- throw new RuntimeException("Unknown source: " + src);
- }
- }
- // public void sendMessage ( ActionEvent e){
- // Object src = e.getSource();
- // if (src == btnSend) {
- // StringBuffer outText = new StringBuffer();
- // outText.append(tfMessage.getText() + "\n");
- // } else if(src == rootPane){
- // StringBuffer outText = new StringBuffer();
- // outText.append(tfMessage.getText() + "\n");
- // }
- // }
- public void sendMessage ( ){
- System.out.println("Отправленно");
- Object src = e.getSource();
- if (src == btnSend) {
- StringBuffer outText = new StringBuffer();
- outText.append(tfMessage.getText() + "\n");
- } else if(src == rootPane){
- StringBuffer outText = new StringBuffer();
- outText.append(tfMessage.getText() + "\n");
- }
- }
- @Override
- public void uncaughtException(Thread t, Throwable e) {
- e.printStackTrace();
- String msg;
- StackTraceElement[] ste = e.getStackTrace();
- if (ste.length == 0) {
- msg = "Empty stack trace";
- } else {
- msg = e.getClass().getCanonicalName() + ": " +
- e.getMessage() + "\n" + "\t at " + ste[0];
- }
- JOptionPane.showMessageDialog(this, msg, "Exception", JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement