Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.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://localhost/chat", "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, 436, 293);
  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, 198);
  104. contentPane.add(list);
  105.  
  106. fromTextField = new JTextField();
  107. fromTextField.setBounds(249, 11, 133, 20);
  108. contentPane.add(fromTextField);
  109. fromTextField.setColumns(10);
  110.  
  111. toTextField = new JTextField();
  112. toTextField.setBounds(249, 42, 133, 20);
  113. contentPane.add(toTextField);
  114. toTextField.setColumns(10);
  115.  
  116. textArea = new JTextArea();
  117. textArea.setBounds(182, 73, 224, 136);
  118. contentPane.add(textArea);
  119.  
  120. JButton checkButton = new JButton("\u041F\u0440\u043E\u0432\u0435\u0440\u0438\u0442\u044C");
  121. checkButton.addActionListener(new ActionListener() {
  122. public void actionPerformed(ActionEvent arg0) {
  123. load_data();
  124. }
  125. });
  126. checkButton.setBounds(10, 221, 162, 23);
  127. contentPane.add(checkButton);
  128.  
  129. JButton createButton = new JButton("\u041D\u043E\u0432\u043E\u0435 \u043F\u0438\u0441\u044C\u043C\u043E");
  130. createButton.setBounds(182, 221, 110, 23);
  131. contentPane.add(createButton);
  132.  
  133. JButton sendButton = new JButton("\u041E\u0442\u043F\u0440\u0430\u0432\u0438\u0442\u044C");
  134. sendButton.setBounds(296, 221, 114, 23);
  135. contentPane.add(sendButton);
  136.  
  137. JLabel lblNewLabel = new JLabel("\u041E\u0442:");
  138. lblNewLabel.setBounds(213, 12, 46, 14);
  139. contentPane.add(lblNewLabel);
  140.  
  141. JLabel lblNewLabel_1 = new JLabel("\u041A\u043E\u043C\u0443:");
  142. lblNewLabel_1.setBounds(213, 45, 46, 14);
  143. contentPane.add(lblNewLabel_1);
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement