Results 1 to 8 of 8
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default My program is running, but it isnt showing up...

    Hey guys, I'm trying to make a simple swing program that just gives car names and stats, and it is running, and turns into an executable. I then see the Java cup icon, but it doesn't actually show anything. it then gives me a bunch of errors which I will show after the code. I appreciate all help :)
    heres the code:


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    class Car extends JFrame {
    	Car() {
    		super("Cars");
    		setSize(1000, 1000);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    
    	int mpg;
    	int mph;
    	String color;
    
    	Car(int mpg, int mph, String color) {
    		this.mpg = mpg;
    		this.mph = mph;
    		this.color = color;
    	}
    
    	Car Ferrari = new Car(20, 200, "Red");
    	Car Lexus = new Car(30, 160, "White");
    	Car Honda = new Car(25, 150, "Blue");
    
    	public void paint(Graphics m) {
    		m.setFont(new Font("Helvetica", Font.PLAIN, 16));
    		m.drawRect(20, 50, 960, 460);
    		m.setColor(Color.BLACK);
    		m.fillRect(20, 50, 960, 460);
    		m.setColor(Color.WHITE);
    		m.drawString("We have a " + Ferrari.color + "that gets " + Ferrari.mpg
    				+ "and can go as fast as " + Ferrari.mph + ".", 30, 70);
    		m.drawString("We have a " + Honda.color + "that gets " + Honda.mpg
    				+ "and can go as fast as " + Honda.mph + ".", 30, 100);
    		m.drawString("We have a " + Lexus.color + "that gets " + Lexus.mpg
    				+ "and can go as fast as " + Lexus.mph + ".", 30, 130);
    	}
    
    	public static void main(String[] args) {
    		new Car();
    	}
    }
    after I run this, it runs, but then it doesn't show absolutely anything. The following are the errors:

    Java Code:
    	at sun.lwawt.macosx.CWrapper$NSScreen.frame(Native Method)
    	at sun.lwawt.macosx.LWCToolkit.getScreenInsets(LWCToolkit.java:357)
    	at java.awt.Window.init(Window.java:498)
    	at java.awt.Window.<init>(Window.java:536)
    	at java.awt.Frame.<init>(Frame.java:420)
    	at java.awt.Frame.<init>(Frame.java:385)
    	at javax.swing.JFrame.<init>(JFrame.java:180)
    	at Car.<init>(Car.java:7)
    	at Car.<init>(Car.java:12) // please note that this line is repeated about a quadrillion times, but for the sake of not taking up a day uploading this post, I showed it once.
    Thanks guys; all help's appreciated!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: My program is running, but it isnt showing up...

    In another thread I gave you a link to the API documentation; did you read it? A JFrame doesn't paint itself ... (and you should use the paintComponent( ... ) method)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My program is running, but it isnt showing up...

    Quote Originally Posted by JosAH View Post
    In another thread I gave you a link to the API documentation; did you read it? A JFrame doesn't paint itself ... (and you should use the paintComponent( ... ) method)

    kind regards,

    Jos
    what would I do here to use paintComponent? Thank you

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: My program is running, but it isnt showing up...

    Read the API documentation for the JFrame again and pay special attention how it handles its content pane.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My program is running, but it isnt showing up...

    Ok, I added the getContentPane().add(Car); into it, but it just says that Car is an unrecognized symbol... what else could I be doing wrong?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: My program is running, but it isnt showing up...

    Quote Originally Posted by MW130 View Post
    Ok, I added the getContentPane().add(Car); into it, but it just says that Car is an unrecognized symbol... what else could I be doing wrong?
    That is just crap; a Car is a JFrame itself and you can't add top level containers to a top level container. Please design your program first before you start banging that poor keyboard. It also increases the level of your questions if you get stuck and come back here again. b.t.w. Car isn't a variable itself, it is the name of a Class so what you were trying to do doesn't even make sense on the language level ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My program is running, but it isnt showing up...

    Quote Originally Posted by JosAH View Post
    That is just crap; a Car is a JFrame itself and you can't add top level containers to a top level container. Please design your program first before you start banging that poor keyboard. It also increases the level of your questions if you get stuck and come back here again. b.t.w. Car isn't a variable itself, it is the name of a Class so what you were trying to do doesn't even make sense on the language level ...

    Jos
    Hey, JosAH I got it to work. I simply made the car constructor in a seperate class. Thanks anyway.
    Last edited by MW130; 01-15-2013 at 02:21 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: My program is running, but it isnt showing up...

    You did not show your full error message, and I mean the *top* of the error message where it likely mentioned a stack overflow exception. Please don't leave important information like this out of your questions. You've got a recursion problem where you are creating new Car objects inside of Car itself which will cause your program to run out of memory.

Similar Threads

  1. How to make a program flash if it isnt in focus
    By mattxo in forum New To Java
    Replies: 3
    Last Post: 06-29-2011, 06:55 PM
  2. Why isnt my program detecting any key presses?
    By rajkobie in forum New To Java
    Replies: 6
    Last Post: 05-19-2011, 05:45 PM
  3. while running multiple thread repaint isnt being called....
    By raptor in forum Threads and Synchronization
    Replies: 4
    Last Post: 08-25-2010, 11:47 PM
  4. Replies: 1
    Last Post: 03-30-2010, 09:23 PM
  5. Replies: 11
    Last Post: 12-25-2009, 10:30 PM

Tags for this Thread

Posting Permissions

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