Question

the java programming offer you to create Java GUI (J2SE) with two options. As we known, there are programmatically(Coding from scratch) or using Java look and feel(Drag & Drop). As my experience, its very easy to create Java GUI using java look and feel rather than programmatically. So, i'm frequently using java look and feel. ;)

Now, i'm know its wrong, because its limit us to create dynamic application. So i wanted start to create java application (J2SE) programmatically. But my question is very simple, how to create this frame programmatically as below :

enter image description here

As we know, its very simple using Java Look & feel (Just drag and drop). How to create space between jLabel1 and JtextField1 as we desired? And how to create leading space(Space below and after) between jTextField1 and jButton1 as we desired?

I know this is very stupid question for java expert, but perhaps this question can help other people(particularly java beginner) to start learning Java Programming and not doing as what i'm doing now. :)

Thanks for your advice, sorry for my bad English and possible duplicate question. :)

Was it helpful?

Solution

In Java, you typically create a hierarchy of JPanels (Swing), each having a suitable LayoutManager (GridLayout, PanelLayout, BoxLayout, GridBagLayout, etc...). See the link below for details;

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Using layout managers is the best dynamic approach for creating scalable and dynamic UIs, but there are lot's of drag-n-drop utility applications that set layout manager to null and thereby allow for pixel-based exact positioning of your different components. In your case, a JLabel, a JButton, and a JTextField.

Hence, to create the exact UI you shown, go for null-layout and place your component instances on upper-left-corner (x,y) positions relative to your frame's origo (again, upper-left corner).

OTHER TIPS

If you use the Drag-and-Drop interface, it should generate some code for you. Examine this code and use it yourself, making changes as you like. The DnD interface is nothing but a layer on top of the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top