如何将注意力集中在JTextField上?

我让我的游戏没有鼠标运行所以使用指针不是一个选择。 当玩家输球时,高分菜单会显示。

这是我的代码

highScore=new MyTextField("Your Name"); highScore.addKeyListener(this); highScore.setFont(font); highScore.requestFocusInWindow(); 

我努力了

 highScore.setFocusable(true); highScore.requestFocusInWindow(); highScore.requestFocus(true); highScore.requestFocus(); 

但仍然没有把注意力集中在我的JTextField

如何关注它?

如果您希望在GUI出现时聚焦JTextField ,可以使用:

 in = new JTextField(40); f.addWindowListener( new WindowAdapter() { public void windowOpened( WindowEvent e ){ in.requestFocus(); } }); 

其中f是你的JFrame ,而你的是JTextField

如果只有一个顶级容器,那么GUI构造函数中的最后一行将是例如

 . . . myFrame.setVisible(true); EventQueue.invokeLater(new Runnable() { @Override public void run() { myComponent.grabFocus(); myComponent.requestFocus();//or inWindow } }); 
 public void actionPerformed(ActionEvent arg0) { if (arg0.getSource()==clearButton) { enterText.setText(null); enterText.grabFocus(); //Places flashing cursor on text box } } 

试试这个,

 myFrame.setVisible(true); EventQueue.invokeLater(new Runnable() { @Override public void run() { myComponent.grabFocus(); myComponent.requestFocus();//or inWindow } }); 

如果页面包含多个项目并且喜欢设置标签序列和焦点,我建议使用FocusTraversalPolicy。

如果您使用FocusTraversalPolicy,grabFocus()将无法工作。

示例代码

 int focusNumber = 0; Component[] focusList; focusList = new Component[] { game, move, amount, saveButton, printButton, editButton, deleteButton, newButton, settingsButton }; frame.setFocusTraversalPolicy(new FocusTraversalPolicy() { @Override public Component getLastComponent(Container aContainer) { return focusList[focusList.length - 1]; } @Override public Component getFirstComponent(Container aContainer) { return focusList[0]; } @Override public Component getDefaultComponent(Container aContainer) { return focusList[1]; } @Override public Component getComponentAfter(Container focusCycleRoot, Component aComponent) { focusNumber = (focusNumber + 1) % focusList.length; if (focusList[focusNumber].isEnabled() == false) { getComponentAfter(focusCycleRoot, focusList[focusNumber]); } return focusList[focusNumber]; } @Override public Component getComponentBefore(Container focusCycleRoot, Component aComponent) { focusNumber = (focusList.length + focusNumber - 1) % focusList.length; if (focusList[focusNumber].isEnabled() == false) { getComponentBefore(focusCycleRoot, focusList[focusNumber]); } return focusList[focusNumber]; } }); 

在我的情况下,上面没有任何工作,直到我的构造函数返回我调用requestFocus()。

 MyPanel panel = new MyPanel(...); frame.add(panel); panel.initFocus(); 

MyPanel.initFocus()将具有:

 myTextField.requestFocus(); 

它有效。

这段代码鼠标光标“jtextfield”“Jcombobox”位置集中

  try { Robot robot = new Robot(); int x = Jtextfield.getLocationOnScreen().x; int y= Jtextfield.getLocationOnScreen().y; JOptionPane.showMessageDialog(null, x+"x< - y>"+y);// for I location see robot.mouseMove(x, y); } catch (AWTException ex) { ex.printStackTrace(); } 

尝试使用时,它对我不起作用:

 JOptionPane.showConfirmDialog(...) 

但是 – 我找到了解决方案! 非常原始,但有效。

只需通过java.awt跳转到该字段。 机器人使用键“Tab”。 例如:

 Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_TAB); robot.delay(100); robot.keyRelease(KeyEvent.VK_TAB); 

如果你应该在“Tab”上按多次以获得你的组件,你可以使用以下方法:

 GUIUtils.pressTab(3); 

定义:

 public static void pressTab(int amountOfClickes) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { Robot robot = new Robot(); int i = amountOfClickes; while (i-- > 0) { robot.keyPress(KeyEvent.VK_TAB); robot.delay(100); robot.keyRelease(KeyEvent.VK_TAB); } } catch (AWTException e) { System.out.println("Failed to use Robot, got exception: " + e.getMessage()); } } }); } 

如果您的Component位置是动态的,您可以无限制地运行while循环,但是在组件上添加一些焦点侦听器,一旦到达它就停止循环。

虽然yourTextField.requestFocus()是一个解决方案,但它不是最好的,因为在官方Java文档中这是不鼓励的,因为requestFocus()方法依赖于平台。

文件说:

请注意,不鼓励使用此方法,因为其行为取决于平台。 相反,我们建议使用requestFocusInWindow()。

请改用yourJTextField.requestFocusInWindow()