Sunday, October 4, 2009

Adding Swing components

The task for this week does not allow making changes by just looking at the example code. You actually need to read and understand chapter 4 and 5!
In order for any Swing component to appear on any panel you need to define the component (like JButton newone;) which says that the component with system name “newone” will be of a data type Jbutton. You also need to actually instantiate this component = create a space in the memory and label it “newone” for future references by issuing command:

newone = new JButton( "success" );

This key word “new” is what actually creates a physical instance in the memory of a theoretically defined JButton newone. See page 71 for examples (read the book or you will be lost soon).

Once you have your button (or any other Swing component) in physical existence (instantiated) – the next step is placing it on the proper panel. In a simplified example for the Central part of the Welcome page (welcomePanel) it might look like:

welcomePanel.add( one );

I am leaving the tweaking of the better positioning for your exploration. But if, for example, you want to put a button after welcome message then you put all three mentioned above statements after the statement:

welcomePanel.add( welcomeMsg );

which adds welcomeMsg to the Panel. It will look as following:

welcomePanel.add( welcomeMsg );
JButton newone; // defining the button
newone = new JButton( "success" ); //instantiating the button
welcomePanel.add( one ); //adding button

No comments:

Post a Comment