A simple class to build on:
To begin with, we introduce a class that provides the basis for adding the visual components. This class simply defines a top-level window with a number of panels, which we will use to hold examples of visual components. Remember that panels are used to group elements, such as buttons, together. Our base class for this section is as follows:
import java.awt. *;
import javax.swing. *;
public class SwingClass extends JFrame
{
protected JPanel topPanel, botPanel;
public SwingClass ( )
{
setSize(400, 400);
setTitle("User Interface");
topPanel = new JPanel( );
botPanel = new JPanel( );
/* Add the elements to the frame via the contentPane. The default layout BorderLayout is used.*/
Container cp = getContentPane( );
cp.add(topPanel, BorderLayout.NORTH);
cp.add(botPanel, BorderLayout.SOUTH);
}
}
To run the code we use the following class:
public class SwingClassTest
{
public static void main (String [] args)
{
SwingClass thisClass = new SwingClass( );
thisClass.setVisible(true);
}
}
Before continuing, you should read through this code in order to make sure you understand what it does. We define a new class by extending JFrame. We then create an instance of this class using the constructor, which carries out the process of building up the interface elements. Inside the constructor, two panels are created and then added to the frame using the add method. We have used this method before and glossed over the details, so it is worth providing a little more detail. Whenever you want to insert an object into a frame (or related container), you will use the add method. There are two ways that the method can be called. The first is the most common:
add(component)
but the second way:
add(component, constraint)
is used by layout managers (for example, the BorderLayout class), which use the second argument to position the component.
The add method you use is determined by the layout method the holder uses. If you are writing code and your calls to add don't seem to be working, one of the first things that you should check is whether the layout manager you are using requires a constraint argument. This is especially likely to happen with certain classes that use the
BorderLayout class - your code will compile and run, but if you call the wrong version of add then the components will not appear.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your A simple class to build on homework and assignments? Live A simple class to build on experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer A simple class to build on homework help, java assignment help and A simple class to build on projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours