📄 internalframe.html
字号:
around frames or internal frames.Switching from internal frames to framesor vice versaisn't necessarily a simple task.By experimenting with both frames and internal frames,you can get an idea of the tradeoffs involvedin choosing one over the other.<p>Here is a picture of an applicationthat has two internal frames(one of which is iconified)inside a regular frame:<p><center><IMG SRC="../../figures/uiswing/components/InternalFrameDemoMetal.png" WIDTH="278" HEIGHT="180" ALIGN="BOTTOM" ALT="InternalFrameDemo has multiple internal frames, managed by a desktop pane"></center></p><p><blockquote><hr><strong>Try this:</strong> <ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/InternalFrameDemo.jnlp">Run InternalFrameDemo</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>. Or, to compile and run the example yourself, consult the <a href="examples/index.html#InternalFrameDemo">example index</a>.<li> Create new internal frames using the Create item in the Document menu. <br> Each internal frame comes up 30 pixels lower and to the right of the place where the previous internal frame first appeared. This functionality is implemented in the <code>MyInternalFrame</code> class, which is the custom subclass of <code>JInternalFrame</code>.</ol><hr></blockquote><p>The following code,taken from<a class="SourceLink" target="_blank" href="examples/InternalFrameDemo.java"><code>InternalFrameDemo.java</code></a>,creates the desktop and internal frames in the previous example.<blockquote><pre><em>...//In the constructor of InternalFrameDemo, a JFrame subclass:</em> desktop = new JDesktopPane(); createFrame(); //Create first window setContentPane(desktop); ... //Make dragging a little faster but perhaps uglier. desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);...protected void createFrame() { MyInternalFrame frame = new MyInternalFrame(); frame.setVisible(true); desktop.add(frame); try { frame.setSelected(true); } catch (java.beans.PropertyVetoException e) {}}<em>...//In the constructor of MyInternalFrame, a JInternalFrame subclass:</em>static int openFrameCount = 0;static final int xOffset = 30, yOffset = 30;public MyInternalFrame() { super("Document #" + (++openFrameCount), true, //resizable true, //closable true, //maximizable true);//iconifiable //...Create the GUI and put it in the window... //...Then set the window size or call pack... ... //Set the window's location. setLocation(xOffset*openFrameCount, yOffset*openFrameCount);}</pre></blockquote></blockquote><h3><a name="frame">Internal Frames vs. Regular Frames</a></h3><blockquote>The code for using internal framesis similar in many ways to the code for using regular Swing frames.Because internal frames have root panes,setting up the GUI for a <code>JInternalFrame</code>is very similar to setting up the GUI for a <code>JFrame</code>.<code>JInternalFrame</code> also providesother API,such as <code>pack</code>,that makes it similar to <code>JFrame</code>.<blockquote><hr><strong>Note:</strong> Just as for a regular frame,you must invoke <code>setVisible(true)</code> or <code>show()</code>on an internal frame to display it.The internal frame does not appear until you explicitly make it visible.<hr></blockquote><p>Internal frames aren't windows or top-level containers, however,which makes them different from frames.For example, you must add an internal frameto a container (usually a <code>JDesktopPane</code>);an internal frame can't be the root of a containment hierarchy.Also, internal frames don't generate window events.Instead, the user actions that wouldcause a frame to fire window eventscause an internal frame to fire internal frame events.<p>Because internal frames are implemented with platform-independent code,they add some features that frames can't give you.One such feature is thatinternal frames give you more control overtheir state and capabilities than frames do.You can programatically iconify or maximize an internal frame.You can also specify what icon goes in the internal frame'stitle bar.You can even specify whether the internal framehas the window decorationsto support resizing, iconifying, closing, and maximizing.<p>Another feature is that internal framesare designed to work within desktop panes.The <code>JInternalFrame</code> APIcontains methods such as <code>moveToFront</code>that work only if the internal frame's containeris a layered pane such as a <code>JDesktopPane</code>.</blockquote><h3><a name="rules">Rules of Using Internal Frames</a></h3><blockquote>If you've built any programs using <code>JFrame</code>and the other Swing components,then you already know a lotabout how to use internal frames.The following listsummarizes the rules for using internal frames.For additional information, see<a href="frame.html">How to Make Frames</a>and<a href="jcomponent.html">The JComponent Class</a>.<dl><dt> <strong>You must set the size of the internal frame.</strong><dd> If you don't set the size of the internal frame, it will have zero size and thus never be visible. You can set the size using one of the following methods: <code>setSize</code>, <code>pack</code>, or <code>setBounds</code>.<dt> <strong>As a rule, you should set the location of the internal frame.</strong><dd> If you don't set the location of the internal frame, it will come up at 0,0 (the upper left of its container). You can use the <code>setLocation</code> or <code>setBounds</code> method to specify the upper left point of the internal frame, relative to its container.<dt> <strong>To add components to an internal frame, you add them to the internal frame's content pane.</strong><dd> This is exactly like the <code>JFrame</code> situation. See <a href="toplevel.html#contentpane">Adding Components to the Content Pane</a> for details.<dt> <strong>Dialogs that are internal frames should be implemented using <code>JOptionPane</code> or <code>JInternalFrame</code>, not <code>JDialog</code>.</strong><dd> To create a simple dialog, you can use the <code>JOptionPane</code> <code>showInternal<em>Xxx</em>Dialog</code> methods, as described in <a href="dialog.html">How to Make Dialogs</a>.<dt> <strong>You must add an internal frame to a container.</strong><dd> If you don't add the internal frame to a container (usually a <code>JDesktopPane</code>), the internal frame won't appear.<dt> <strong>You need to call <code>show</code> or <code>setVisible</code> on internal frames.</strong><dd> Internal frames are invisible by default. You must invoke <code>setVisible(true)</code> or <code>show()</code> to make them visible.<dt> <strong>Internal frames fire internal frame events, not window events.</strong><dd> Handling internal frame events is almost identical to handling window events. See <a href="../events/internalframelistener.html">How to Write an Internal Frame Listener</a> for more information.</dl><a name="drag"><blockquote><hr><strong>Performance tip:</strong> </a>When a desktop has many internal frames,the user might notice that moving them seems slow.Outline draggingis one way to avoid this problem.With outline dragging,only the outline of the internal frame is paintedat the current mouse position while the internal frame's being dragged.The internal frame's innardsare not repainted at a new position until dragging stops.The default behavior (called <em>live</em> dragging)is to reposition and repaint some or all of internal frame continuouslywhile it's being moved;this can be slow if the desktop has many internal frames.<p>Use the <code>JDesktopPane</code> method<code>setDragMode</code>*to specify outline dragging.For example:<blockquote><pre>desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);</pre></blockquote><hr></blockquote><p></blockquote><h3><a name="api">The Internal Frame API</a></h3><blockquote>The following tables list the commonly used<code>JInternalFrame</code> constructors and methods,as well as a few methods that <code>JDesktopPane</code> provides.Besides the API listed in this section,<code>JInternalFrame</code> inherits useful API fromits superclasses,<code>JComponent</code>,<code>Component</code>, and<code>Container</code>.See <a href="jcomponent.html">The JComponent Class</a>for lists of methods from those classes.<p>Like <code>JInternalFrame</code>,<code>JDesktopPane</code> descends from<code>JComponent</code>,and thus provides the methods described in<a href="jcomponent.html">The JComponent Class</a>.Because <code>JDesktopPane</code> extends<code>JLayeredPane</code>,it also supports the methods described in<a href="layeredpane.html#api">The Layered Pane API</a>.<p>The API for using internal frames falls into these categories:<ul><li><a href="#construct">Creating the internal frame</a><li><a href="#add">Adding components to the internal frame</a><li><a href="#layout">Specifying the internal frame's visibility, size, and location</a><li><a href="#window">Performing window operations on the internal frame</a><li><a href="#decorate">Controlling window decorations and capabilities</a><li><a href="#JDesktopPane">Using the JDesktopPane API</a></ul><p><table border=1><caption><a name="construct">Creating the Internal Frame</a></caption><tr><th align=left>Constructor or Method</th><th align=left>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html#JInternalFrame()">JInternalFrame()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html#JInternalFrame(java.lang.String)">JInternalFrame(String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html#JInternalFrame(java.lang.String, boolean)">JInternalFrame(String, boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html#JInternalFrame(java.lang.String, boolean, boolean)">JInternalFrame(String, boolean, boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html#JInternalFrame(java.lang.String, boolean, boolean, boolean)">JInternalFrame(String, boolean, boolean, boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html#JInternalFrame(java.lang.String, boolean, boolean, boolean, boolean)">JInternalFrame(String, boolean, boolean, boolean, boolean)</a> </td> <td>Create a <code>JInternalFrame</code> instance. The first argument specifies the title (if any) 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, maximize, and iconify the internal frame (specified in that order). The default value for each boolean argument is <code>false</code>, which means that the operation is not allowed. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html#showInternalConfirmDialog(java.awt.Component, java.lang.Object)">static int showInternalConfirmDialog(Component, Object)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html#showInternalInputDialog(java.awt.Component, java.lang.Object)">static String showInternalInputDialog(Component, Object)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html#showInternalMessageDialog(java.awt.Component, java.lang.Object)">static Object showInternalMessageDialog(Component, Object)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html#showInternalOptionDialog(java.awt.Component, java.lang.Object, java.lang.String, int, int, javax.swing.Icon, java.lang.Object[], java.lang.Object)">static int showInternalOptionDialog(Component, Object, String, int, int, Icon, Object[], Object)</a> </td> <td>Create a <code>JInternalFrame</code> that simulates a dialog. See <a href="dialog.html">How to Make Dialogs</a> for details. </td> </tr></table><p><table border=1><caption><a name="add">Adding Components to the Internal Frame</a></caption><tr><th align=left>Method</th><th align=left>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html#setContentPane(java.awt.Container)">void setContentPane(Container)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html#getContentPane()">Container getContentPane()</a> </td> <td>Set or get the internal frame's content pane, which generally contains all of the internal frame's GUI, with the exception of the menu bar and window decorations. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html#setJMenuBar(javax.swing.JMenuBar)">void setJMenuBar(JMenuBar)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html#getJMenuBar()">JMenuBar getJMenuBar()</a> </td> <td>Set or get the internal frame's menu bar.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html#setLayeredPane(javax.swing.JLayeredPane)">void setLayeredPane(JLayeredPane)</a> <br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -