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

📄 html.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 2 页
字号:
        showLeft(leftBar());        document.getElementById("ToggleLeft").style.display="inline";    }    </script>    </head><body onload="load()">    <div id=TopBar> <div id=TopBar_tr> <div id=TopBar_tl> <div id=TopBar_br> <div id=TopBar_bl>                         <div id=TopBar_right>                             <a target="_blank"                                href="http://java.sun.com/javase/6/download.jsp">Download                                the JDK</a>                            <br>                            <a href="../../search.html" target="_blank">Search the                                Tutorials</a>                            <br>                            <a href="javascript:toggleLeft()"                                id="ToggleLeft">Hide the TOC</a>                        </div>                    </div> </div> </div> </div> </div>    <div class=PrintHeaders>        <b>Trail:</b> Creating a GUI with JFC/Swing        <br><b>Lesson:</b> Using Swing Components    </div>    <div id=LeftBar class=LeftBar_shown>        <div id=Contents>            <div class="linkLESSON"><a href="index.html">Using Swing Components</a></div><div class="linkAHEAD"><a href="components.html">A Visual Index to the Swing Components</a></div><div class="nolinkAHEAD">Using HTML in Swing Components</div><div class="linkAHEAD"><a href="toplevel.html">Using Top-Level Containers</a></div><div class="linkAHEAD"><a href="model.html">Using Models</a></div><div class="linkAHEAD"><a href="jcomponent.html">The JComponent Class</a></div><div class="linkAHEAD"><a href="text.html">Using Text Components</a></div><div class="linkBHEAD"><a href="generaltext.html">Text Component Features</a></div><div class="linkBHEAD"><a href="textapi.html">The Text Component API</a></div><div class="linkAHEAD"><a href="componentlist.html">How to Use Various Components</a></div><div class="linkBHEAD"><a href="applet.html">How to Make Applets</a></div><div class="linkBHEAD"><a href="button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a></div><div class="linkBHEAD"><a href="colorchooser.html">How to Use Color Choosers</a></div><div class="linkBHEAD"><a href="combobox.html">How to Use Combo Boxes</a></div><div class="linkBHEAD"><a href="dialog.html">How to Make Dialogs</a></div><div class="linkBHEAD"><a href="editorpane.html">How to Use Editor Panes and Text Panes</a></div><div class="linkBHEAD"><a href="filechooser.html">How to Use File Choosers</a></div><div class="linkBHEAD"><a href="formattedtextfield.html">How to Use Formatted Text Fields</a></div><div class="linkBHEAD"><a href="frame.html">How to Make Frames (Main Windows)</a></div><div class="linkBHEAD"><a href="internalframe.html">How to Use Internal Frames</a></div><div class="linkBHEAD"><a href="label.html">How to Use Labels</a></div><div class="linkBHEAD"><a href="layeredpane.html">How to Use Layered Panes</a></div><div class="linkBHEAD"><a href="list.html">How to Use Lists</a></div><div class="linkBHEAD"><a href="menu.html">How to Use Menus</a></div><div class="linkBHEAD"><a href="panel.html">How to Use Panels</a></div><div class="linkBHEAD"><a href="passwordfield.html">How to Use Password Fields</a></div><div class="linkBHEAD"><a href="progress.html">How to Use Progress Bars</a></div><div class="linkBHEAD"><a href="rootpane.html">How to Use Root Panes</a></div><div class="linkBHEAD"><a href="scrollpane.html">How to Use Scroll Panes</a></div><div class="linkBHEAD"><a href="separator.html">How to Use Separators</a></div><div class="linkBHEAD"><a href="slider.html">How to Use Sliders</a></div><div class="linkBHEAD"><a href="spinner.html">How to Use Spinners</a></div><div class="linkBHEAD"><a href="splitpane.html">How to Use Split Panes</a></div><div class="linkBHEAD"><a href="tabbedpane.html">How to Use Tabbed Panes</a></div><div class="linkBHEAD"><a href="table.html">How to Use Tables</a></div><div class="linkBHEAD"><a href="textarea.html">How to Use Text Areas</a></div><div class="linkBHEAD"><a href="textfield.html">How to Use Text Fields</a></div><div class="linkBHEAD"><a href="toolbar.html">How to Use Tool Bars</a></div><div class="linkBHEAD"><a href="tooltip.html">How to Use Tool Tips</a></div><div class="linkBHEAD"><a href="tree.html">How to Use Trees</a></div><div class="linkAHEAD"><a href="icon.html">How to Use Icons</a></div><div class="linkAHEAD"><a href="border.html">How to Use Borders</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Component Problems</a></div></div>    </div>    <div id=MainFlow class=MainFlow_indented>            <span id=BreadCrumbs>                <a href=../../index.html target=_top>Home Page</a>                &gt;                <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a>                &gt;                <a href=index.html target=_top>Using Swing Components</a>            </span>            <div class=NavBit>                <a target=_top href=components.html>&laquo;&nbsp;Previous</a>&nbsp;&bull;&nbsp;<a target=_top href=../TOC.html>Trail</a>&nbsp;&bull;&nbsp;<a target=_top href=toplevel.html>Next&nbsp;&raquo;</a>            </div>            <div id=PageTitle>Using HTML in Swing Components</div>            <blockquote><!-- Using HTML in Swing Components -->Many Swing components display a text stringas part of their GUI.By default, a component's text is displayedin a single font and color,all on one line.You can determine the font and color of a component's textby invoking the component's <code>setFont</code> and <code>setForeground</code>methods, respectively.For example, the following codecreates a label and then sets its font and color:<blockquote><pre>label = new JLabel("A label");label.setFont(new Font("Serif", Font.PLAIN, 14));label.setForeground(new Color(0xffffdd));</pre></blockquote>If you want to mix fonts or colors within the text,or to have formatting such as multiple lines,you can use HTML.HTML formatting can be used in all Swing buttons, menu items,labels, tool tips, and tabbed panes,as well as in components such as trees and tablesthat use labels as renderers.<p>To specify that a component's text has HTML formatting,just put the <code>&lt;html&gt;</code> tag at the beginning of the text,then use any valid HTML in the remainder.Here is an example of using HTML in a button's text:<blockquote><pre>button = new JButton("&lt;html&gt;&lt;b&gt;&lt;u&gt;T&lt;/u&gt;wo&lt;/b&gt;&lt;br&gt;lines&lt;/html&gt;");</pre></blockquote>Here's the resulting button.<IMG SRC="../../figures/uiswing/components/HtmlButtonMetal.png" WIDTH="61" HEIGHT="42" ALT="Screenshot of a button that shows HTML in the Metal look and feel."><blockquote><hr><strong>Performance note:</strong>&nbsp;Because Swing's HTML rendering support uses many classes,users on oldersystems might notice a delay the first timea component with HTML formatting is shown. One way toavoid the possible delay is not to showthe HTML-formatted component immediately, and tocreate it (or another component that uses HTML)on a background thread.<hr></blockquote></blockquote><h3>An Example: HtmlDemo</h3><blockquote>The following snapshot shows an application called HtmlDemo thatlets you play with HTML formattingby setting the text on a label.<p><p><center><IMG SRC="../../figures/uiswing/components/HtmlDemoMetal.png" WIDTH="536" HEIGHT="300" ALIGN="BOTTOM" ALT="Screenshot of HtmlDemo in the Metal look and feel."></center></p><blockquote><hr><strong>Try This:</strong>&nbsp;<ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/HtmlDemo.jnlp">Run HtmlDemo</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#HtmlDemo">example index</a>.</li><li>Edit the HTML in the text area at the leftand click the "Change the label" button. Thelabel at the right shows the result.</li><li>Remove &lt;html&gt; from the text area on the left.The label's text is no longer parsed as HTML.</li></ol><hr></blockquote></blockquote><h3>Example 2: ButtonHtmlDemo</h3><blockquote>Let's look at another example that uses HTML.ButtonHtmlDemo adds font, color, and other text formattingto three buttons.You can find its full source code in<a class="SourceLink" target="_blank" href="examples/ButtonHtmlDemo.java"><code>ButtonHtmlDemo.java</code></a> and links to its image files in the<a href="examples/index.html#ButtonHtmlDemo">example index</a>.You can <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/ButtonHtmlDemo.jnlp"><b>run ButtonHtmlDemo</b></a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>.<p><p><center><IMG SRC="../../figures/uiswing/components/ButtonHtmlDemoMetal.png" WIDTH="403" HEIGHT="96" ALIGN="BOTTOM" ALT="Screenshot of ButtonHtmlDemo in the Metal look and feel."></center></p><p>The left and right buttons have multiple lines and text stylesand are implemented usingHTML. The middle button, on the other hand,uses just one line, font, and color, so it doesn'trequire HTML. Here is the code that specifiesthe text formatting for these three buttons:<blockquote><pre>b1 = new JButton("&lt;html&gt;&lt;center&gt;&lt;b&gt;&lt;u&gt;D&lt;/u&gt;isable&lt;/b&gt;&lt;br&gt;"                 + "&lt;font color=#ffffdd&gt;middle button&lt;/font&gt;",                 leftButtonIcon);Font font = b1.getFont().deriveFont(Font.PLAIN);b1.setFont(font);...b2 = new JButton("middle button", middleButtonIcon);b2.setFont(font);b2.setForeground(new Color(0xffffdd));...b3 = new JButton("&lt;html&gt;&lt;center&gt;&lt;b&gt;&lt;u&gt;E&lt;/u&gt;nable&lt;/b&gt;&lt;br&gt;"                 + "&lt;font color=#ffffdd&gt;middle button&lt;/font&gt;",                 rightButtonIcon);b3.setFont(font);</pre></blockquote>Note that we had to use a <code>&lt;u&gt;</code> tag to causethe mnemonic characters "D" and "E" to be underlined inthe HTML-using buttons. Note also that whena button is disabled, its HTML text unfortunatelyremains black, instead of becoming gray.(Keep an eye on<a class="OutsideLink" target="_blank" href="http://developer.java.sun.com/developer/bugParade/bugs/4783068.html">bug #4783068</a> to see if this situation changes.)<p>This section discussed how to use HTML in ordinary,non-text components.For information on components whose primary purpose is formatting text,see<a class="TutorialLink" target="_top" href="text.html">Using Text Components</a>.        </blockquote>        <div class=NavBit>            <a target=_top href=components.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=toplevel.html>Next &raquo;</a>        </div>    </div>    <div id=Footer><div id=TagNotes>    Problems with the examples? Try <a target="_blank"        href=../../information/run-examples.html>Compiling and Running        the Examples: FAQs</a>.    <br>    Complaints? Compliments? Suggestions? <a target="_blank"        href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give    us your feedback</a>.<br><br>    <a target="_blank" href="../../information/copyright.html">Copyright</a>    1995-2006 Sun Microsystems, Inc.  All rights reserved.    <span id=Download></span></div>     </div>    <div class=PrintHeaders>        <b>Previous page:</b> A Visual Index to the Swing Components        <br><b>Next page:</b> Using Top-Level Containers    </div>    </body></html> 

⌨️ 快捷键说明

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