Java Tutorial/Swing/JDesktopPane
Содержание
- 1 Adding Internal Frames to a JDesktopPane
- 2 Create a virtual desktop in your application
- 3 Creating a JDesktopPane Container
- 4 Customizing a JDesktopPane Look and Feel
- 5 extends JDesktopPane implements Scrollable
- 6 Getting All Frames in a JDesktopPane Container
- 7 JDesktopPane is a specialized JLayeredPane.
- 8 JDesktopPane.PALETTE_LAYER
Adding Internal Frames to a JDesktopPane
The JDesktopPane doesn"t implement RootPaneContainer.
<source lang="java">
import java.awt.BorderLayout; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; public class AddingInternalFramestoaJDesktopPane {
public static void main(final String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JDesktopPane desktop = new JDesktopPane(); JInternalFrame internalFrame = new JInternalFrame("Can Do All", true, true, true, true); desktop.add(internalFrame); internalFrame.setBounds(25, 25, 200, 100); JLabel label = new JLabel(internalFrame.getTitle(), JLabel.CENTER); internalFrame.add(label, BorderLayout.CENTER); internalFrame.setVisible(true); frame.add(desktop, BorderLayout.CENTER); frame.setSize(500, 300); frame.setVisible(true); }
}</source>
Create a virtual desktop in your application
<source lang="java">
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")); }
}</source>
Creating a JDesktopPane Container
<source lang="java">
import java.awt.BorderLayout; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JTextArea; public class Main {
public static void main(String[] argv) throws Exception { boolean resizable = true; boolean closeable = true; boolean maximizable = true; boolean iconifiable = true; String title = "Frame Title"; JInternalFrame iframe = new JInternalFrame(title, resizable, closeable, maximizable, iconifiable); iframe.setSize(300, 300); iframe.setVisible(true); iframe.getContentPane().add(new JTextArea()); JDesktopPane desktop = new JDesktopPane(); desktop.add(iframe); JFrame frame = new JFrame(); frame.getContentPane().add(desktop, BorderLayout.CENTER); frame.setSize(300, 300); frame.setVisible(true); }
}</source>
Customizing a JDesktopPane Look and Feel
Property StringObject TypedesktopColorDesktop.ancestorInputMapInputMapDesktop.backgroundColorDesktop.windowBindingsObject[ ]DesktopPane.actionMapActionMapDesktopPaneUIString
extends JDesktopPane implements Scrollable
<source lang="java">
import java.awt.Dimension; import java.awt.Rectangle; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.Scrollable; class ScrollDesktop extends JDesktopPane implements Scrollable {
public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); } public int getScrollableUnitIncrement(Rectangle r, int axis, int dir) { return 50; } public int getScrollableBlockIncrement(Rectangle r, int axis, int dir) { return 200; } public boolean getScrollableTracksViewportWidth() { return false; } public boolean getScrollableTracksViewportHeight() { return false; }
} public class Main extends JFrame {
public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JDesktopPane desk = new ScrollDesktop(); desk.setPreferredSize(new Dimension(1000, 1000)); getContentPane().add(new JScrollPane(desk), "Center"); JInternalFrame f1 = new JInternalFrame("Frame 1"); f1.getContentPane().add(new JLabel("This is frame f1")); f1.setResizable(true); f1.pack(); f1.setVisible(true); desk.add(f1, new Integer(10)); JInternalFrame f2 = new JInternalFrame("Frame 2"); f2.getContentPane().add(new JLabel("Content for f2")); f2.setResizable(true); f2.pack(); f2.setVisible(true); desk.add(f2, new Integer(20)); JInternalFrame f3 = new JInternalFrame("Frame 3"); f3.getContentPane().add(new JLabel("Content for f3")); f3.setResizable(true); f3.pack(); f3.setVisible(true); desk.add(f3, new Integer(20)); f3.toFront(); try { f3.setSelected(true); } catch (java.beans.PropertyVetoException ignored) { } pack(); setSize(300, 300); setVisible(true); } public static void main(String arg[]) { new Main(); }
}</source>
Getting All Frames in a JDesktopPane Container
<source lang="java">
import javax.swing.JDesktopPane; import javax.swing.JInternalFrame; public class Main {
public static void main(String[] argv) throws Exception { JDesktopPane desktop = new JDesktopPane(); JInternalFrame[] frames = desktop.getAllFrames(); for (int i = 0; i < frames.length; i++) { String title = frames[i].getTitle(); boolean isVisible = frames[i].isVisible(); boolean isCloseable = frames[i].isClosable(); boolean isResizeable = frames[i].isResizable(); boolean isIconifiable = frames[i].isIconifiable(); boolean isIcon = frames[i].isIcon(); boolean isMaximizable = frames[i].isMaximizable(); boolean isSelected = frames[i].isSelected(); } }
}</source>
JDesktopPane is a specialized JLayeredPane.
The management of the frames within a desktop is the responsibility of a DesktopManager, in which the default implementation that"s provided is DefaultDesktopManager.
JDesktopPane.PALETTE_LAYER
<source lang="java">
import java.awt.BorderLayout; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame;
public class InternalFramePaletteSample {
public static void main(final String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JDesktopPane desktop = new JDesktopPane(); JInternalFrame palette = new JInternalFrame("Palette", true, false, true, false); palette.setBounds(350, 150, 100, 100); palette.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE); desktop.add(palette, JDesktopPane.PALETTE_LAYER); palette.setVisible(true); frame.add(desktop, BorderLayout.CENTER); frame.setSize(500, 300); frame.setVisible(true); }
}</source>