TestDesktop.java
Select all
/* TestDesktop.java CIS 260 2/22/2006 David Klick Demonstration of a JDesktopPane used to hold multiple JInternalFrames (similar to an MDI application in Windows). The use of a JMenuBar is also demonstrated. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; public class TestDesktop extends JFrame { Container c; JDesktopPane desktop; static int count = 0; public static void main(String[] args) { TestDesktop app = new TestDesktop("Internal Frame Test"); } public TestDesktop() { this(""); } public TestDesktop(String title) { super(title); setDefaultCloseOperation(EXIT_ON_CLOSE); c = getContentPane(); c.setLayout(new BorderLayout()); JMenuBar mbar = new JMenuBar(); mbar.setBorder(new BevelBorder(BevelBorder.RAISED)); JMenu mnuFile = new JMenu("File"); mnuFile.setMnemonic('F'); mbar.add(mnuFile); JMenuItem mnuItemNew = new JMenuItem("New"); mnuItemNew.setMnemonic('N'); mnuItemNew.addActionListener(new MyMenuListener()); mnuFile.add(mnuItemNew); setJMenuBar(mbar); desktop = new JDesktopPane(); c.add(desktop, BorderLayout.CENTER); setSize(400, 400); setVisible(true); } class MyMenuListener implements ActionListener { public void actionPerformed(ActionEvent e) { JInternalFrame iframe = new JInternalFrame( "Frame #" + (count+1), true, true, true, true); iframe.setSize(200, 100); iframe.setLocation(10*count, 10*count); iframe.setVisible(true); desktop.add(iframe); count++; System.out.println("Frame count: " + count); iframe.addInternalFrameListener(new InternalFrameAdapter() { public void internalFrameClosing(InternalFrameEvent e2) { count--; System.out.println("Frame count: " + count); } }); try { iframe.setSelected(true); } catch (Exception e3) { e3.printStackTrace(); } } } }