I am new to java programming. I am trying to create a GUI which includes JLabels, JPanels, JButton, text fields and labels. I am not using layout manager. Is there a particular structure in laying out the code that I should follow.? Thanks

.

Recommended Answers

All 2 Replies

Add the components before setting the JFrame visible.

The code to create a GUI is always a long long list of repetitive add/set method calls - people often use a GUI design tool to generate them. It really doesn't have a structure. The only thing you can do is to put it all in a simple method, eg private void createGUI() so it don't clutter up or confuse the rest of your logic.
I recommend using anonymous inner classes to handle events (mouse clicks etc) - the very first one is a bit of a learning curve, but after that it's just copy/paste. The beginner's technique of making your main class the event handler is a dead-end that doesn't scale at all well for the future.

Unless you have some very special requirement, not using a layout manager is a big mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.