The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Problem: My application is not showing the look and feel I have requested via UIManager.setLookAndFeel
.
You probably either set the look and feel to an invalid look and feel or set it after the UI manager loaded the default look and feel. If you are sure that the look and feel you specified is valid and setting the look and feel is the first thing your program does (at the top of its main method, for example), check whether you have a static field that references a Swing class. This reference can cause the default look and feel to be loaded if none has been specified. For more information, including how to set a look and feel after the GUI has been created, see the look and feel section.
Problem: Why is not my component getting the focus?
JComponent
) that you created? If so, you may need to give your component an input map and mouse listener. See How to Make a Custom Component Focusable for more information and a demo.JWindow
object? The focus system requires a JWindow
's owning frame to be visible for any components in the JWindow
object to get the focus. By default, if you do not specify an owning frame for a JWindow
object, an invisible owning frame is created for it. The solution is to either specify a visible and focusable owning frame when creating the JWindow
object or to use JDialog
or JFrame
objects instead.Problem: Why cannot my dialog receive the event generated when the user hits the Escape key?
If your dialog contains a text field, it may be consuming the event.
KeyEventDispatcher
.JComponent
component in the JDialog
object, using the WHEN_IN_FOCUSED_WINDOW
input map. For more information, see the How to Use Key Bindings page.Problem: Why I cannot apply Swing components to a tray icon? Current implementation of the TrayIcon
class supports the PopupMenu
component, but not its Swing counterpart JPopupMenu
. This limitation narrows capabilities to employ additional Swing features, for example, menu icons. See the Bug ID 6285881.
JTrayIcon
class will be created to eliminate this inconvenience. Until then, use AWT components to add a menu item, check box menu item, or submenu.If you do not find your problem in this section, consult Solving Common Component Problems.