📄 questions-ch4.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><html> <head> <title>Questions and Exercises: Laying Out Components within a Container (The Java™ Tutorials > Creating a GUI with JFC/Swing)</title><style type="text/css"> .FigureCaption { margin-left: 1in; margin-right: 1in; font-family: sans-serif; font-size: smaller; text-align: justify; } #TopBar_bl { background: url(../../images/java_bar_bl.gif) 0 100% no-repeat; width: 100%; height: 60px; } #TopBar_br { background: url(../../images/java_bar_br.gif) 100% 100% no-repeat; width: 100%; height: 60px; } #TopBar_tl { background: url(../../images/java_bar_tl.gif) 0 0 no-repeat; width: 100%; height: 60px; } #TopBar_tr { background: url(../../images/java_bar_tr.gif) 100% 0 no-repeat; width: 100%; height: 60px; } #TopBar { background: #35556B url(../../images/java_bar.gif); margin: 10px 10px 0 10px; height:60px; min-width:700px; color: white; font-family: sans-serif; font-weight: bold; } @media print { #BreadCrumbs, #Download { display: none; } } #TopBar_right { line-height: 14px; float: right; padding-top: 2px; padding-right: 30px; text-align: center; } @media print { #TopBar_right { display: none; } } #TopBar_right a { font-size: 12px; margin: 3px; padding: 0; } #TopBar a:visited, #TopBar a:link { color: white; text-decoration: none; } #TopBar a:hover, #TopBar a:active { background-color: white; color: #35556B; } #BreadCrumbs { padding: 4px 5px 0.5em 0; font-family: sans-serif; float: right; } #BreadCrumbs a { color: blue; } #BreadCrumbs a:visited, #BreadCrumbs a:link { text-decoration: none; } #BreadCrumbs a:hover, #BreadCrumbs a:active { text-decoration: underline; } #PageTitle { margin: 0 5px 0.5em 0; color: #E76F00; font-family: sans-serif; font-weight: bold; font-size: 20px; } .LeftBar_shown { width: 13em; float: left; margin-left: 10px; margin-top: 4px; margin-bottom: 2em; } @media print { .LeftBar_shown { display: none; } } .LeftBar_hidden { display: none; } #Footer { padding-top: 10px; padding-left: 10px; margin-right: 10px; } .NavBit { padding: 4px 5px 0.5em 0; font-family: sans-serif; } @media print { .NavBit { display: none; } } #TagNotes { text-align: right; font-size: smaller; font-family: sans-serif; } @media print { #TagNotes a:visited, #TagNotes a:link { color: #35556B; text-decoration: none; } } #Contents a, .NavBit a, #TagNotes a { color: blue } #TagNotes a:visited, #TagNotes a:link, #Contents a:visited, #Contents a:link, .NavBit a:visited, .NavBit a:link { text-decoration: none; } #TagNotes a:hover, #TagNotes a:active, #Contents a:hover, #Contents a:active, .NavBit a:hover, .NavBit a:active { text-decoration: underline; } #Contents { float: left; font-family: sans-serif; } @media print { #Contents { display: none; } } @media screen { div.PrintHeaders { display: none; } } .linkLESSON, .nolinkLESSON { margin-left: 0.5em; text-indent: -0.5em } .linkAHEAD, .nolinkAHEAD, .linkQUESTIONS, .nolinkQUESTIONS { margin-left: 1.5em; text-indent: -0.5em } .linkBHEAD, .nolinkBHEAD { margin-left: 2.5em; text-indent: -0.5em } .linkCHEAD, .nolinkCHEAD { margin-left: 3.5em; text-indent: -0.5em } .nolinkLESSON, .nolinkAHEAD, .nolinkBHEAD, .nolinkCHEAD, .nolinkQUESTIONS { font-weight: bold; color: #E76F00; } .MainFlow_indented { margin-right: 10px; margin-left: 15em; margin-bottom: 2em; } .MainFlow_wide { margin-right: 10px; margin-left: 10px; margin-bottom: 2em; } @media print { .MainFlow_indented, .MainFlow_wide { padding-top: 0; margin-top: 10px; margin-right: 10px; margin-left: 0; } } h3, h4, h5 { color: #E76F00; font-family: sans-serif; } #ToggleLeft { display: none; } /t</style><script type="text/javascript"> function leftBar() { var nameq = 'tutorial_showLeftBar=' var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookieString = cookies[i]; while (cookieString.charAt(0) == ' ') { cookieString = cookieString.substring(1, cookieString.length); } if (cookieString.indexOf(nameq) == 0) { cookieValue = cookieString.substring(nameq.length, cookieString.length); return cookieValue == 'yes'; } } return true; } function showLeft(b) { var contents = document.getElementById("LeftBar"); var main = document.getElementById("MainFlow"); var toggle = document.getElementById("ToggleLeft"); if (b) { contents.className = "LeftBar_shown"; main.className = "MainFlow_indented"; toggle.innerHTML = "Hide the TOC"; document.cookie = 'tutorial_showLeftBar=yes; path=/'; } else { contents.className = "LeftBar_hidden"; main.className = "MainFlow_wide"; toggle.innerHTML = "Show the TOC"; document.cookie = 'tutorial_showLeftBar=no; path=/'; } } function toggleLeft() { showLeft(document.getElementById("LeftBar").className == "LeftBar_hidden"); document.getElementById("ToggleLeft").blur(); } function load() { 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 </div> <div class=MainFlow_wide> <span id=BreadCrumbs> <a href=../../index.html target=_top>Home Page</a> > <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a> </span> <div class=NavBit> <a target=_top href=../TOC.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=../lookandfeel/index.html>Next »</a> </div> <div id=PageTitle>Questions and Exercises: 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>1. 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>2. The container has a row of components that should all be displayed at the same size, filling the container’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>3. 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>4. The container can display three completely different components at different times, depending perhaps on user input or program state. Even if the components’ sizes differ, switching from one component to the next shouldn’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> <IMG SRC="../../figures/uiswing/QandE/Layout4-2.png" WIDTH="347" HEIGHT="132" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Layout4-2.png"> <P>a. <CODE>SpringLayout</CODE><BR> b. <CODE>BoxLayout</CODE><BR> c. <CODE>CardLayout</CODE><BR> d. <CODE>GridBagLayout</CODE></P> </BLOCKQUOTE></BLOCKQUOTE><H3>Exercises</H3><BLOCKQUOTE> <P>1. Implement the layout described and shown in question 1.</P> <P>2. Implement the layout described and shown in question 2.</P> <P>3. Implement the layout described and shown in question 3.</P> <P>4. Implement the layout described and shown in question 4.</P> <P>5. 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></BLOCKQUOTE><a class="TutorialLink" target="_top" href="answers-ch4.html">Check your answers.</a> </blockquote> <div class=NavBit> <a target=_top href=../TOC.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=../lookandfeel/index.html>Next »</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> Solving Common Layout Problems <br><b>Next page:</b> Modifying the Look and Feel </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -