Advertisement
Guest User

Untitled

a guest
May 8th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1. public class ClientGUI extends JFrame {
  2.  
  3.     private JPanel contentPane;
  4.     private JTextField txtsend;
  5.     private static JTextField txtreceive;
  6.     private static JTextField txtip;
  7.     private static JTextField txtport;
  8.     private JTextField txthost;
  9.  
  10.  
  11.  
  12.     /**
  13.      * Launch the application.
  14.      */
  15.     public static void main(String[] args) {
  16.         EventQueue.invokeLater(new Runnable() {
  17.             public void run() {
  18.                 try {
  19.                     ClientGUI frame = new ClientGUI();
  20.                     frame.setVisible(true);
  21.                 } catch (Exception e) {
  22.                     e.printStackTrace();
  23.                 }
  24.             }
  25.         });
  26.     }
  27.  
  28.     /**
  29.      * Create the frame.
  30.      * @throws UnknownHostException
  31.      * @throws IOException
  32.      */
  33.    
  34.     public ClientGUI() throws IOException {
  35.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.         setBounds(100, 100, 500, 350);
  37.         contentPane = new JPanel();
  38.         contentPane.setBorder(null);
  39.         contentPane.setLayout(new BorderLayout(0, 0));
  40.         setContentPane(contentPane);
  41.         InetAddress thisIp = InetAddress.getLocalHost();
  42.         String ownIP = thisIp.getHostAddress().toString(); 
  43.  
  44.        
  45.        
  46.         JPanel panel = new JPanel();
  47.         contentPane.add(panel, BorderLayout.CENTER);
  48.         panel.setLayout(null);
  49.        
  50.         JPanel panel_1 = new JPanel();
  51.         panel_1.setBounds(18, 185, 448, 118);
  52.         panel.add(panel_1);
  53.         panel_1.setLayout(null);
  54.         panel_1.setBorder(BorderFactory.createLineBorder(Color.black, 2));
  55.        
  56.         txtreceive = new JTextField();
  57.         txtreceive.setToolTipText("Gauta \u017Einut\u0117");
  58.         txtreceive.setFont(new Font("Tahoma", Font.BOLD, 15));
  59.         txtreceive.setEnabled(false);
  60.         txtreceive.setBounds(70, 35, 165, 45);
  61.         panel_1.add(txtreceive);
  62.         txtreceive.setColumns(10);
  63. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!       
  64.         JButton btnGauti = new JButton("Gauti");
  65.         btnGauti.addActionListener(new ActionListener() {
  66.             public void actionPerformed(ActionEvent e) {
  67.                 String serverAddress = txtip.getText();
  68.                 int port = Integer.parseInt(txtport.getText());
  69.                 Socket s = new Socket(serverAddress, port);
  70.                 try{
  71.                 BufferedReader input =
  72.                         new BufferedReader(new InputStreamReader(s.getInputStream()));
  73.                 String answer = input.readLine();
  74.                 txtreceive.setText(answer);
  75.                 System.exit(0);
  76.                 s.close();
  77.                 s.shutdownInput();}
  78.                 catch(IOException e1){}        
  79.             }});
  80. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  81.         btnGauti.setBounds(300, 46, 89, 23);
  82.         panel_1.add(btnGauti);
  83.                
  84.         JPanel panel_2 = new JPanel();
  85.         panel_2.setBounds(18, 85, 448, 91);
  86.         panel.add(panel_2);
  87.         panel_2.setLayout(null);
  88.         panel_2.setBorder(BorderFactory.createLineBorder(Color.black, 2));
  89.        
  90.         txtsend = new JTextField();
  91.         txtsend.setFont(new Font("Tahoma", Font.BOLD, 15));
  92.         txtsend.setToolTipText("J\u016Bs\u0173 \u017Einut\u0117");
  93.         txtsend.setBounds(70, 20, 165, 45);
  94.         panel_2.add(txtsend);
  95.         txtsend.setColumns(10);
  96. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  97.         JButton btnNewButton = new JButton("Si\u0173sti");
  98.         btnNewButton.addActionListener(new ActionListener() {
  99.             public void actionPerformed(ActionEvent e) {
  100.                 String message = txtsend.getText();
  101.                 int port = Integer.parseInt(txtport.getText());
  102.                 ServerSocket listener = new ServerSocket(port);
  103.                 try{
  104.                     while (true) {
  105.                         Socket socket = listener.accept();
  106.                         try {
  107.                             PrintWriter out = new PrintWriter(socket.getOutputStream(),
  108.                                     true);
  109.                             out.println(message);
  110.                             txtsend.setText(new String(""));
  111.                             socket.close();
  112.                             System.exit(0);
  113.                             listener.close();}
  114.                         catch(IOException e1){}
  115.                         }}
  116.                     catch(IOException e1){}
  117.                 finally {}
  118.     }});
  119. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  120.         btnNewButton.setBounds(300, 33, 89, 23);
  121.         panel_2.add(btnNewButton);
  122.        
  123.         JPanel panel_3 = new JPanel();
  124.         panel_3.setBounds(18, 10, 448, 67);
  125.         panel.add(panel_3);
  126.         panel_3.setLayout(null);
  127.         panel_3.setBorder(new LineBorder(new Color(0, 0, 0), 2));
  128.        
  129.         Label label = new Label("TCP/IP komunikacija");
  130.         label.setBounds(5, 20, 171, 28);
  131.         panel_3.add(label);
  132.         label.setFont(new Font("Dialog", Font.BOLD, 16));
  133.         label.setAlignment(Label.CENTER);
  134.        
  135.         txtip = new JTextField();
  136.         txtip.setToolTipText("Gav\u0117jo IP Adresas");
  137.         txtip.setBounds(241, 11, 121, 20);
  138.         panel_3.add(txtip);
  139.         txtip.setColumns(10);
  140.        
  141.         txtport = new JTextField();
  142.         txtport.setToolTipText("Portas");
  143.         txtport.setBounds(384, 22, 54, 20);
  144.         panel_3.add(txtport);
  145.         txtport.setColumns(10);
  146.        
  147.         txthost = new JTextField();
  148.         txthost.setToolTipText("J\u016Bs\u0173 IP adresas");
  149.         txthost.setHorizontalAlignment(SwingConstants.CENTER);
  150.         txthost.setText(ownIP);
  151.         txthost.setEditable(false);
  152.         txthost.setBounds(241, 36, 121, 20);
  153.         panel_3.add(txthost);
  154.         txthost.setColumns(10);
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement