Java 教程是为 JDK 8 编写的。本页中描述的示例和实践未利用在后续版本中引入的改进。
Use the information in this lesson and the component how-to sections to help you complete these questions and exercises.
问题1: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.
问题1a: A component that lets the user pick a color.
答案1a: color chooser
问题1b:A component that displays an icon, but that doesn't react to user clicks.
答案1b: label
问题1c:A component that looks like a button and that, when pressed, brings up a menu of items for the user to choose from.
答案1c: uneditable combo box
问题1d:A container that looks like a frame, but that appears (usually with other, similar containers) within a real frame.
答案1d: internal frame
问题1e:A container that lets the user determine how two components share a limited amount of space.
答案1e: split pane
问题2:Which method do you use to add a menu bar to a top-level container such as a JFrame
?
答案2:setJMenuBar
问题3:Which method do you use to specify the default button for a top-level container such as a JFrame
or JDialog
?
答案3:JRootPane
's setDefaultButton
method. (You get the top-level container's root pane using the getRootPane
method defined by the RootPaneContainer
interface, which every top-level container implements.)
问题4:Which method do you use to enable and disable components such as JButton
s?What class is it defined in?
答案4:setEnabled
, which is defined in the Component
class
问题5a:Which Swing components use ListSelectionModel
?[Hint: 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.]
答案5a: JList
and JTable
问题5b:Do those components use any other models to handle other aspects of the components' state?If so, list the other models' types.
答案5b: JList
also uses a ListModel
, which holds the list's data. JTable
uses a TableModel
to hold its data and a TableColumnModel
to manage the table's columns.
问题6:Which type of model holds a text component's content?
答案6:Document
练习1.Implement a program with a GUI that looks like the one shown below. Put the main method in a class named MyDemo1
.
答案1:See MyDemo1.java
. Here's the code that adds the bold, italicized text:
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)
练习2.Make a copy of MyDemo1.java
named MyDemo2.java
. Add a menu bar to MyDemo2
.
答案2:See MyDemo2.java
. The menu bar can be implemented with this code:
JMenu menu = new JMenu("Menu"); JMenuBar mb = new JMenuBar(); mb.add(menu); frame.setJMenuBar(mb);
练习3.Copy MyDemo1.java
to MyDemo3.java
. Add a button (JButton
) to MyDemo3.java
. Make it the default button.
答案3:See MyDemo3.java
. Here's the code that adds the button and makes it the default button:
JButton b = new JButton("A button"); frame.getContentPane().add(BorderLayout.PAGE_END, b); frame.getRootPane().setDefaultButton(b);