Wednesday, 13 November 2013

6 Ways to Create a JInternalFrame - Example

The following example illustrates 6 ways to create a JInternalFrame easily using its six constructors.

import javax.swing.*;
import java.awt.*;
class JInternalFrameExample extends JFrame
{
JInternalFrame f1,f2,f3,f4,f5,f6;
JDesktopPane pane;

    public JInternalFrameExample()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("JInternalFrame Example");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
      
        pane=new JDesktopPane();
        pane.setBackground(Color.GRAY);
      
        // No title,not resizable,not iconifiable,not maximizable, not closable, no icon
        f1=new JInternalFrame();
      
        // Only title, others same as above
        f2=new JInternalFrame("Internal Frame 2");
      
        // title and resizable
        f3=new JInternalFrame("Internal Frame 3",true);
      
        // title, resizable, closable
        f4=new JInternalFrame("Internal Frame 4",true,true);
      
        // title, resizable, closable, maximizable
        f5=new JInternalFrame("Internal Frame 5",true,true,true);
      
        // title, resizable, closable, maximizable, iconifiable
        f6=new JInternalFrame("Internal Frame 6",true,true,true,true);
      
        // Set title for f4
        f4.setTitle("Internal Frame 1");
      
        // Set sizes
        f1.setSize(150,150);
        f2.setSize(150,150);
        f3.setSize(150,150);
        f4.setSize(150,150);
        f5.setSize(150,150);
        f6.setSize(150,150);
      
        // Make all visible
        f1.setVisible(true);
        f2.setVisible(true);
        f3.setVisible(true);
        f4.setVisible(true);
        f5.setVisible(true);
        f6.setVisible(true);
      
        f1.setLocation(20,20);
        f2.setLocation(40,40);
        f3.setLocation(60,60);
        f4.setLocation(80,80);
        f5.setLocation(100,100);
        f6.setLocation(120,120);
      
        // Add all!
        pane.add(f1);
        pane.add(f2);
        pane.add(f3);
        pane.add(f4);
        pane.add(f5);
        pane.add(f6);
      
        add(pane);
      
        setSize(400,400);
        setVisible(true);
    }
   
    public static void main(String args[])
    {
        new JInternalFrameExample();
    }
}

JInternalFrame(String title, boolean resizable, boolean closeable, boolean maximizable, boolean iconifiable): This is the sixth constructor, If you can understand this, you'll understand all the remaining constructors. title represents the title of the internal frame, next is resizable whether the frame can be resized, whether the frame can be closed, whether it can be maximized and iconified. The default values of these is false and there will be no title initially. If any of the parameter is not specified in the constructor, the boolean value is false. We need not add JInternalFrame to JDesktopPane but we generally do it. In this way, we can easily create six JInternalFrames using its constructors.

Three things cannot be long hidden: the sun, the moon, and the truth. - Buddha

No comments:

Post a Comment