Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Why is a Frame Not Showing Up When I Run This Class?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is a Frame Not Showing Up When I Run This Class?

    I am trying to create a frame with 4 labels on it. When I run this class, it runs with no errors, but there is no frame that shows up? Any suggestions?


    I should have a frame with 4 labels that read "test text" of 4 separate lines. Thanks for taking a look - Here is what I have:





    import java.awt.Frame;
    import java.awt.Label;

    public class CustFrame extends Frame {
    public CustFrame(Customer cust) {

    this.setSize(300, 282);
    this.setLayout(null);
    this.setVisible(true);

    Label custNameLbl = new Label();
    Label shipToLbl1 = new Label();
    Label shipToLbl2 = new Label();
    Label contactInfo = new Label();

    custNameLbl.setBounds(62, 65, 176, 23);
    shipToLbl1.setBounds(62, 120, 176, 23);
    shipToLbl2.setBounds(62, 175, 176, 23);
    contactInfo.setBounds(62, 230, 176, 23);

    custNameLbl.setText("test text");
    shipToLbl1.setText("test text");
    shipToLbl2.setText("test text");
    contactInfo.setText("test text");

    this.add(custNameLbl);
    this.add(shipToLbl1);
    this.add(shipToLbl2);
    this.add(contactInfo);
    }

    public static void main(String[] args) {
    Customer cust = new Customer();
    Customer CustFrame = cust;
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    Why are you using the Frame class instead of the JFrame class? How does the code exit the jvm when the frame is closed?

    Where does the code create an instance of the CustFrame class? The constructor needs to be called.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    I am not sure why JFrame is not being used. This is in alignment with the example provided by my professor. What precisely do you mean when you ask how the code exits the JVM? How do you call the constructor exactly? Sorry, I am a newbie...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    I'll take a look. Thanks!

    --- Update ---

    Okay, I took a look at the tutorials along with my textbook, and I am still obviously missing something because I am not getting a frame displayed. I think I understand the concept of how to create an object and how to instantiate it. My constructor starts with the public CustFrame(Customer cust); right? Isn't that "calling" the constructor? As for creating an instance of CustFrame, hmmmm...wouldn't that look something like: CustFrame cust = new CustFrame();? This would be the equivalent of creating an object named cust, which is an instantiation of the CustFrame class, correct? I'm sorry, but I am missing a basic element here. Thanks for your help and your patience!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    creating an instance of CustFrame, hmmmm...wouldn't that look something like:
    CustFrame cust = new CustFrame();?
    Yes that is how a constructor is called. That is what is missing from the posted code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    Thanks Norm!

    --- Update ---

    Ugh...still not working...don't know why...probably going to take a break. Been going at this for like 5 hours - not really making progress.

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    I did this and it works for me. This is my code for a game i'm making.
    	public static void main(String[] args) {
     
    		JFrame frame = new JFrame("My Game Alpha v0.1"); // Title
     
    		frame.add(new Main());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Ends process when closed
    		frame.setSize(800, 500); // Resolution
    		frame.setVisible(true); // Sets visible
    		frame.setLocationRelativeTo(null); // Centers window when opened
    		frame.setResizable(false);

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    Cool! Let me soak your code in for a while Cody, thanks! I think I already see a few nuggets that might help me out as far as grasping the concepts. you have everything in the main method...hmmm.

  10. #10
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    No problem man. And yes! I put it in a seperate class. This is the full source coede for it. If you want just create a class and add this. There are other ways to do it but I find this easiest.
    package MyGame;
     
    import javax.swing.JFrame;
     
    public class Frame {
     
    	public static void main(String[] args) {
     
    		JFrame frame = new JFrame("My Game Alpha v0.1"); // Title
     
    		frame.add(new Main());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Ends process when closed
    		frame.setSize(800, 500); // Resolution
    		frame.setVisible(true); // Sets visible
    		frame.setLocationRelativeTo(null); // Centers window when opened
    		frame.setResizable(false);
    	}
    }

    Try and make a new class with that and tell me if it works. Also don't forget to import. (CTRL + Shift + O)

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    Ugh...still not working...
    Please post the new code and the full text of any error messages you are getting.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    Well, following is my code. Honestly, I think the problem is at least partially located within the main method - I am not sure I completely understanding what is happening there. I am essentially creating new objects but I am not sure to what end?



    import java.awt.Frame;
    import java.awt.Label;

    public class CustFrame extends Frame {



    public CustFrame(Customer cust) {

    // Label custNameLbl = new Label();
    this.setSize(300, 282);
    this.setLayout(null);
    this.setVisible(true);


    Label custNameLbl = new Label();
    this.setSize(300, 282);
    this.setLayout(null);
    custNameLbl.setBounds(62, 65, 176, 23);
    custNameLbl.setText("test text");
    this.add(custNameLbl);
    this.setVisible(true);


    Label shipToLbl1 = new Label();
    this.setSize(300, 282);
    this.setLayout(null);
    shipToLbl1.setBounds(62, 120, 176, 23);
    shipToLbl1.setText("test text");
    this.add(shipToLbl1);
    this.setVisible(true);

    Label shipToLbl2 = new Label();
    this.setSize(300, 282);
    this.setLayout(null);
    shipToLbl2.setBounds(62, 175, 176, 23);
    shipToLbl2.setText("test text");
    this.add(shipToLbl2);
    this.setVisible(true);

    Label contactInfo = new Label();
    this.setSize(300, 282);
    this.setLayout(null);
    contactInfo.setBounds(62, 230, 176, 23);
    contactInfo.setText("test text");
    this.add(contactInfo);
    this.setVisible(true);
    }

    }
    public CustFrame() {
    // TODO Auto-generated constructor stub
    }

    public CustFrame cust = new CustFrame();

    public static void main(String[] args) {
    Customer cust = new Customer();
    CustFrame Customer = new CustFrame();
    }
    }


    As for error messages, I get about a hundred lines of "at CustFrame.<init>(CustFrame.java:70)" followed by one line of "at CustFrame.main(CustFrame.java:74)." I do not, however, generate any frame with the appropriate labels at all. Is there something obvious in my code staring me in the face that I am missing? Thanks!

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    Please copy the full text of the error message and paste it here.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Ormolu611 View Post
    Well, following is my code. Honestly, I think the problem is at least partially located within the main method - I am not sure I completely understanding what is happening there. I am essentially creating new objects but I am not sure to what end?



    import java.awt.Frame;
    import java.awt.Label;

    public class CustFrame extends Frame {



    public CustFrame(Customer cust) {

    //Label custNameLbl = new Label();
    this.setSize(300, 282);
    this.setLayout(null);
    this.setVisible(true);


    Label custNameLbl = new Label();
    this.setSize(300, 282);
    this.setLayout(null);
    custNameLbl.setBounds(62, 65, 176, 23);
    custNameLbl.setText("test text");
    this.add(custNameLbl);
    this.setVisible(true);


    Label shipToLbl1 = new Label();
    this.setSize(300, 282);
    this.setLayout(null);
    shipToLbl1.setBounds(62, 120, 176, 23);
    shipToLbl1.setText("test text");
    this.add(shipToLbl1);
    this.setVisible(true);

    Label shipToLbl2 = new Label();
    this.setSize(300, 282);
    this.setLayout(null);
    shipToLbl2.setBounds(62, 175, 176, 23);
    shipToLbl2.setText("test text");
    this.add(shipToLbl2);
    this.setVisible(true);

    Label contactInfo = new Label();
    this.setSize(300, 282);
    this.setLayout(null);
    contactInfo.setBounds(62, 230, 176, 23);
    contactInfo.setText("test text");
    this.add(contactInfo);
    this.setVisible(true);
    }

    }
    public CustFrame() {
    // TODO Auto-generated constructor stub
    }

    public CustFrame cust = new CustFrame();

    public static void main(String[] args) {
    Customer cust = new Customer();
    CustFrame Customer = new CustFrame();
    }
    }


    As for error messages, I get about a hundred lines of "at CustFrame.<init>(CustFrame.java:70)" followed by one line of "at CustFrame.main(CustFrame.java:74)." I do not, however, generate any frame with the appropriate labels at all. Is there something obvious in my code staring me in the face that I am missing? Thanks!
    I see your issue. Your calling the wrong constructor. You have two of them. One that accepts a parameter of type customer and another that has no para maters. The one that take son parameters is being called but does nothing! Pass in cust inside of the constructor call and it will take off. also don't instantiate twice.
     
     import java.awt.Frame;
     import java.awt.Label;
     
     public class CustFrame extends Frame {
     
     
     
     public CustFrame(Customer cust) {
     
    	 //Label custNameLbl = new Label();
    	 this.setSize(300, 282);
    	 this.setLayout(null);
    	 this.setVisible(true);
     
     
    	 Label custNameLbl = new Label();
    	 this.setSize(300, 282);
    	 this.setLayout(null);
    	 custNameLbl.setBounds(62, 65, 176, 23);
    	 custNameLbl.setText("test text");
    	 this.add(custNameLbl);
    	 this.setVisible(true);
     
     
    	 Label shipToLbl1 = new Label();
    	 this.setSize(300, 282);
    	 this.setLayout(null);
    	 shipToLbl1.setBounds(62, 120, 176, 23);
    	 shipToLbl1.setText("test text");
    	 this.add(shipToLbl1);
    	 this.setVisible(true);
     
    	 Label shipToLbl2 = new Label();
    	 this.setSize(300, 282);
    	 this.setLayout(null);
    	 shipToLbl2.setBounds(62, 175, 176, 23);
    	 shipToLbl2.setText("test text");
    	 this.add(shipToLbl2);
    	 this.setVisible(true);
     
    	 Label contactInfo = new Label();
    	 this.setSize(300, 282);
    	 this.setLayout(null);
    	 contactInfo.setBounds(62, 230, 176, 23);
    	 contactInfo.setText("test text");
    	 this.add(contactInfo);
    	 this.setVisible(true);
     	}
     
    /*
     	public CustFrame() {
     	// TODO Auto-generated constructor stub
     	}
    */
     	//public CustFrame cust;
     
     	public static void main(String[] args) {
     		Customer cust = new Customer();
     		CustFrame Customer = new CustFrame(cust);
     	}
     }
    try that for your code

  15. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    Rockking8350, it works! Fantastic! Thank you. So, to review, the constructor that accepts a parameter of type customer in my code is:

    Customer cust = new Customer();

    Conversely, the constructor that was doing nothing was:

    CustFrame Customer = new CustFrame();

    Is this accurate? So my understanding is that with the first constructor, I was instantiating an object of type Customer called cust. The second constructor, I was essentially not "passing" anything to that object, and that was my problem - is that right? So by adding cust in the parenthesis, I was "passing" the previously created cust to Customer....right? I am still just a little shaky about what is happening here and just want to make sure. Thanks!

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    This is the constructor for the CustFrame class:
      public CustFrame(Customer cust) {
    Notice it takes a Customer class object as a parameter.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    you have another issue. you have the same name variable. cust is two different types of variables. you really need to watch how you instantiate the variables, and make sure not to double do it.
    Customer cust = new Customer();
    all that does is makes a new customer named cust
    CustFrame Customer = new CustFrame();
    this is the call to the CustFrame constructor. but by property of polymorphism you are doing nothing. you need to pass in a variable of type customer to actually do anything because the constructor that accepts a parameter is what has all the code in it. so you were making a call to an empty constructor which did nothing. also, watch your naming. your frame is the same name as a class type. that should be changed. now the reason why you didn't get an error is because you had a constructor that could be called but didn't require a parameter. had you not had the empty constructor, you would have been told that it required a parameter of type customer.

  18. #18
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    Thanks guys! I am going to play with it a bit and try to comment out alternate code snippets to see the effect. It looks like some of my coding is superfluous and unnecessary...I'll also try to be aware of the double dipping variable issue...thanks again!

  19. #19
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is a Frame Not Showing Up When I Run This Class?

    we all started somewhere.

Similar Threads

  1. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  2. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  3. Frame not showing Image until resized
    By gamalytical in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 13th, 2011, 07:09 AM
  4. Hiding\Showing parts of the frame
    By liron50 in forum AWT / Java Swing
    Replies: 17
    Last Post: April 29th, 2011, 12:14 PM
  5. any ways to run a class from another class?
    By javanub:( in forum Java Theory & Questions
    Replies: 3
    Last Post: May 9th, 2010, 06:57 AM