📄 answers-ch3.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><html> <head> <title>Answers: Using Swing Components (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> </head><body> <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> </div> <div class=PrintHeaders> <b>Trail:</b> Creating a GUI with JFC/Swing </div> </div> </div> </div> </div> </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 href=../TOC.html>« Previous</a> • <a href=../TOC.html>TOC</a> </div> <div id=PageTitle>Answers: Using Swing Components</div> <blockquote></blockquote><BLOCKQUOTE> <P>Use the information in this lesson and the component<a class="TutorialLink" target="_top" href="../components/componentlist.html">how-to sections</a> to help you complete these questions and exercises.</P></BLOCKQUOTE><H3>Questions</H3><BLOCKQUOTE> <P><FONT COLOR="#ff0000">Question 1:</FONT> Find the component that best fits each of the following needs. Write down both the component’s common name (such as “frame”) and find the component's how-to page online. [<I>Hint: </I>You can use <A HREF="../components/components.html">A Visual Index to the Swing Components</A> to help you answer this question.]</P> <BLOCKQUOTE> <P><FONT COLOR="#ff0000">Question 1a</FONT>: A component that lets the user pick a color.<BR> <FONT COLOR="#009900">Answer 1a</FONT>: <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/colorchooser.html">color chooser</a><BR> <BR> <FONT COLOR="#ff0000">Question 1b:</FONT> A component that displays an icon, but that doesn’t react to user clicks.<BR> <FONT COLOR="#009900">Answer 1b</FONT>: <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/label.html">label</a><BR> <BR> <FONT COLOR="#ff0000">Question 1c:</FONT> A component that looks like a button and that, when pressed, brings up a menu of items for the user to choose from.<BR> <FONT COLOR="#009900">Answer 1c</FONT>: <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html">uneditable combo box</a><BR> <BR> <FONT COLOR="#ff0000">Question 1d: </FONT>A container that looks like a frame, but that appears (usually with other, similar containers) within a real frame.<BR> <FONT COLOR="#009900">Answer 1d</FONT>: <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html">internal frame</a><BR> <BR> <FONT COLOR="#ff0000">Question 1e:</FONT> A container that lets the user determine how two components share a limited amount of space.<BR> <FONT COLOR="#009900">Answer 1e</FONT>: <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/splitpane.html">split pane</a></P> </BLOCKQUOTE> <P><FONT COLOR="#ff0000">Question 2:</FONT> Which method do you use to add a menu bar to a top-level container such as a <CODE>JFrame</CODE>?<BR> <FONT COLOR="#009900">Answer 2</FONT>: <code><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html#setJMenuBar(javax.swing.JMenuBar)">setJMenuBar</a></code></P> <P><FONT COLOR="#ff0000">Question 3: </FONT>Which method do you use to specify the default button for a top-level container such as a <CODE>JFrame</CODE> or <CODE>JDialog</CODE>?<BR> <FONT COLOR="#009900">Answer 3</FONT>: <code>JRootPane</code>'s <code><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JRootPane.html#setDefaultButton(javax.swing.JButton)">setDefaultButton</a></code> method. (You get the top-level container's root pane using the <code><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/RootPaneContainer.html#getRootPane()">getRootPane</a></code> method defined by the <code>RootPaneContainer</code> interface, which every top-level container implements.)</P> <P><FONT COLOR="#ff0000">Question 4:</FONT> Which method do you use to enable and disable components such as <CODE>JButton</CODE>s? What class is it defined in?<BR> <FONT COLOR="#009900">Answer 4</FONT>: <code><a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html#setEnabled(boolean)">setEnabled</a></code>, which is defined in the <code>Component</code> class</P> <P><FONT COLOR="#ff0000">Question 5a:</FONT> Which Swing components use <code><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/ListSelectionModel.html">ListSelectionModel</a></code>? [<I>Hint:</I> The “Use” link at the top of the specification for each interface and class takes you to a page showing where in the API that interface or class is referenced.]<BR> <FONT COLOR="#009900">Answer 5a</FONT>: <code><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JList.html">JList</a></code> and <code><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html">JTable</a></code></P> <BLOCKQUOTE> <P><FONT COLOR="#ff0000">Question 5b: </FONT>Do those components use any other models to handle other aspects of the components’ state? If so, list the other models’ types.<BR> <FONT COLOR="#009900">Answer 5b</FONT>: <code>JList</code> also uses a <code>ListModel</code>, which holds the list's data. <code>JTable</code> uses a <code>TableModel</code> to hold its data and a <code>TableColumnModel</code> to manage the table's columns.</P> </BLOCKQUOTE> <P><FONT COLOR="#ff0000">Question 6:</FONT> Which type of model holds a text component’s content?<BR> <FONT COLOR="#009900">Answer 6</FONT>: <code><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/Document.html">Document</a></code><BR> </P></BLOCKQUOTE><H3>Exercises</H3><BLOCKQUOTE> <P><FONT COLOR="#ff0000">Exercise 1.</FONT> Implement a program with a GUI that looks like the one shown below. Put the main method in a class named <CODE>MyDemo1</CODE>.</P> <BLOCKQUOTE> <P><IMG SRC="../../figures/uiswing/QandE/MyDemo1.png" WIDTH="223" HEIGHT="100" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="MyDemo1.png"> </BLOCKQUOTE> <P><FONT COLOR="#009900">Answer 1</FONT>: See <a target="source" href="examples/MyDemo1.java"><font color="#bb000f"><code>MyDemo1.java</code></font></a><a href="examples/MyDemo1.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 adds the bold, italicized text:</P> <blockquote><pre>JLabel label = new JLabel("My Demo");frame.getContentPane().add(BorderLayout.CENTER, label);label.setFont(label.getFont().deriveFont(Font.ITALIC | Font.BOLD));label.setHorizontalAlignment(JLabel.CENTER)</pre></blockquote><p> <FONT COLOR="#ff0000">Exercise 2.</FONT> Make a copy of <CODE>MyDemo1.java</CODE> named <CODE>MyDemo2.java</CODE>. Add a menu bar to <CODE>MyDemo2</CODE>. <br> <FONT COLOR="#009900">Answer 2</FONT>: See <a target="source" href="examples/MyDemo2.java"><font color="#bb000f"><code>MyDemo2.java</code></font></a><a href="examples/MyDemo2.java"><img src="../../images/sourceIcon.gif" width=11 height=11 border=0 align="ABSMIDDLE" alt="(in a .java source file)"></a>. The menu bar can be implemented with this code: </p><blockquote><pre>JMenu menu = new JMenu("Menu");JMenuBar mb = new JMenuBar();mb.add(menu);frame.setJMenuBar(mb);</pre></blockquote> <P> <FONT COLOR="#ff0000">Exercise 3. </FONT>Copy <CODE>MyDemo1.java</CODE> to <CODE>MyDemo3.java</CODE>. Add a button (<CODE>JButton</CODE>) to <CODE>MyDemo3.java</CODE>. Make it the default button.<BR> <FONT COLOR="#009900">Answer 3</FONT>: See <a target="source" href="examples/MyDemo3.java"><font color="#bb000f"><code>MyDemo3.java</code></font></a><a href="examples/MyDemo3.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 adds the button and makes it the default button:</P> <blockquote><pre>JButton b = new JButton("A button");frame.getContentPane().add(BorderLayout.PAGE_END, b);frame.getRootPane().setDefaultButton(b);</pre></blockquote> <P> </P></blockquote> </blockquote> </div> <div id=Footer> <span class=NavBit> <a href=../TOC.html>« Previous</a> • <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: Using Swing Components </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -