文档

Java™ 教程-Java Tutorials 中文版
Trail: Creating a GUI With JFC/Swing
主页>使用 JFC/Swing 创建 GUI

Answers: Writing Event Listeners

Use this lesson’s tables, the component how-to sections and the event listeners how-to sections to complete these questions and exercises.

问题

问题1:What listener would you implement to be notified when a particular component has appeared on screen?What method tells you this information?
答案1:You would register a ComponentListener on the component. The componentShown method. This method is called when the window is first displayed or is deiconified.

问题2:What listener would you implement to be notified when the user has finished editing a text field by pressing Enter?What listener would you implement to be notified as each character is typed into a text field?Note that you should not implement a general-purpose key listener, but a listener specific to text.
答案2:To be notified when the user presses Enter, you would register an ActionListener on the text field; the actionPerformed method is called when the user types Enter. Note that the Enter character is not part of the resulting string. To be notified as each character is typed, you would register a DocumentListener on the text field's Document. The insertUpdate method is then called as each character is typed. Note that this is not the correct way to implement input validation. For that behavior you should check out the Input Verification API section in How to Use the Focus Subsystem.

问题3:What listener would you implement to be notified when a spinner’s value has changed?How would you get the spinner’s new value?
答案3:To be notified when the value has changed, you would register a ChangeListener on the spinner. You would get the new value through the event's source in the stateChanged method. The following code snippet shows how this could be done:

public void stateChanged(ChangeEvent e) {
    JSpinner mySpinner = (JSpinner)(e.getSource());
    SpinnerNumberModel model = (SpinnerNumberModel)(mySpinner.getModel());
    Number currentValue = model.getNumber();
    ...
}

问题4:The default behavior for the focus subsystem is to consume the focus traversal keys, such as Tab and Shift Tab. Say you want to prevent this behavior in one of your application’s components. How would you accomplish this?
答案4:You call setFocusTraversalKeysEnabled(false) on that particular component. Note that you must then handle focus traversal manually. See How to Write a Key Listener and How to Use the Focus Subsystem for more information.

练习

练习1.Take the Beeper.java(in a .java source file) example and add a text field. Implement it so that when the user has finishing entering data, the system beeps.
答案1:See Beeper1.java(in a .java source file)

练习2.Take the Beeper.java(in a .java source file) example and add a selectable component that allows the user to enter a number from 1 to 10. For example, you can use a combo box, a set of radio buttons, or a spinner. Implement it so that when the user has selected the number, the system beeps that many times.
答案2:See Beeper2.java(in a .java source file)


Previous page: Questions and Exercises: Writing Event Listeners