31

我让我的游戏在没有鼠标的情况下运行,所以使用指针不是一种选择。高分菜单将在玩家输掉时显示。

这是我的代码

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

我试过了

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

但仍然没有引起我的关注JTextField

如何集中注意力?

4

10 回答 10

60

如果您希望JTextField在您的 GUI 出现时集中注意力,您可以使用以下命令:

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

f你在哪里JFramein是你的JTextField

于 2011-07-17T10:42:31.737 回答
8

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

.
.
.
myFrame.setVisible(true);
EventQueue.invokeLater(new Runnable() {

   @Override
     public void run() {
         myComponent.grabFocus();
         myComponent.requestFocus();//or inWindow
     }
});
于 2011-07-17T10:49:20.843 回答
6
public void actionPerformed(ActionEvent arg0)
{
    if (arg0.getSource()==clearButton)
    {
        enterText.setText(null);
        enterText.grabFocus();  //Places flashing cursor on text box        
    }       
}
于 2013-11-17T03:32:57.783 回答
3

试试这个,

myFrame.setVisible(true);
EventQueue.invokeLater(new Runnable() {

   @Override
     public void run() {
         myComponent.grabFocus();
         myComponent.requestFocus();//or inWindow
     }
});
于 2015-05-27T15:04:07.797 回答
0

如果页面包含多个项目并且喜欢设置选项卡顺序和焦点,我会建议使用 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];
        }
    });
于 2015-07-19T05:35:57.693 回答
0

在我的情况下,在我的构造函数返回我调用 requestFocus() 之前,以上没有任何效果。

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

MyPanel.initFocus() 将具有:

myTextField.requestFocus();

它有效。

于 2016-02-15T21:10:32.290 回答
0

此代码鼠标光标“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();
    } 
于 2017-05-02T17:55:55.723 回答
0

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

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());
            }
        }
    });
}

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

于 2018-05-16T08:26:32.613 回答
0

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

文档说:

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

改为使用yourJTextField.requestFocusInWindow()

于 2018-05-20T11:15:01.507 回答
0

怎么样把 jTextField.requestFocusInWindow(); 进入 jTextField FocusLost 事件?为我工作在 JPanel 上有 5 个控件单击 MessageBox 后,焦点就会丢失在 jTextField 上。使用了所有建议的代码,但没有运气只有上述方法适用于我的情况。

于 2020-06-17T05:29:47.103 回答