Results 1 to 4 of 4

Thread: No frame?

  1. #1
    Lars1111 is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Question No frame?

    Hi

    I have build a client application and a server application. Both applications communicate with eachother. Everything works but I have one problem with the server application. I don't see the frame.

    It pops up when I close the client. It must show, when I run the server application.

    Could someone help me?

    Java Code:
     
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    
    public class ServerApp extends JFrame
    {
    		private int status;
    		private JLabel ownerLabel;
    		ServerSocket   server;
    		Socket         socket;
    		PrintWriter    out;
    		BufferedReader in;
    
    
    		int port = 5000;
    		int i, code;
    		CommPort comm;
    
    		int MotorArray1[] = new int[]{};
    		int MotorArray2[] = new int[]{};
    		int MotorArray3[] = new int[]{};
    		int MotorArray4[] = new int[]{};
    		int MotorArray5[] = new int[]{};
    		int MotorArray6[] = new int[]{};
    
    		public static void main(String[] sCommand)
    		{
    			System.out.println("Server - Opstarten...");
    			ServerApp frame = new ServerApp();
    			frame.setBounds(900,100,260,330);
    			frame.setTitle("Server");
    			frame.setVisible(true);
    
    		}
    
    		public ServerApp()
    		{
    			initializeMethod();
    			createConn();
    		}
    
    		public void initializeMethod()
    		{
    
    
    			ownerLabel = new JLabel ("2010-04-07 Lars");
    			ownerLabel.setBounds (10,260,300,20);
    			add (ownerLabel);
    
    			addWindowListener(new WindowAdapter()
    			{
    				public void windowClosing(WindowEvent e)
    				{
    					dispose();
    					System.exit(0);
    				}
    			});
    		}
    		private void createConn()
    		{
    
    			try
    			{
    				server = new ServerSocket(port);
    				System.out.println("Server - Wachten op verbinding...");
    				socket = server.accept();
    				System.out.println("Server - Client heeft verbinding gemaakt!");
    				out = new PrintWriter(socket.getOutputStream());
    				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
    				comm = new CommPort();
    				comm.open(1, 2400, true, 8, CommPort.PAR_NONE, CommPort.STOP_ONE);
    
    		for(;;)
    		{
    			String request = in.readLine();
    			System.out.println("Server - Ontvangen van client: '" + request + "'...");
    			out.println(request.toUpperCase());
    			out.flush();
    
    			if (request.equals("Stand 1"))
    			{
    				for(i=0; i<MotorArray1.length; i++)
    				{
    					code = MotorArray1[i];
    					comm.write(code);
    					status = 1;
    					repaint();
    
    				}
    			}
    
    			else if (request.equals("Stand 2"))
    			{
    				for(i=0; i<MotorArray2.length; i++)
    				{
    					code = MotorArray2[i];
    					comm.write(code);
    					status = 2;
    					repaint();
    
    				}
    			}
    
    			else if (request.equals("Stand 3"))
    			{
    				for(i=0; i<MotorArray3.length; i++)
    				{
    					code = MotorArray3[i];
    					comm.write(code);
    					status = 3;
    					repaint();
    
    				}
    			}
    
    			else if (request.equals("Stand 4"))
    			{
    				for(i=0; i<MotorArray4.length; i++)
    				{
    					code = MotorArray4[i];
    					comm.write(code);
    					status = 4;
    					repaint();
    
    				}
    			}
    
    			else if (request.equals("Stand 5"))
    			{
    				for(i=0; i<MotorArray5.length; i++)
    				{
    					code = MotorArray5[i];
    					comm.write(code);
    					status = 5;
    					repaint();
    
    				}
    			}
    
    			else if (request.equals("Stand 6"))
    			{
    				for(i=0; i<MotorArray6.length; i++)
    				{
    					code = MotorArray6[i];
    					comm.write(code);
    					status = 6;
    					repaint();
    
    				}
    			}
    		}
    	}
    		catch (IOException e)
    		{
    		}
    		System.out.println("Verbinding verbroken...");
    	}
    
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    
    		if(status == 1)
    		{
    			g.drawString("Robot in stand 1", 15, 180);
    
    		}
    		else if(status == 2)
    		{
    			g.drawString("Robot in stand 2", 15, 180);
    		}
    		else if(status == 3)
    		{
    			g.drawString("Robot in stand 3", 15, 180);
    		}
    		else if(status == 4)
    		{
    			g.drawString("Robot in stand 4", 15, 180);
    		}
    		else if(status == 5)
    		{
    			g.drawString("Robot in stand 5", 15, 180);
    		}
    		else if(status == 6)
    		{
    			g.drawString("Robot in stand 6", 15, 180);
    		}
    	}
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Do a slight change in your code.

    Java Code:
        public static void main(String[] sCommand) {
            System.out.println("Server - Opstarten...");
            ServerApp frame = new ServerApp();
        }
    
        public ServerApp() {
            setBounds(900, 100, 260, 330);
            setTitle("Server");
            setVisible(true);
    
            initializeMethod();
            createConn();
        }
    Why? Because when your constructor initialized, invoke createConn() method lead to infinite loop.

  3. #3
    Lars1111 is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    Thank you very much! That small change did the thrick. Now I get it. Problem solved!

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Frame doesn't appear
    By SarahB in forum AWT / Swing
    Replies: 3
    Last Post: 12-27-2009, 08:36 AM
  2. Replies: 7
    Last Post: 11-02-2009, 06:31 PM
  3. GUI new frame
    By billbo123 in forum New To Java
    Replies: 15
    Last Post: 03-02-2009, 04:24 AM
  4. frame
    By arunkumarinfo in forum NetBeans
    Replies: 0
    Last Post: 02-07-2009, 10:26 AM
  5. Frame to other Frame
    By Aswq in forum New To Java
    Replies: 2
    Last Post: 07-19-2008, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •