You're welcome.
Oracle's Tutorial is very good - arguably the best - but the Swing section assumes some familiarity with Java and it attempts to be rather comprehensive so it's long. Also it presents things a piece at a time rather disconnectedly rather than building up a modest application which is probably a better way to proceed if you're new to programming and/or Swing. Hence my reluctance to simply point you there and say grumpily "don't copy code".
Whatever you do, understand every line. And any surrounding textual comment. In this case the author did discuss "widgets" enough to give a clue about what was going on (provided you knew enough about Swing to already have a fair idea what was going on!
. You can find every method documented in the
API docs, so bookmark that resource and consult it so that no method call is a mystery.
If you find things on the web that don't wrok
or are unclear, ask about them on forums like this. And don't be afraid to give the url: the more information, the better.
When I started to write code, I commented things. A lot. Probably I over commented them, but that's by far a lesser evil and easily fixed later with the delete key. But the act of commenting on discoveries as I made them helped to fix them in my mind. Java comments and meaningful variable names are essential for anything more complex than "Hello World" and the compiler isn't going to help you here: you have to be disciplined and do this for yourself. The easiest thing is if the discipline becomes a habit. The method in question could be rewritten:
/**
* Sets the size and position of a component and adds it to a container.
*
* @param con the container to which the component will be added
* @param widget the component to be added
* @param left the x- coordinate of the component's position
* @param top the y- coordinate of the component's position
* @param width the width to make the component
* @param height the height to make the component
*/
private void addElement (Container con, Component widget, int left, int top, int width, int height) {
widget.setBounds(left, top, width, height);
con.add(widget);
}
(Notice the order of the parameters in setBounds(). The author had called the third one "h" suggesting he thought it was height. This is the sort of mistake I make and it's corrected by using proper variable names and looking up the
setBounds() documentation.)
Good luck, and happy coding!