Friday 24 March 2017

Make Frames in JAVA (Main Windows)

By
Ashwini,

                  A Frame is a top-level window with a title and border. The size of the frame includes any area designated for the border. Since the border area is included in the overall size of the frame, the border effectively obscures a portion of the frame, constraining the area available for rendering and/or displaying subcomponents to the rectangle which has an upper-left corner location of (insets.left, insets.top).
                  A frame is implemented as an instance of the JFrame class which is a window that has decorations such as a border, a title, and which supports button components that close the window. An application with a GUI usually includes at least one frame. Applets sometimes use frames, as well.  


Here is a picture of the extremely plain window. The FrameDemo demonstration application.






The following FrameDemo code shows how to create a frame:-

JFrame frame = new JFrame("FrameDemo");  

//1. Create the frame.

//2. What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//3. Create components and put in the frame.
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

//4. Size the frame
frame.setSize(200);

//5. Show Frame
frame.setVisible(true);


Here are some details about the code:

1. The first line of code creates a frame using a constructor that set the frame title.

2. The EXIT_ON_CLOSE operation exits the program when your user closes the frame. 

3. The next bit of code adds a blank label to the frame content pane. For frames that have menus, you'd typically add the menu bar to the frame here using the setJMenuBar method.

4. This method sizes the frame to 200. We may change it.

5. The method setVisible(true) makes the frame appear onscreen.


No comments:

Post a Comment