Results 1 to 4 of 4
Thread: GUI - is this bad practice?
- 05-01-2011, 06:41 AM #1
Member
- Join Date
- May 2010
- Posts
- 34
- Rep Power
- 0
GUI - is this bad practice?
I have to write a GUI without an IDE.
Is this bad practice: Make a new regular class for the GUI, define each component of the GUI as a variable. Set the properties in the constructor, then let methods access it easily.
Or do I have to make things static.. as when I use the example at the bottom of this page: Java BorderLayout Example I'm not sure on how to divide it into multiple methods, instead of just one.
Thanks for your help.
- 05-01-2011, 07:12 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Either is probably fine, however; I prefer to go with a more oo approach and use variables that are instantiated by the constructor, then provide getters and setters so other classes can make changes and get the current states of components.
- 05-01-2011, 09:47 AM #3
there are many patterns how to create a gui. one i like is this one: the components used in the gui are declared as variables. inside the class there is method createGUI() that initialize/instatiate the variables and returns a jpanel with all components for the gui. inside the main() method there is only the call to new yourclass.createGUI(); which adds the jpanel to the top-level component. this pattern is very flexible. if the top-level component is a applet the jpanel will be added to the applet and if it is a frame the jpanel is added to the frame.
Last edited by j2me64; 05-01-2011 at 09:51 AM.
-
I'd only make as variables those fields that I need a reference to in the class. For instance, if I have a JTextField whose text changes depending on the state of the object, then it needs to be an instance field of the class. If I have a JLabel whose text never changes, then it is declared and added inside of a method or constructor and it is not visible to the class.
The only things static are things that should be static. For instance if I have a class Player, and I need a variable in this class to keep track of total number of Player objects, then this variable is static. Or if I have a constant, it is usually final static. Everything else should probably be instance. The example you show is just a quick and dirty example of how to use borders but and puts all in the main method, something you should not do in anything but the most simple of programs (say a 20 line program).Or do I have to make things static.. as when I use the example at the bottom of this page: Java BorderLayout Example I'm not sure on how to divide it into multiple methods, instead of just one.
Similar Threads
-
Suggestions on a Practice App for Java2ee
By chizbox in forum New To JavaReplies: 0Last Post: 05-12-2010, 06:36 AM -
java practice
By miko5054 in forum New To JavaReplies: 3Last Post: 03-22-2010, 05:31 PM -
Best practice for packaging add-ons
By Goose in forum Advanced JavaReplies: 2Last Post: 08-05-2009, 10:51 PM -
SCJA - practice
By Mihail Kravsun in forum Java CertificationReplies: 2Last Post: 04-29-2009, 07:26 AM -
Best practice of IF loop with NOT
By sathishranganathan in forum Advanced JavaReplies: 13Last Post: 05-29-2008, 12:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks