As a NetBeans novice, I'm laying out a pretty simple 2-panel GUI using NetBeans 7.0, and I'm having the craziest time getting some components (JButtons, JLabels, JSlider) to stay where I position them within the enclosing JPanel!
For example, when I drag one of the buttons to the upper left corner of its enclosing panel, I never see any guidelines, and when I release it, it zooms back toward the center of the panel, disturbi开发者_开发技巧ng the placement of other components, and enlarging both the panel and, ultimately, the JFrame. It's as if the button somehow imagines itself to be a lot larger, or to need a lot more room around its edges, then it really does.
I hope that's enough information for someone with experience to readily diagnose my problem; otherwise I'll try to answer follow-up questions.
You need to specify the LayoutManager that you wish to use in the JPanel. The default LayoutManager is the FlowLayout and that is responsible for the behaviour that you are experiencing. You may want to try GridBagLayout or BoxLayout for more control over the positioning of components.
It is recommended that you learn the Layout specifications and how things fit together before using a GUI builder. You could use your Netbeans as a sophisticated editor with file completion and debugger and write your first Swing code in regular classes, until you get the grasp of things.
Then you will appreciate better the capabilities of the tool.
Netbeans give you a default Free-Design Layout which is very easy to use graphically and although the generated code is kind of weird, the good news is that you will never have to deal with it.
You can add borders to your panels so you can see the behavior when you drag and drop components and resize the panels.
You first change the Resizable property of that button to false. by default it is true. for this, select the button, at right hand side's Properties window you'll find that property in Layout group. Also check the Anchoring is done properly. Sometimes the component you add to left side may be anchored to right or top/bottom. For this right click on the button and go to "anchor" and select the appropriate value. Hope this helps you.
精彩评论