Results 1 to 8 of 8
- 01-14-2013, 07:19 AM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
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:
after I run this, it runs, but then it doesn't show absolutely anything. The following are the errors: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(); } }
Thanks guys; all help's appreciated!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.
- 01-14-2013, 07:42 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-14-2013, 02:04 PM #3
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
- 01-14-2013, 02:29 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-14-2013, 03:18 PM #5
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
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?
- 01-14-2013, 03:34 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Re: My program is running, but it isnt showing up...
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 ...
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-15-2013, 02:16 AM #7
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
-
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
-
How to make a program flash if it isnt in focus
By mattxo in forum New To JavaReplies: 3Last Post: 06-29-2011, 06:55 PM -
Why isnt my program detecting any key presses?
By rajkobie in forum New To JavaReplies: 6Last Post: 05-19-2011, 05:45 PM -
while running multiple thread repaint isnt being called....
By raptor in forum Threads and SynchronizationReplies: 4Last Post: 08-25-2010, 11:47 PM -
Changes in code not showing up when running program!?
By jchsu in forum New To JavaReplies: 1Last Post: 03-30-2010, 09:23 PM -
How can i use keyevent even tho the program isnt selected?
By Addez in forum New To JavaReplies: 11Last Post: 12-25-2009, 10:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks