Advertisement
Guest User

Java 1C messages

a guest
Apr 23rd, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import java.util.ArrayList;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.border.EmptyBorder;
  8. import javax.swing.JList;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JTextField;
  11. import javax.swing.JTextArea;
  12. import javax.swing.DefaultListModel;
  13. import javax.swing.JButton;
  14. import javax.swing.JLabel;
  15. import java.sql.*;
  16. import java.awt.event.ActionListener;
  17. import java.awt.event.ActionEvent;
  18. import javax.swing.event.ListSelectionListener;
  19. import javax.swing.event.ListSelectionEvent;
  20.  
  21.  
  22. public class Main extends JFrame {
  23.     private JPanel contentPane;
  24.     private JTextField fromTextField;
  25.     private JTextField toTextField;
  26.     JList list;
  27.     JTextArea textArea;
  28.     ArrayList<String> text = new ArrayList<>();
  29.     ArrayList<String> from = new ArrayList<>();
  30.     ArrayList<String> to = new ArrayList<>();
  31.     Connection con;
  32.     String name;
  33.  
  34.     public static void main(String[] args) {
  35.         EventQueue.invokeLater(new Runnable() {
  36.             public void run() {
  37.                 try {
  38.                     String login = JOptionPane.showInputDialog("Введите логин:");
  39.                     Main frame = new Main(login);
  40.                     frame.setVisible(true);
  41.                 } catch (Exception e) {
  42.                     e.printStackTrace();
  43.                 }
  44.             }
  45.         });
  46.     }
  47.  
  48.     void load_data() {
  49.         try {
  50.             Statement stm = con.createStatement();
  51.             ResultSet res = stm.executeQuery(
  52.                     "select * from message where `to` = '" + name + "'");
  53.             text.clear();
  54.             from.clear();
  55.             to.clear();
  56.             while (res.next()) {
  57.                 text.add(res.getString("text"));
  58.                 from.add(res.getString("from"));
  59.                 to.add(res.getString("to"));
  60.             }
  61.             DefaultListModel<String> model = new DefaultListModel<>();
  62.             for (int i = 0; i < text.size(); ++i) {
  63.                 model.addElement(from.get(i));
  64.             }
  65.             list.setModel(model);
  66.         } catch (SQLException e) {
  67.             e.printStackTrace();
  68.         }
  69.     }
  70.    
  71.     public Main(String login) {
  72.         name = login;
  73.         try {
  74.             Class.forName("com.mysql.jdbc.Driver");
  75.             con = DriverManager.getConnection(
  76.                     "jdbc:mysql://ryadom.me/chat", "root", "root");
  77.         } catch (ClassNotFoundException | SQLException e) {
  78.             e.printStackTrace();
  79.             JOptionPane.showMessageDialog(null, "Нет подключения");
  80.             System.exit(0);
  81.         }
  82.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  83.         setBounds(100, 100, 754, 429);
  84.         contentPane = new JPanel();
  85.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  86.         setContentPane(contentPane);
  87.         contentPane.setLayout(null);
  88.        
  89.         list = new JList();
  90.         list.addListSelectionListener(new ListSelectionListener() {
  91.             public void valueChanged(ListSelectionEvent arg0) {
  92.                 int n = list.getSelectedIndex();
  93.                
  94.                 fromTextField.setText(from.get(n));
  95.                 toTextField.setText(to.get(n));
  96.                 textArea.setText(text.get(n));
  97.                
  98.                 fromTextField.setEditable(false);
  99.                 toTextField.setEditable(false);
  100.                 textArea.setEditable(false);
  101.             }
  102.         });
  103.         list.setBounds(10, 11, 162, 334);
  104.         contentPane.add(list);
  105.        
  106.         fromTextField = new JTextField();
  107.         fromTextField.setEditable(false);
  108.         fromTextField.setBounds(249, 11, 465, 20);
  109.         contentPane.add(fromTextField);
  110.         fromTextField.setColumns(10);
  111.        
  112.         toTextField = new JTextField();
  113.         toTextField.setEditable(false);
  114.         toTextField.setBounds(249, 42, 465, 20);
  115.         contentPane.add(toTextField);
  116.         toTextField.setColumns(10);
  117.        
  118.         textArea = new JTextArea();
  119.         textArea.setEditable(false);
  120.         textArea.setBounds(182, 73, 532, 272);
  121.         contentPane.add(textArea);
  122.        
  123.         JButton checkButton = new JButton("\u041F\u0440\u043E\u0432\u0435\u0440\u0438\u0442\u044C");
  124.         checkButton.addActionListener(new ActionListener() {
  125.             public void actionPerformed(ActionEvent arg0) {
  126.                 load_data();
  127.             }
  128.         });
  129.         checkButton.setBounds(10, 356, 162, 23);
  130.         contentPane.add(checkButton);
  131.        
  132.         JButton createButton = new JButton("\u041D\u043E\u0432\u043E\u0435 \u043F\u0438\u0441\u044C\u043C\u043E");
  133.         createButton.addActionListener(new ActionListener() {
  134.             public void actionPerformed(ActionEvent e) {
  135.                 fromTextField.setText(name);
  136.                 toTextField.setText("");
  137.                 textArea.setText("");
  138.                
  139.                 toTextField.setEditable(true);
  140.                 textArea.setEditable(true);
  141.             }
  142.         });
  143.         createButton.setBounds(182, 356, 248, 23);
  144.         contentPane.add(createButton);
  145.        
  146.         JButton sendButton = new JButton("\u041E\u0442\u043F\u0440\u0430\u0432\u0438\u0442\u044C");
  147.         sendButton.addActionListener(new ActionListener() {
  148.             public void actionPerformed(ActionEvent e) {
  149.                
  150.                 try {
  151.                     PreparedStatement stm = con.prepareStatement("INSERT INTO message (`from`, `to`, `text`) VALUES(?, ?, ?)");
  152.                     stm.setString(1, fromTextField.getText());
  153.                     stm.setString(2, toTextField.getText());
  154.                     stm.setString(3, textArea.getText());
  155.                     stm.execute();
  156.                 } catch (SQLException e1) {
  157.                     e1.printStackTrace();
  158.                 }
  159.                 JOptionPane.showMessageDialog(null, "Отправлено!");
  160.                 toTextField.setEditable(false);
  161.                 textArea.setEditable(false);
  162.             }
  163.         });
  164.         sendButton.setBounds(466, 356, 248, 23);
  165.         contentPane.add(sendButton);
  166.        
  167.         JLabel lblNewLabel = new JLabel("\u041E\u0442:");
  168.         lblNewLabel.setBounds(213, 12, 46, 14);
  169.         contentPane.add(lblNewLabel);
  170.        
  171.         JLabel lblNewLabel_1 = new JLabel("\u041A\u043E\u043C\u0443:");
  172.         lblNewLabel_1.setBounds(213, 45, 46, 14);
  173.         contentPane.add(lblNewLabel_1);
  174.         load_data();
  175.     }
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement