Advertisement
far_light

Client

Feb 17th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.46 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import javax.swing.JOptionPane;
  4.  
  5. import java.awt.BorderLayout;
  6. import java.awt.EventQueue;
  7.  
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10. import javax.swing.border.EmptyBorder;
  11. import javax.swing.JTable;
  12. import javax.swing.table.DefaultTableModel;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.border.BevelBorder;
  15. import java.awt.Color;
  16. import javax.swing.JButton;
  17. import java.awt.Font;
  18. import java.awt.event.ActionListener;
  19. import java.awt.event.ActionEvent;
  20.  
  21. public class Main extends JFrame {
  22.  
  23.     private JPanel contentPane;
  24.     private JTable table;
  25.    
  26.     private String makeRequest() {
  27.         String result = "?";
  28.        
  29.         for (int i = 0; i < 10; i++) {
  30.             result += "thing" + (i + 1) + "=";
  31.            
  32.             int count;
  33.             try {
  34.                 count = Integer.parseInt(table.getValueAt(i,  3).toString().trim());
  35.             } catch (Exception e) {
  36.                 count = 0;
  37.             }
  38.            
  39.             result += "" + count + "&";
  40.         }
  41.        
  42.         return result;
  43.     }
  44.  
  45.     /**
  46.      * Launch the application.
  47.      */
  48.     public static void main(String[] args) {
  49.         EventQueue.invokeLater(new Runnable() {
  50.             public void run() {
  51.                 try {
  52.                     Main frame = new Main();
  53.                     frame.setVisible(true);
  54.                 } catch (Exception e) {
  55.                     e.printStackTrace();
  56.                 }
  57.             }
  58.         });
  59.     }
  60.  
  61.     /**
  62.      * Create the frame.
  63.      */
  64.     public Main() {
  65.         setResizable(false);
  66.         setTitle("\u041F\u043E\u0441\u0442\u0443\u043F\u043B\u0435\u043D\u0438\u0435 \u0442\u043E\u0432\u0430\u0440\u043E\u0432");
  67.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  68.         setBounds(100, 100, 450, 300);
  69.         contentPane = new JPanel();
  70.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  71.         setContentPane(contentPane);
  72.         contentPane.setLayout(null);
  73.        
  74.         JScrollPane scrollPane = new JScrollPane();
  75.         scrollPane.setBounds(10, 11, 424, 184);
  76.         contentPane.add(scrollPane);
  77.        
  78.         table = new JTable();
  79.         scrollPane.setViewportView(table);
  80.         table.setSurrendersFocusOnKeystroke(true);
  81.         table.setBackground(Color.WHITE);
  82.         table.setForeground(Color.DARK_GRAY);
  83.         table.setModel(new DefaultTableModel(
  84.             new Object[][] {
  85.                 {"1", "\u041C\u0435\u0434", null, null},
  86.                 {"2", "\u041F\u0440\u043E\u043F\u043E\u043B\u0438\u0441", null, null},
  87.                 {"3", "\u0412\u043E\u0441\u043A \u043F\u0447\u0435\u043B\u0438\u043D\u044B\u0439", null, null},
  88.                 {"4", "\u0426\u0432\u0435\u0442\u043E\u0447\u043D\u0430\u044F \u043F\u044B\u043B\u044C\u0446\u0430", null, null},
  89.                 {"5", "\u041F\u0435\u0440\u0433\u0430", null, null},
  90.                 {"6", "\u041C\u0430\u0442\u043E\u0447\u043D\u043E\u0435 \u043C\u043E\u043B\u043E\u0447\u043A\u043E", null, null},
  91.                 {"7", "\u0422\u0440\u0443\u0442\u043D\u0435\u0432\u043E\u0435 \u043C\u043E\u043B\u043E\u0447\u043A\u043E", null, null},
  92.                 {"8", "\u041F\u0447\u0435\u043B\u0438\u043D\u044B\u0439 \u044F\u0434", null, null},
  93.                 {"9", "\u041F\u0447\u0435\u043B\u0438\u043D\u0430\u044F \u043E\u0433\u043D\u0435\u0432\u043A\u0430", null, null},
  94.                 {"10", "\u041F\u0447\u0435\u043B\u0438\u043D\u044B\u0439 \u043F\u043E\u0434\u043C\u043E\u0440", null, null},
  95.             },
  96.             new String[] {
  97.                 "\u2116", "\u041D\u0430\u0438\u043C\u0435\u043D\u043E\u0432\u0430\u043D\u0438\u0435 \u0442\u043E\u0432\u0430\u0440\u0430", "\u041E\u0441\u0442\u0430\u0442\u043E\u043A \u043D\u0430 \u0441\u043A\u043B\u0430\u0434\u0435", "\u041A\u043E\u043B\u0438\u0447\u0435\u0441\u0442\u0432\u043E \u043F\u043E\u0441\u0442\u0443\u043F\u043B\u0435\u043D\u0438\u044F"
  98.             }
  99.         ) {
  100.             Class[] columnTypes = new Class[] {
  101.                 Object.class, Object.class, Object.class, Integer.class
  102.             };
  103.             public Class getColumnClass(int columnIndex) {
  104.                 return columnTypes[columnIndex];
  105.             }
  106.             boolean[] columnEditables = new boolean[] {
  107.                 false, false, false, true
  108.             };
  109.             public boolean isCellEditable(int row, int column) {
  110.                 return columnEditables[column];
  111.             }
  112.         });
  113.         table.getColumnModel().getColumn(0).setResizable(false);
  114.         table.getColumnModel().getColumn(0).setPreferredWidth(37);
  115.         table.getColumnModel().getColumn(1).setResizable(false);
  116.         table.getColumnModel().getColumn(1).setPreferredWidth(130);
  117.         table.getColumnModel().getColumn(2).setResizable(false);
  118.         table.getColumnModel().getColumn(2).setPreferredWidth(112);
  119.         table.getColumnModel().getColumn(3).setResizable(false);
  120.         table.getColumnModel().getColumn(3).setPreferredWidth(139);
  121.        
  122.         JButton buttonOk = new JButton("\u0412\u044B\u043F\u043E\u043B\u043D\u0438\u0442\u044C");
  123.         buttonOk.addActionListener(new ActionListener() {
  124.             public void actionPerformed(ActionEvent e) {
  125.                 String query = "http://store/goods.php" + makeRequest();
  126.                
  127.                 boolean isSuccess = false;
  128.                 try {
  129.                     URL url = new URL(query);
  130.                     HttpURLConnection connect = (HttpURLConnection) url.openConnection();
  131.                     connect.connect();
  132.                    
  133.                     BufferedReader in = new BufferedReader(
  134.                             new InputStreamReader(connect.getInputStream()));
  135.                     String inputLine = in.readLine().trim();
  136.                    
  137.                     if (inputLine.indexOf('~') >= 0) {
  138.                         isSuccess = true;
  139.                         String[] massive = inputLine.split("~");
  140.                        
  141.                         for (int i = 0; i < massive.length; i++)
  142.                             table.setValueAt(massive[i], i, 2);
  143.                     }
  144.                    
  145.                     in.close();
  146.                     connect.disconnect();
  147.                     connect = null;
  148.                 } catch (Exception ex) {
  149.                     if (!isSuccess)
  150.                         JOptionPane.showMessageDialog(null, "Not connect", "Error on query!", 0);
  151.                 }
  152.             }
  153.         });
  154.         buttonOk.setForeground(Color.GREEN);
  155.         buttonOk.setFont(new Font("Tahoma", Font.PLAIN, 17));
  156.         buttonOk.setBounds(154, 205, 136, 23);
  157.         contentPane.add(buttonOk);
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement