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

📄 answers-ch4.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 2 页
字号:
        <div id=PageTitle>Answers: Laying Out Components within a Container</div>        <blockquote></blockquote><H3>Questions</H3><P>In each of the following questions, choose the layout manager(s)most naturally suited for the described layout. Assume that thecontainer controlled by the layout manager is a <CODE>JPanel</CODE>.[<I>Hint: </I>Two sections that might help are <A HREF="../components/components.html">AVisual Index to Swing Components</A> and <A HREF="../layout/using.html#choosing">Tipson Choosing a Layout Manager</A>.]</P><BLOCKQUOTE>  <P><FONT COLOR="#ff0000">Question 1</FONT>. The container has  one component that should take up as much space as possible<BR>  <BLOCKQUOTE>  <TABLE WIDTH="450" BORDER="0" CELLSPACING="2" CELLPADDING="0">    <TR>      <TD WIDTH="50%">    <IMG SRC="../../figures/uiswing/QandE/Layout1-1.png" WIDTH="123" HEIGHT="60"      ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Layout1-1.png">    </TD>      <TD WIDTH="50%">    <IMG SRC="../../figures/uiswing/QandE/Layout1-2.png" WIDTH="195" HEIGHT="100"      ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Layout1-2.png">      </TD>    </TR>  </TABLE><p></P>    <P>a. <CODE>BorderLayout</CODE><BR>    b. <CODE>GridLayout</CODE><BR>    c. <CODE>GridBagLayout</CODE><BR>    d. a and b<BR>    e. b and c</P>  </BLOCKQUOTE>  <P><FONT COLOR="#009900">Answer 1</FONT>: d. <code>BorderLayout</code> and <code>GridLayout</code>     easily deal with this situation. Although you could use <code>GridBagLayout</code>,     it's much more complex than necessary.</P>  <BR>  <P><FONT COLOR="#ff0000">Question 2</FONT>. The container has  a row of components that should all be displayed at the same  size, filling the container&#146;s entire area.</P>    <BLOCKQUOTE>    <IMG SRC="../../figures/uiswing/QandE/Layout2-1.png" WIDTH="348" HEIGHT="60"      ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Layout2-1.png">    </BLOCKQUOTE>    <BLOCKQUOTE>    <IMG SRC="../../figures/uiswing/QandE/Layout2-2.png" WIDTH="395" HEIGHT="89"      ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Layout2-2.png">    </BLOCKQUOTE>  <BLOCKQUOTE>    <P>a. <CODE>FlowLayout</CODE><BR>    b. <CODE>GridLayout</CODE><BR>    c. <CODE>BoxLayout</CODE><BR>    d. a and b</P>  </BLOCKQUOTE>  <P><FONT COLOR="#009900">Answer 2</FONT>: b. This type of same-size layout &#151;     whether in a row, a column, or a grid &#151; is what <code>GridLayout</code>     is best at. </P>  <BR>  <P><FONT COLOR="#ff0000">Question 3</FONT>. The container displays  a number of components in a column, with any extra space going  between the first two components.<BR>  <BLOCKQUOTE>  <TABLE WIDTH="450" BORDER="0" CELLSPACING="2" CELLPADDING="0">    <TR>      <TD WIDTH="50%">    <IMG SRC="../../figures/uiswing/QandE/Layout3-1.png" WIDTH="123" HEIGHT="138"      ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Layout3-1.png">     </TD>      <TD WIDTH="50%">    <IMG SRC="../../figures/uiswing/QandE/Layout3-2.png" WIDTH="186" HEIGHT="180"      ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Layout3-2.png">       </TD>    </TR>  </TABLE><p></P>    <P>a. <CODE>FlowLayout</CODE><BR>    b. <CODE>BoxLayout</CODE><BR>    c. <CODE>GridLayout</CODE><BR>    d. <CODE>BorderLayout</CODE></P>  </BLOCKQUOTE>  <P><FONT COLOR="#009900">Answer 3</FONT>: b. <code>BoxLayout</code> lays out     components in either a column or a row. You can specify extra space using     an invisible component.</P>  <BR>  <P><FONT COLOR="#ff0000">Question 4</FONT>. The container can  display three completely different components at different times,  depending perhaps on user input or program state. Even if the  components&#146; sizes differ, switching from one component to  the next shouldn&#146;t change the amount of space devoted to  the component.</P>    <BLOCKQUOTE>    <IMG SRC="../../figures/uiswing/QandE/Layout4-1.png" WIDTH="347" HEIGHT="132"      ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Layout4-1.png">    </BLOCKQUOTE>    <BLOCKQUOTE>    <p><IMG SRC="../../figures/uiswing/QandE/Layout4-2.png" WIDTH="347" HEIGHT="132"      ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Layout4-2.png"> </p>      <P>a. <CODE>SpringLayout</CODE><BR>      b. <CODE>BoxLayout</CODE><BR>      c. <CODE>CardLayout</CODE><BR>      d. <CODE>GridBagLayout</CODE></P>      </BLOCKQUOTE>  <P><FONT COLOR="#009900">Answer 4</FONT>: c. <code>CardLayout</code> exists to   allow components to share the same space. Although it's simpler to use a <code>JTabbedPane</code>   component to control an area, <code>CardLayout</code> is the solution when you   don't want tabs.</P></BLOCKQUOTE><H3>Exercises</H3><BLOCKQUOTE>  <P><FONT COLOR="#ff0000">Exercise 1</FONT>. Implement the layout  described and shown in question 1.<BR>    <FONT COLOR="#009900">Answer 1</FONT>: See <a target="source" href="examples/Layout1.java"><font color="#bb000f"><code>Layout1.java</code></font></a><a href="examples/Layout1.java"><img src="../../images/sourceIcon.gif" width=11 height=11 border=0 align="ABSMIDDLE" alt="(in a .java source file)"></a>.     Here's the code that implements the layout:</P>	<blockquote><pre>JPanel p = new JPanel(new BorderLayout());p.add(createComponent("Component 1"),                       BorderLayout.CENTER);frame.setContentPane(p);</pre></blockquote>  <br>  <P><FONT COLOR="#ff0000">Exercise 2</FONT>. Implement the layout  described and shown in question 2.<BR>  <FONT COLOR="#009900">Answer 2</FONT>: See <a target="source" href="examples/Layout2.java"><font color="#bb000f"><code>Layout2.java</code></font></a><a href="examples/Layout2.java"><img src="../../images/sourceIcon.gif" width=11 height=11 border=0 align="ABSMIDDLE" alt="(in a .java source file)"></a>.     Here's the code that implements the layout:</P>	<blockquote><pre>JPanel p = new JPanel(new GridLayout(1,0));p.add(createComponent("Component 1"));p.add(createComponent("Component 2"));p.add(createComponent("Component 3"));p.add(createComponent("Component 4"));frame.setContentPane(p);</pre></blockquote>  <br>  <P><FONT COLOR="#ff0000">Exercise 3</FONT>. Implement the layout  described and shown in question 3.<BR>  <FONT COLOR="#009900">Answer 3</FONT>: See <a target="source" href="examples/Layout3.java"><font color="#bb000f"><code>Layout3.java</code></font></a><a href="examples/Layout3.java"><img src="../../images/sourceIcon.gif" width=11 height=11 border=0 align="ABSMIDDLE" alt="(in a .java source file)"></a>.     Here's the code that implements the layout:</P>	<blockquote><pre>JPanel p = new JPanel();p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));p.add(createComponent("Component 1"));p.add(Box.createVerticalGlue());p.add(createComponent("Component 2"));p.add(createComponent("Component 3"));p.add(createComponent("Component 4"));frame.setContentPane(p);</pre></blockquote>  <br>  <P><FONT COLOR="#ff0000">Exercise 4</FONT>. Implement the layout  described and shown in question 4.<BR>  <FONT COLOR="#009900">Answer 4</FONT>: See <a target="source" href="examples/Layout4.java"><font color="#bb000f"><code>Layout4.java</code></font></a><a href="examples/Layout4.java"><img src="../../images/sourceIcon.gif" width=11 height=11 border=0 align="ABSMIDDLE" alt="(in a .java source file)"></a>.     Here's the code that implements the layout:</P>	<blockquote><pre><em>...//Where the radio buttons are set up:</em>for (int i= 0; i < strings.length; i++) {    ...	rb[i].setActionCommand(String.valueOf(i));    ...}<em>...//Where the panel to contain the shared-space components is set up:</em>cards = new JPanel(new CardLayout());for (int i = 0; i < strings.length; i++) {	cards.add(createComponent(strings[i]), String.valueOf(i));}<em>...//In the action listener for the radio buttons:</em>public void actionPerformed(ActionEvent evt) {	CardLayout cl = (CardLayout)(cards.getLayout());	cl.show(cards, (String)evt.getActionCommand());}</pre></blockquote>  <br>  <P><FONT COLOR="#ff0000">Exercise 5</FONT>. By adding a single line of code,     make the program you wrote for Exercise 2 display the components from right-to-left,     instead of from left-to-right.</P>    <BLOCKQUOTE>    <IMG SRC="../../figures/uiswing/QandE/Layout2-3.png" WIDTH="348" HEIGHT="60"      ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Layout2-3.png">    </BLOCKQUOTE>  <P><FONT COLOR="#009900">Answer 5</FONT>: You can change the horizontal orientation     using the <code><a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html#setComponentOrientation(java.awt.ComponentOrientation)">setComponentOrientation</a></code>     method defined by the <code>Component</code> class. For example:   <blockquote><pre>p.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);</pre>  </blockquote></BLOCKQUOTE>        </blockquote>    </div>    <div id=Footer>        <span class=NavBit>            <a href=../TOC.html>&laquo; Previous</a>            &bull;            <a href=../TOC.html>TOC</a>        </span><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> Questions and Exercises: Laying Out Components within a Container    </div>    </body></html> 

⌨️ 快捷键说明

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