import javax.swing.*;
public class Hello
{
public void initGUI()
{
JFrame frame =new JFrame("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run()
{
Hello hello =new Hello();
hello.initGUI();
}
});
}
}
🔒 Locked
[java] Problem with hello world program
Advertisement
Original Post
I'm trying to do a program along the lines of this tutorial here I have this so far Anyway that will work except after like 5-10 seconds the application will stop responding and ask me to close it...Any ideas why this might happen?
import javax.swing.*;public class Hello { public Hello(){ this.initGUI(); } public void initGUI() { JFrame frame =new JFrame("Hello World"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800,600); frame.setVisible(true); } public static void main(String[] args) { Hello hello = new Hello(); }}It was working fine for me, but you could try this, wasn't quite sure why you were using invokeLater when displaying a JFrame so took that out, and made it so when you create a new instance of Hello, it calls the initGUI straight away.
This also worked for me fine. Could give it a go and see if you get the same problem, perhaps its something to do with that invokeLater?
Quote:
Original post by natebuckley
It was working fine for me, but you could try this, wasn't quite sure why you were using invokeLater when displaying a JFrame so took that out, [...]
Not using invokeLater can lead to a rare race condition which can make the application crash or freeze.
Quote:
Original post by BitMaster
Not using invokeLater can lead to a rare race condition which can make the application crash or freeze.
Ahh Thank you, I didn't know that.
Quote:
Original post by BitMaster Quote:
Original post by natebuckley
It was working fine for me, but you could try this, wasn't quite sure why you were using invokeLater when displaying a JFrame so took that out, [...]
Not using invokeLater can lead to a rare race condition which can make the application crash or freeze.
That is for making updates to an existing GUI. If the GUI hasn't been displayed yet it is meaningless.
Yep, working with the EDT (event dispatch thread) and crummy Swing is a pain in the arse (that's only one reason why), especially for games.
...Which is why you use Flex and AIR instead. :p With a Java backend if you're multiplayer. (Couldn't resist the opportunity for an attack on swing..)
...Which is why you use Flex and AIR instead. :p With a Java backend if you're multiplayer. (Couldn't resist the opportunity for an attack on swing..)
Quote:
Original post by CaptainJester Quote:
Original post by BitMaster Quote:
Original post by natebuckley
It was working fine for me, but you could try this, wasn't quite sure why you were using invokeLater when displaying a JFrame so took that out, [...]
Not using invokeLater can lead to a rare race condition which can make the application crash or freeze.
That is for making updates to an existing GUI. If the GUI hasn't been displayed yet it is meaningless.
No, if you look for example at this link the Sun tutorials are distinctively suggesting to use invokeLater to create the GUI from the main method. To the best of my knowledge, they even took the time to rework all their Swing examples to use this method. I once had an article that explained the reasoning in more depth but unfortunately I can't find it at the moment.
Quote:
Original post by BitMaster
No, if you look for example at this link the Sun tutorials are distinctively suggesting to use invokeLater to create the GUI from the main method. To the best of my knowledge, they even took the time to rework all their Swing examples to use this method. I once had an article that explained the reasoning in more depth but unfortunately I can't find it at the moment.
Yeah I remember that crummy stuff. Just makes using Swing more infuriating, especially for GAME development. I can throw together a GUI in Flex and wire it to a Java backend in minutes. Hell, Flex Builder generates half of that for you, complete with an integrated Java project to run on the server if you want. No lousy multithreaded GUI code, no "custom look and feel" rubbish, no pissy "layouts" to fight with.
I don't just want to bash Swing, it was great for it's time (although not as much for a gaming platform), but in practice I guess it just turned out too hard for everyone to use correctly. So we ended up with all these poorly responding "Java applications" which made everyone think Java was crap, whenever someone didn't use the EDT correctly. There's new technologies emerging now that also make a much better gaming client platform (ex Flex), which wire into Java servers (where Java does dominate the market) just fine.
To attempt staying on topic--I'm assuming since this is Gamedev the OP is interested in game development--do youself a favor and do some research on the newer rich client technologies out there, forget Swing unless you're in a college Java class. Although if you really want to impress a Java teacher, you could create a Java server, make it talk to a Flex client, and then show that off.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement