⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frame.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 3 页
字号:
Here's a picture of the extremely plain window created by an example called FrameDemo.You can find the source code in<a class="SourceLink" target="_blank" href="examples/FrameDemo.java"><code>FrameDemo.java</code></a>.You can <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/FrameDemo.jnlp">run FrameDemo</a> (it requires release 6)using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>.<p><center><IMG SRC="../../figures/uiswing/components/FrameDemoMetal.png" WIDTH="183" HEIGHT="134" ALIGN="BOTTOM" ALT="A very boring frame"></center></p>The following code, taken from FrameDemo,is a typical example of the code used to create and set up a frame.<blockquote><pre><font color=gray>//1. Optional: Specify who draws the window decorations. </font>JFrame.setDefaultLookAndFeelDecorated(true);<font color=gray>//2. Create the frame.</font>JFrame frame = new JFrame("FrameDemo");<font color=gray>//3. Optional: What happens when the frame closes?</font>frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<font color=gray>//4. Create components and put them in the frame.</font><em>//...create emptyLabel...</em>frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);<font color=gray>//5. Size the frame.</font>frame.pack();<font color=gray>//6. Show it.</font>frame.setVisible(true);</pre></blockquote>Here are some details about the code:<p><ol><li>Calling <code>setDefaultLookAndFeelDecorated(true)</code> requests that any subsequently created frameshave window decorations providedby the look and feel,and not by the window system.For details, see<a href="#setDefaultLookAndFeelDecorated">Specifying Window Decorations</a>.<p><li>The next line of code creates a frame using aconstructor that lets you set the frame's title.The other frequently used <code>JFrame</code> constructor is the no-argument constructor.<p><li>Next the code specifies what should happen when the user closes the frame.The <code>EXIT_ON_CLOSE</code> operation,not surprisingly,makes the program exit when the user closes the frame.This behavior is appropriate for this programbecause the program has only one frame,and closing the frame makes the program useless.See <a href="#windowevents">Responding to Window-Closing Events</a>for more information.<p><li>The next bit of code adds a blank label to the frame's content pane.If you're not already familiar with content panesand how to add components to them,please read<a href="toplevel.html#contentpane">Adding Components to the Content Pane</a>.<p>For frames that have menus,you'd typically add the menu bar to the frame hereusing the <code>setJMenuBar</code> method.See <a href="menu.html">How to Use Menus</a> for details.<p><li>The <code>pack</code> method sizes the frameso that all its contents are ator above their preferred sizes.An alternative to <code>pack</code>is to establish a frame's size explicitlyby calling <code>setSize</code>or <code>setBounds</code>(which also sets the frame's location).In general, using <code>pack</code>is preferable to calling <code>setSize</code>,since <code>pack</code>leaves the frame's layout manager in charge of the frame's size,and layout managers are good at adjusting to platform dependenciesand other factors that affect component size. <p>This example doesn't set the frame's location,but it's easy to do so using either the <code>setLocationRelativeTo</code>or <code>setLocation</code> method.For example, the following codecenters a frame onscreen:<blockquote><pre>frame.setLocationRelativeTo(null);</pre></blockquote><p><li>Calling <code>setVisible(true)</code> makes the frame appear onscreen.Sometimes you might see the <code>show</code> method used instead.The two usages are equivalent,but we use <code>setVisible(true)</code> for consistency's sake.</ol><blockquote><hr><strong>Note:</strong>&nbsp;The <code>pack</code> and <code>setVisible</code> methods<em>realize</em> the frame and the components it contains.Once a component has been realized,any code that modifies or inspects itshould be executed on the event-dispatching thread.For more information, refer to<a class="TutorialLink" target="_top" href="../concurrency/index.html">Concurrency in Swing</a>.<hr></blockquote></blockquote><h3><a name="setDefaultLookAndFeelDecorated">Specifying Window Decorations</a></h3><blockquote>By default, window decorations are supplied by the native window system.However, you can request that the look and feelprovide the decorations for a frame.You can even specifythat the frame have no window decorations at all,a feature that can be used on its own,or to provide your own decorations,or with<a class="TutorialLink" target="_top" href="../../extra/fullscreen/index.html">full-screen exclusive mode</a>.<p>Besides specifying who provides the window decorations,you can also specify which icon is used the represent the window.Exactly how this icon is used depends on the window systemor look and feel that provides the window decorations.If the window system supports minimization,then the icon is used to represent the minimized window.Most window systems or look and feelsalso display the icon in the window decorations.A typical icon size is 16x16 pixels,but some window systems use other sizes.<p>The following snapshots show three framesthat are identicalexcept for their window decorations.As you can tell by the appearance of thebutton in each frame,all three use the Java look and feel.However, only the first and third frames use window decorationsprovided by the Java look and feel.The second uses decorations provided by the window system,which happens to be Microsoft Windowsbut could as easily be any other systemrunning the Java platform.The third frame uses Java look and feel window decorations,but has a custom icon.<blockquote><table summary="layout"><tr><td><IMG SRC="../../figures/uiswing/components/FrameDemo2_1.png" WIDTH="170" HEIGHT="100" ALT="A frame with decorations provided by the look and feel"></td><td><IMG SRC="../../figures/uiswing/components/FrameDemo2_2.png" WIDTH="170" HEIGHT="100" ALT="A frame with decorations provided by the window system"></td><td><IMG SRC="../../figures/uiswing/components/FrameDemo2_3.png" WIDTH="170" HEIGHT="100" ALT="A frame with a custom icon"></td></tr><tr><td><font size=-1>Window decorations provided by the look and feel</font></td><td><font size=-1>Window decorations provided by the window system</font></td><td><font size=-1>Custom icon; window decorations provided by the look and feel</font></td></tr></table></blockquote><p>Here is an example of creating a frame with a custom iconand with window decorations provided by the look and feel:<blockquote><pre><font color=gray>//Ask for window decorations provided by the look and feel.</font>JFrame.setDefaultLookAndFeelDecorated(true);<font color=gray>//Create the frame.</font>JFrame frame = new JFrame("A window");<font color=gray>//Set the frame's icon to an image loaded from a file.</font>frame.setIconImage(new ImageIcon(imgURL).getImage());</pre></blockquote><p>As the preceding code snippet implies, you must invoke the <code>setDefaultLookAndFeelDecorated</code> method<em>before</em> creating the framewhose decorations you wish to affect.The value you set with <code>setDefaultLookAndFeelDecorated</code> is used for all subsequently created <code>JFrame</code>s.You can switch back to using window system decorationsby invoking <code>JFrame.setDefaultLookAndFeelDecorated(false)</code>.Some look and feels might not support window decorations;in this case, the window system decorations are used.<p>The full source code for the applicationthat creates the frames pictured above is in<a class="SourceLink" target="_blank" href="examples/FrameDemo2.java"><code>FrameDemo2.java</code></a>.Besides showing how to choose window decorations,FrameDemo2 also shows how to disable all window decorationsand gives an example of positioning windows.It includes two methods that create the <code>Image</code> objectsused as icons &#151;one is loaded from a file,and the other is painted from scratch.<blockquote><hr><strong>Try this:</strong>&nbsp;<ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/FrameDemo2.jnlp">Run FrameDemo2</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#FrameDemo2">example index</a>.<li> Bring up two windows, both with look-and-feel-provided decorations,     but with different icons.     <br>     The Java look and feel displays the icons     in its window decorations.     Depending on your window system,     the icon may be used elsewhere to represent the window,     especially when the window is minimized.<li> Bring up one or more windows with window system decorations.     <br>     See if your window system treats these icons differently.<li> Bring up one or more windows with no window decorations.     <br>     Play with the various types of windows      to see how the window decorations,     window system, and frame icons interact.</ol><hr></blockquote></blockquote><a name="windowevents"><h3>Responding to Window-Closing Events</h3></a><blockquote>By default,when the user closes a frame onscreen,the frame is hidden.Although invisible,the frame still exists and the program can make it visible again.If you want different behavior,then you need to eitherregister a window listenerthat handles window-closing events,or you need to specify default close behavior usingthe <code>setDefaultCloseOperation</code> method.You can even do both.<p>The argument to <code>setDefaultCloseOperation</code>must be one of the following values,the first three of which are defined in the <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/WindowConstants.html"><code>WindowConstants</code></a> interface (implemented by <code>JFrame</code>,  <code>JInternalPane</code>, and <code>JDialog</code>):<dl><dt><code>DO_NOTHING_ON_CLOSE</code><dd> 	Don't do anything when the user requests that the window close.	Instead, the program should probably use a window listener	that performs some other action in its <code>windowClosing</code>	method.<dt><code>HIDE_ON_CLOSE</code>    (the default for <code>JDialog</code> and <code>JFrame</code>)<dd>	Hide the window when the user closes it.	This removes the window from the screen	but leaves it displayable.<dt><code>DISPOSE_ON_CLOSE</code>    (the default for <code>JInternalFrame</code>)<dd>	Hide and dispose of the window when the user closes it.	This removes the window from the screen and	frees up any resources used by it.<dt><code>EXIT_ON_CLOSE</code>        (defined in the <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JFrame.html"><code>JFrame</code></a> class)<dd>	Exit the application, using <code>System.exit(0)</code>.        This is recommended for applications only.  If used        within an applet, a <code>SecurityException</code>        may be thrown.</dl><a name="4030718"><blockquote><hr><strong>Note:</strong>&nbsp;</a><code>DISPOSE_ON_CLOSE</code> can have results similar to <code>EXIT_ON_CLOSE</code> if only one window is onscreen.More precisely, when the last displayable window within the Java virtual machine (VM) is disposed of,the VM may terminate.See <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/doc-files/AWTThreadIssues.html">AWT Threading Issues</a>    for details.<hr></blockquote>The default close operation is executed after the anywindow listeners handle the window-closing event.So, for example, assume that you specify that the default closeoperation is to dispose of a frame.You also implement a window listenerthat tests whether the frame is the last one visibleand, if so, saves some data and exits the application.Under these conditions,when the user closes a frame,the window listener will be called first.If it doesn't exit the application,then the default close operation &#151;disposing of the frame &#151;will then be performed.<p>For more information about handling window-closing events,see <a class="TutorialLink" target="_top" href="../events/windowlistener.html">How to Write Window Listeners</a>.Besides handling window-closing events,window listeners can also react toother window state changes,such as iconification and activation.</blockquote><h3><a name="api">The Frame API</a></h3>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -