Java JDesktopPane extend

Description

Java JDesktopPane extend


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

public class Main extends JDesktopPane {

  public Main() {
      setDragMode(JDesktopPane.LIVE_DRAG_MODE);
      //setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
  }/*  w ww. j  a  v a2  s.  c o m*/

  public static void main(String[] args) {
      final Main c = new Main();
      c.setPreferredSize(new Dimension(433, 312));
      JFrame frame = new JFrame("java2s.com");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.add(c);
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("File");
      JMenuItem newWindowMenuItem = new JMenuItem("New Internal Frame");
      newWindowMenuItem.setMnemonic(KeyEvent.VK_N);
      newWindowMenuItem.setAccelerator(KeyStroke.getKeyStroke(
              KeyEvent.VK_N, ActionEvent.CTRL_MASK));
 
      newWindowMenuItem.addActionListener(new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
              JInternalFrame frame = new InternalFrame();
              frame.setVisible(true);
              c.add(frame);
              try {
                  frame.setSelected(true);
              } catch (PropertyVetoException pve) {
              }
          }
      });
      menu.add(newWindowMenuItem);
      menuBar.add(menu);
      frame.setJMenuBar(menuBar);
      frame.setSize(300, 250);
      frame.setVisible(true);
      
  }
}

class InternalFrame extends JInternalFrame {

  static int count = 0;
  static final int xOffset = 35;
  static final int yOffset = 35;


  public InternalFrame() {
      super("Window #" + (count++),
              true, //resizable
              true, //closable
              true, //maximizable
              true);//iconifiable
      
      

      setLayout(new BorderLayout());
      JTextArea ta = new JTextArea(""+count);
      JScrollPane sp = new JScrollPane(ta);
      ta.setLineWrap(true);
      ta.setWrapStyleWord(true);
      add(sp, BorderLayout.CENTER);
      setSize(200, 100);

      // Stagger windows
      setLocation(xOffset * count, yOffset * count);
  }
}



PreviousNext

Related