epubjs
Version:
Render ePub documents in the browser, across many devices
123 lines (112 loc) • 16.5 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Buttons and Labels</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="Buttons and Labels"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-17-SECT-1"/>Buttons and Labels</h1></div></div></div><p><a id="idx10973" class="indexterm"/> <a id="idx10983" class="indexterm"/> <a id="idx10991" class="indexterm"/> <a id="idx10998" class="indexterm"/> We’ll start with the simplest components: buttons and
labels. Frankly, there isn’t much to say about them. If you’ve seen one
button, you’ve seen them all, and you’ve already seen buttons in the
applications in <a class="xref" href="ch02.html" title="Chapter 2. A First Application">Chapter 2</a> (<code class="literal">HelloJava3</code> and <code class="literal">HelloJava4</code>). A button generates an <code class="literal">ActionEvent</code> when the user presses it. To receive
these events, your program registers an <code class="literal">ActionListener</code>, which must implement the
<code class="literal">actionPerformed()</code> method. The argument
passed to <code class="literal">actionPerformed()</code> is the
event itself.</p><p>There’s one more thing worth saying about buttons, which applies to
any component that generates an action event. Java lets us specify an
“action command” string for buttons (and other components, like menu
items, that can generate action events). The action command is less
interesting than it sounds. It is just a <code class="literal">String</code> that serves to identify the component
that sent the event. By default, the action command of a <a id="I_indexterm17_id793867" class="indexterm"/><code class="literal">JButton</code> is the same as
its label; it is included in action events so that you could use it to
figure out which button an event came from. However, you’ll often know
this from the context of your event listener.</p><p>To get the action command from an action event, call the event’s
<a id="I_indexterm17_id793885" class="indexterm"/><code class="literal">getActionCommand()</code>
method. The following code checks whether the user pressed the button
labeled <span class="emphasis"><em>Yes</em></span>:</p><a id="I_17_tt1002"/><pre class="programlisting"> <code class="kd">public</code> <code class="kt">void</code> <code class="nf">actionPerformed</code><code class="o">(</code><code class="n">ActionEvent</code> <code class="n">e</code><code class="o">){</code>
<code class="k">if</code> <code class="o">(</code><code class="n">e</code><code class="o">.</code><code class="na">getActionCommand</code><code class="o">().</code><code class="na">equals</code><code class="o">(</code><code class="s">"Yes"</code><code class="o">)</code> <code class="o">{</code>
<code class="c1">//the user pressed "Yes"; do something</code>
<code class="o">...</code>
<code class="o">}</code>
<code class="o">}</code></pre><p><span class="emphasis"><em>Yes</em></span> is a string, not a command per se. You can
change the action command by calling the button’s <a id="I_indexterm17_id793916" class="indexterm"/><code class="literal">setActionCommand()</code>
method. The following code changes button <code class="literal">myButton</code>’s action command to “confirm:”</p><a id="I_17_tt1003"/><pre class="programlisting"> <code class="n">myButton</code><code class="o">.</code><code class="na">setActionCommand</code><code class="o">(</code><code class="s">"confirm"</code><code class="o">);</code></pre><p>It’s a good idea to get used to setting action commands explicitly;
this helps to prevent your code from breaking when you or some other
developer internationalizes it or otherwise changes the button’s label. If
you rely on the button’s label, your code stops working as soon as that
label changes; a French user might see the label <code class="literal">Oui</code> rather than <code class="literal">Yes</code>.</p><p>Swing buttons can have an image in addition to a label. The <code class="literal">JButton</code> class includes constructors that accept
an <code class="literal">Icon</code> object, which knows how to draw
itself. You can create buttons with captions, images, or both. A handy
class called <a id="I_indexterm17_id793974" class="indexterm"/><code class="literal">ImageIcon</code> takes care of
loading an image for you and can be used to add an image to a button. The
following example shows how this works:</p><a id="I_17_tt1004"/><pre class="programlisting"> <code class="c1">//file: PictureButton.java</code>
<code class="kn">import</code> <code class="nn">java.awt.*</code><code class="o">;</code>
<code class="kn">import</code> <code class="nn">java.awt.event.*</code><code class="o">;</code>
<code class="kn">import</code> <code class="nn">javax.swing.*</code><code class="o">;</code>
<code class="kd">public</code> <code class="kd">class</code> <code class="nc">PictureButton</code> <code class="o">{</code>
<code class="kd">public</code> <code class="kd">static</code> <code class="kt">void</code> <code class="nf">main</code><code class="o">(</code><code class="n">String</code><code class="o">[]</code> <code class="n">args</code><code class="o">)</code>
<code class="o">{</code>
<code class="n">JFrame</code> <code class="n">frame</code> <code class="o">=</code> <code class="k">new</code> <code class="n">JFrame</code><code class="o">();</code>
<code class="n">Icon</code> <code class="n">icon</code> <code class="o">=</code> <code class="k">new</code> <code class="n">ImageIcon</code><code class="o">(</code><code class="s">"rhino.gif"</code><code class="o">);</code>
<code class="n">JButton</code> <code class="n">button</code> <code class="o">=</code> <code class="k">new</code> <code class="n">JButton</code><code class="o">(</code><code class="n">icon</code><code class="o">);</code>
<code class="n">button</code><code class="o">.</code><code class="na">addActionListener</code><code class="o">(</code> <code class="k">new</code> <code class="n">ActionListener</code><code class="o">()</code> <code class="o">{</code>
<code class="kd">public</code> <code class="kt">void</code> <code class="nf">actionPerformed</code><code class="o">(</code><code class="n">ActionEvent</code> <code class="n">ae</code><code class="o">)</code> <code class="o">{</code>
<code class="n">System</code><code class="o">.</code><code class="na">out</code><code class="o">.</code><code class="na">println</code><code class="o">(</code><code class="s">"Urp!"</code><code class="o">);</code>
<code class="o">}</code>
<code class="o">});</code>
<code class="n">frame</code><code class="o">.</code><code class="na">getContentPane</code><code class="o">().</code><code class="na">add</code><code class="o">(</code> <code class="n">button</code> <code class="o">);</code>
<code class="n">frame</code><code class="o">.</code><code class="na">pack</code><code class="o">();</code>
<code class="n">frame</code><code class="o">.</code><code class="na">setDefaultCloseOperation</code><code class="o">(</code> <code class="n">JFrame</code><code class="o">.</code><code class="na">EXIT_ON_CLOSE</code> <code class="o">);</code>
<code class="n">frame</code><code class="o">.</code><code class="na">setVisible</code><code class="o">(</code><code class="kc">true</code><code class="o">);</code>
<code class="o">}</code>
<code class="o">}</code></pre><p>The example creates an <code class="literal">ImageIcon</code>
from the <span class="emphasis"><em>rhino.gif</em></span> file. Then, a <code class="literal">JButton</code> is created from the <code class="literal">ImageIcon</code>. The whole thing is displayed in a
<code class="literal">JFrame</code>. This example also shows the
idiom of using an anonymous inner class as an <code class="literal">ActionListener</code>.</p><p>There’s even less to be said about <a id="I_indexterm17_id794036" class="indexterm"/><code class="literal">JLabel</code> components.
They’re just text strings or images housed in a component. There aren’t
any special events associated with labels; about all you can do is specify
the text’s alignment, which controls the position of the text within the
label’s display area. As with buttons, <code class="literal">JLabel</code>s can be created with <code class="literal">Icon</code>s if you want to create a picture label. The
following code creates some labels with different options:</p><a id="I_17_tt1005"/><pre class="programlisting"> <code class="c1">// default alignment (CENTER)</code>
<code class="n">JLabel</code> <code class="n">label1</code> <code class="o">=</code> <code class="k">new</code> <code class="n">JLabel</code><code class="o">(</code><code class="s">"Lions"</code><code class="o">);</code>
<code class="c1">// left aligned</code>
<code class="n">JLabel</code> <code class="n">label2</code> <code class="o">=</code> <code class="k">new</code> <code class="n">JLabel</code><code class="o">(</code><code class="s">"Tigers"</code><code class="o">,</code> <code class="n">SwingConstants</code><code class="o">.</code><code class="na">LEFT</code><code class="o">);</code>
<code class="c1">//label with no text, default alignment</code>
<code class="n">JLabel</code> <code class="n">label3</code> <code class="o">=</code> <code class="k">new</code> <code class="n">JLabel</code><code class="o">();</code>
<code class="c1">// create image icon</code>
<code class="n">Icon</code> <code class="n">icon</code> <code class="o">=</code> <code class="k">new</code> <code class="n">ImageIcon</code><code class="o">(</code><code class="s">"rhino.gif"</code><code class="o">);</code>
<code class="c1">// create image label</code>
<code class="n">JLabel</code> <code class="n">label4</code> <code class="o">=</code> <code class="k">new</code> <code class="n">JLabel</code><code class="o">(</code><code class="n">icon</code><code class="o">);</code>
<code class="c1">// assigning text to label3</code>
<code class="n">label3</code><code class="o">.</code><code class="na">setText</code><code class="o">(</code><code class="s">"and Bears"</code><code class="o">);</code>
<code class="c1">// set alignment</code>
<code class="n">label3</code><code class="o">.</code><code class="na">setHorizontalAlignment</code><code class="o">(</code><code class="n">SwingConstants</code><code class="o">.</code><code class="na">RIGHT</code><code class="o">);</code></pre><p>The alignment constants are defined in the <code class="literal">SwingConstants</code> interface.</p><p>We’ve built several labels using a variety of constructors and
several of the class’s methods. To display the labels, just add them to a
container by calling the container’s <code class="literal">add()</code> method.</p><p>You can set other label characteristics, such as changing their font
or color, using the methods of the <code class="literal">Component</code> class, <code class="literal">JLabel</code>’s distant ancestor. For example, you can
call <code class="literal">setFont()</code> and <code class="literal">setBackground()</code> on a label, as with any other
component.</p><p>Given that labels are so simple, why do we need them at all? Why not
find a way to draw a text string directly on the container object?
Remember that a <code class="literal">JLabel</code> is a <code class="literal">JComponent</code>. That means that labels have the
normal complement of methods for setting fonts and colors that we
mentioned earlier as well as the ability to be persistently and sensibly
managed by a layout manager. Therefore, they’re much more flexible than a
text string drawn procedurally at an arbitrary location within a
container. Speaking of layouts—if you use the <a id="I_indexterm17_id794141" class="indexterm"/><code class="literal">setText()</code> method to
change the text of your label, the label’s preferred size may change. But
the label’s container automatically lays out its components when this
happens so you don’t have to worry about it.</p><div class="sect2" title="HTML Text in Buttons and Labels"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-17-SECT-2"/>HTML Text in Buttons and Labels</h2></div></div></div><p><a id="idx10978" class="indexterm"/> A neat feature of Swing is that it can interpret
HTML-formatted text in <code class="literal">JLabel</code> and
<code class="literal">JButton</code> labels. The following example
shows how to create a button with some HTML-formatted text:</p><a id="I_17_tt1006"/><pre class="programlisting"> <code class="n">JButton</code> <code class="n">button</code> <code class="o">=</code> <code class="k">new</code> <code class="n">JButton</code><code class="o">(</code>
<code class="s">"<html>"</code>
<code class="o">+</code> <code class="s">"S<font size=-1>MALL<font size=+0> "</code>
<code class="o">+</code> <code class="s">"C<font size=-1>APITALS"</code><code class="o">);</code></pre><p>Older versions of Java may not render complex HTML very well. But
as of JDK 1.4, most basic HTML features are supported, including crazy
things such as images and tables.</p><p><a class="xref" href="ch17s01.html#learnjava3-CHP-17-FIG-1" title="Figure 17-1. Button using HTML table">Figure 17-1</a> uses an HTML table to
arrange its text.</p><div class="figure"><a id="learnjava3-CHP-17-FIG-1"/><div class="figure-contents"><div class="mediaobject"><a id="I_17_tt1007"/><img src="httpatomoreillycomsourceoreillyimages1707653.png.jpg" alt="Button using HTML table"/></div></div><p class="title">Figure 17-1. Button using HTML table</p></div><p><a class="xref" href="ch17s01.html#learnjava3-CHP-17-FIG-2" title="Figure 17-2. Button using HTML img tag">Figure 17-2</a> uses an HTML image tag
to display an image.</p><div class="figure"><a id="learnjava3-CHP-17-FIG-2"/><div class="figure-contents"><div class="mediaobject"><a id="I_17_tt1008"/><img src="httpatomoreillycomsourceoreillyimages1707654.png" alt="Button using HTML img tag"/></div></div><p class="title">Figure 17-2. Button using HTML img tag</p></div><p>The code for the two figures looks like this:<a id="I_indexterm17_id794255" class="indexterm"/><a id="I_indexterm17_id794262" class="indexterm"/><a id="I_indexterm17_id794269" class="indexterm"/><a id="I_indexterm17_id794276" class="indexterm"/><a id="I_indexterm17_id794283" class="indexterm"/></p><a id="I_17_tt1009"/><pre class="programlisting"> <code class="n">String</code> <code class="n">html</code><code class="o">=</code>
<code class="s">"<html><table border=1>"</code>
<code class="o">+</code><code class="s">"<tr><td>One</td><td>Two</td></tr>"</code>
<code class="o">+</code><code class="s">"<tr><td>Three</td><td>Four</td></tr>"</code>
<code class="o">+</code><code class="s">"</table>"</code><code class="o">;</code>
<code class="n">JButton</code> <code class="n">button</code> <code class="o">=</code> <code class="k">new</code> <code class="n">JButton</code><code class="o">(</code><code class="n">html</code><code class="o">);</code>
<code class="n">String</code> <code class="n">html2</code><code class="o">=</code>
<code class="s">"<html><h3>Learning Java</h3>"</code>
<code class="o">+</code><code class="s">"<img src=\"http://www.oreilly.com/catalog/covers/learnjava3.s.gif\">"</code><code class="o">;</code>
<code class="n">Jbutton</code> <code class="n">button2</code> <code class="o">=</code> <code class="k">new</code> <code class="n">JButton</code><code class="o">(</code><code class="n">html2</code><code class="o">);</code></pre></div></div></body></html>