• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is best Practice for Using JFrame class in GUI designing

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I have one question that suppose I am designing a Swing applications and I am using JFrame as Top level container.

So what is best
1)first Extends that class like


public class A Extends JFrame
{
//Do all stuff with Top Level container with gui
}



2)Or design like this from this source
http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TopLevelDemoProject/src/components/TopLevelDemo.java





Thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My rule of thumb would be: Will the app have more than one window? If so, I'd create a subclass of JFrame, and keep the code separate from the class containing the "main" method. Otherwise I might set up the GUI in the main class.

As to whether the main class should extend JFrame, or construct it, I think that's personal preference to some degree. I think the latter is a somewhat cleaner approach.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should you have a class which extends JFrame? I prefer not to extend it myself. You can probably use most Swing components without subclassing them, but when you need to override paintComponent you will need a subclass (probably of JPanel). Example contrasting JFrame and its subclass in this thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!