Create a virtual desktop in your application : JDesktopPane « Swing « Java Tutorial






import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class Main extends JFrame {
  public Main() {
    JMenuBar bar = new JMenuBar();
    JMenu addMenu = new JMenu("Add");
    JMenuItem newFrame = new JMenuItem("Internal Frame");
    addMenu.add(newFrame);
    bar.add(addMenu);
    setJMenuBar(bar);

    final JDesktopPane theDesktop = new JDesktopPane();
    getContentPane().add(theDesktop);

    newFrame.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JInternalFrame frame = new JInternalFrame("Internal Frame", true, true, true, true);

        Container c = frame.getContentPane();
        MyJPanel panel = new MyJPanel();

        c.add(panel, BorderLayout.CENTER);
        frame.setSize(200,200);
        frame.setOpaque(true);
        theDesktop.add(frame);
      }
    });

    setSize(500, 400);
    setVisible(true);
  }

  public static void main(String args[]) {
    Main app = new Main();
  }
}

class MyJPanel extends JPanel {
  public MyJPanel() {
    add(new JLabel("adf"));
  }

}








14.54.JDesktopPane
14.54.1.JDesktopPane is a specialized JLayeredPane.
14.54.2.Adding Internal Frames to a JDesktopPaneAdding Internal Frames to a JDesktopPane
14.54.3.JDesktopPane.PALETTE_LAYERJDesktopPane.PALETTE_LAYER
14.54.4.extends JDesktopPane implements Scrollable
14.54.5.Getting All Frames in a JDesktopPane Container
14.54.6.Creating a JDesktopPane Container
14.54.7.Create a virtual desktop in your application
14.54.8.Customizing a JDesktopPane Look and Feel