Documentation

The Java™ Tutorials
Hide TOC
How to Use HTML in Swing Components如何在Swing组件中使用HTML
Trail: Creating a GUI With Swing
Lesson: Using Swing Components

How to Use HTML in Swing Components如何在Swing组件中使用HTML

Many Swing components display a text string as part of their GUI. 许多Swing组件将文本字符串显示为其GUI的一部分。By default, a component's text is displayed in a single font and color, all on one line. 默认情况下,组件的文本以单一字体和颜色显示,全部显示在一行中。You can determine the font and color of a component's text by invoking the component's setFont and setForeground methods, respectively. 您可以通过分别调用组件的setFontsetForeground方法来确定组件文本的字体和颜色。For example, the following code creates a label and then sets its font and color:例如,以下代码创建标签,然后设置其字体和颜色:

label = new JLabel("A label");
label.setFont(new Font("Serif", Font.PLAIN, 14));
label.setForeground(new Color(0xffffdd));

If you want to mix fonts or colors within the text, or if you want formatting such as multiple lines, you can use HTML. 如果要在文本中混合字体或颜色,或者需要多行格式,可以使用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 tables that use labels to render text.HTML格式可用于所有Swing按钮、菜单项、标签、工具提示和选项卡式窗格,以及使用标签呈现文本的树和表等组件。

To specify that a component's text has HTML formatting, just put the <html> tag at the beginning of the text, then use any valid HTML in the remainder. 要指定组件的文本具有HTML格式,只需将<html>标记放在文本的开头,然后在剩余部分使用任何有效的HTML。Here is an example of using HTML in a button's text:以下是在按钮文本中使用HTML的示例:

button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");

Here is the resulting button. 这是结果按钮。Screenshot of a button that shows HTML in the Metal look and feel.

An Example: HtmlDemo例如:HtmlDemo

An application called HtmlDemo lets you play with HTML formatting by setting the text on a label. 一个名为HtmlDemo的应用程序允许您通过设置标签上的文本来处理HTML格式。You can find the entire code for this program in HtmlDemo.java. 您可以在HtmlDemo.java中找到该程序的完整代码。Here is a picture of the HtmlDemo example.以下是HtmlDemo示例的图片。

Screenshot of HtmlDemo in the Metal look and feel.

Try This: 
  1. Click the Launch button to run HtmlDemo using Java™ Web Start (download JDK 7 or later). 单击启动按钮,使用Java™Web启动运行HtmlDemo(下载JDK 7或更高版本)。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引Launches the HtmlDemo Application
  2. Edit the HTML formatting in the text area at the left and click the "Change the label" button. 编辑左侧文本区域中的HTML格式,然后单击“更改标签”按钮。The label at the right shows the result.右侧的标签显示结果。
  3. Remove the <html> tag from the text area on the left. 从左侧的文本区域中删除<html>标记。The label's text is no longer parsed as HTML.标签的文本不再解析为HTML。

Example 2: 示例2:ButtonHtmlDemo

Let us look at another example that uses HTML. 让我们看看另一个使用HTML的示例。ButtonHtmlDemo adds font, color, and other text formatting to three buttons. 将字体、颜色和其他文本格式添加到三个按钮。You can find the entire code for this program in ButtonHtmlDemo.java. 您可以在ButtonHtmlDemo.java中找到该程序的完整代码。Here is a picture of the ButtonHtmlDemo example.下面是ButtonHtmlDemo示例的图片。

Screenshot of ButtonHtmlDemo in the Metal look and feel.

Click the Launch button to run ButtonHtmlDemo using Java™ Web Start (download JDK 7 or later). 单击启动按钮,使用Java™Web启动运行ButtonHtmlDemo(下载JDK 7或更高版本)。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引

Launches the ButtonHtmlDemo Application

The left and right buttons have multiple lines and text styles and are implemented using HTML. 左右按钮具有多行和文本样式,并使用HTML实现。The middle button, on the other hand, uses just one line, font, and color, so it does not require HTML. 另一方面,中间按钮只使用一行、字体和颜色,因此不需要HTML。Here is the code that specifies the text formatting for these three buttons:下面是指定这三个按钮的文本格式的代码:

b1 = new JButton("<html><center><b><u>D</u>isable</b><br>"
                 + "<font color=#ffffdd>middle button</font>",
                 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("<html><center><b><u>E</u>nable</b><br>"
                 + "<font color=#ffffdd>middle button</font>",
                 rightButtonIcon);
b3.setFont(font);

Note that we have to use a <u> tag to cause the mnemonic characters "D" and "E" to be underlined in the buttons that use HTML. 注意,我们必须使用<u>标记,以使使用HTML的按钮中的助记符“D”和“E”带下划线。Note also that when a button is disabled, its HTML text unfortunately remains black, instead of becoming gray. 还请注意,当按钮被禁用时,其HTML文本不幸地保持黑色,而不是变成灰色。(Refer to bug #4783068 to see if this situation changes.)(请参阅bug #4783068,查看此情况是否发生变化。)

This section discussed how to use HTML in ordinary, non-text components. 本节讨论了如何在普通的非文本组件中使用HTML。For information on components whose primary purpose is formatting text, see Using Text Components.有关主要目的是格式化文本的组件的信息,请参阅使用文本组件

If you are programming in JavaFX, see HTML Editor.如果您使用JavaFX编程,请参阅HTML编辑器


Previous page: How to Use Trees
Next page: How to Use Models