从Swing教程
如果要确保特定组件在第一次激活窗口时获得焦点,则可以在实现组件之后但在显示框架之前调用组件上的requestFocusInWindow方法。以下示例代码显示了如何完成此操作:
//...Where initialization occurs...Jframe frame = new Jframe("Test");JPanel panel = new JPanel(new BorderLayout());//...Create a variety of components here...//Create the component that will have the initial focus.JButton button = new JButton("I am first");panel.add(button);frame.getContentPane().add(panel); //Add it to the panelframe.pack(); //Realize the components.//This button will have the initial focus.button.requestFocusInWindow(); frame.setVisible(true); //Display the window.


