Bitcoin

Bitcoin
Bitcoin

JInternalFrame

Java Swing Tutorial Explaining the JInternalFrame class. A JInternalFrame is confined to a visible area of a container it is placed in. JInternalFrame a top level swing component that has a contentpane.

  • It can be iconified — in this case the icon remains in the main application container.
  • It can be maximized — Frame consumes the main application
  • It can be closed using standard popup window controls
  • It can be layered

JAVA JINTERNALFRAME HIERARCHY

javax.swing
Class JInternalFrame 
java.lang.Object
|
+–java.awt.Component
|
+–java.awt.Container
|
+–javax.swing.JComponent
|
+–javax.swing.JInternalFrame
All Implemented Interfaces: 
Accessible, ImageObserver, MenuContainer, RootPaneContainer, Serializable, WindowConstants

JINTERNALFRAME CONSTRUCTOR

JInternalFrame()
Creates a non-resizable, non-closable, non-maximizable, non-iconifiable JInternalFrame with no title.
JInternalFrame(String title)
Creates a non-resizable, non-closable, non-maximizable, non-iconifiable JInternalFrame with the specified title.
JInternalFrame(String title, boolean resizable)
Creates a non-closable, non-maximizable, non-iconifiable JInternalFrame with the specified title and resizability.
JInternalFrame(String title, boolean resizable, boolean closable)
Creates a non-maximizable, non-iconifiable JInternalFrame with the specified title, resizability, and closability.

JInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable)
Creates a non-iconifiable JInternalFrame with the specified title, resizability, closability, and maximizability.

JInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable)
Creates a JInternalFrame with the specified title, resizability, closability, maximizability

DECLARATION OF JINTERNALFRAME

JInternalFrame jn = new JInternalFrame(“InternalFrame”,true,true,true);

The first argument specifies the title to be displayed by the internal frame. The rest of the arguments specify whether the internal frame should contain decorations allowing the user to resize, close, and maximize the internal frame . The default value for each boolean argument is false, which means that the operation is not allowed.

JINTERNALFRAME SOURCE CODE

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();
    }
}
THE OUTPUT IS:

ANOTHER CODE ON JINTERNALFRAME
import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import java.awt.event.*;
import java.awt.*;

public class JInternalFrameDemo extends JFrame {

 JDesktopPane jdpDesktop;
 static int openFrameCount = 0;
 public JInternalFrameDemo() {
  super("JInternalFrame Usage Demo");
  // Make the main window positioned as 50 pixels from each edge of the
  // screen.
  int inset = 50;
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  setBounds(inset, inset, screenSize.width - inset * 2,
    screenSize.height - inset * 2);
  // Add a Window Exit Listener
  addWindowListener(new WindowAdapter() {

   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });
  // Create and Set up the GUI.
  jdpDesktop = new JDesktopPane();
  // A specialized layered pane to be used with JInternalFrames
  createFrame(); // Create first window
  setContentPane(jdpDesktop);
  setJMenuBar(createMenuBar());
  // Make dragging faster by setting drag mode to Outline
  jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
 }
 protected JMenuBar createMenuBar() {
  JMenuBar menuBar = new JMenuBar();
  JMenu menu = new JMenu("Frame");
  menu.setMnemonic(KeyEvent.VK_N);
  JMenuItem menuItem = new JMenuItem("New IFrame");
  menuItem.setMnemonic(KeyEvent.VK_N);
  menuItem.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent e) {
    createFrame();
   }
  });
  menu.add(menuItem);
  menuBar.add(menu);
  return menuBar;
 }
 protected void createFrame() {
  MyInternalFrame frame = new MyInternalFrame();
  frame.setVisible(true);
  // Every JInternalFrame must be added to content pane 
  //using JDesktopPane
  jdpDesktop.add(frame);
  try {
   frame.setSelected(true);
  } catch (java.beans.PropertyVetoException e) {
  }
 }
 public static void main(String[] args) {
  JInternalFrameDemo frame = new JInternalFrameDemo();
  frame.setVisible(true);
 }
 class MyInternalFrame extends JInternalFrame {

  static final int xPosition = 30, yPosition = 30;
  public MyInternalFrame() {
   super("IFrame #" + (++openFrameCount), true, // resizable
     true, // closable
     true, // maximizable
     true);// iconifiable
   setSize(300, 300);
   // Set the window's location.
   setLocation(xPosition * openFrameCount, yPosition
     * openFrameCount);
  }
 }
}
THE OUTPUT IS:
ANOTHER JINTERNALFRAME CODE
import javax.swing.*;
 
public class JInternalFrameExample {
 
  public static void main(String[] args) {
 
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JInternalFrame Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,300);
 
    JDesktopPane desktop = new JDesktopPane();
 
    JInternalFrame if1 = new JInternalFrame("Frame 1", true, true, true, true );
    if1.setSize(200,200);
    desktop.add(if1);
 
    JInternalFrame if2 = new JInternalFrame("Frame 2", true, true, true, true );
    if2.setSize(200,200);
    desktop.add(if2);
 
    if1.setLocation(20,20);
    if1.setVisible(true);
    if2.setLocation(40,40);
    if2.setVisible(true);
 
    frame.add(desktop);
    frame.setVisible(true);
  }
}
THE OUTPUT IS:

COMMENT BELOW IF THE CODE SNIPPETS ARE USEFUL

No comments:

Post a Comment

Facebook